1

In my object, have different key for each of the object. for that how to write generic data type?

here is my try:

interface Dic {
    [key:string]:string; // how to write for generic values
    age:number;
}

const Object1:Dic = {
        name:"name1",
        age:0
}

const Object2:Dic = {
        village:"name1",
        age:40
}

For me it's throws the error. any one help me to understand this kind of scenarios?

user2024080
  • 1
  • 14
  • 56
  • 96
  • 1
    Does this answer your question? [Typescript Interface, enforce the type of extra properties](https://stackoverflow.com/questions/44358611/typescript-interface-enforce-the-type-of-extra-properties) – Robby Cornelissen Jul 06 '22 at 05:34
  • [Another duplicate](https://stackoverflow.com/questions/61431397/how-to-define-typescript-type-as-a-dictionary-of-strings-but-with-one-numeric-i) – Robby Cornelissen Jul 06 '22 at 05:35

1 Answers1

1

You can do age:any; or [key:string]:any;.

Unfortunately in the typescript interface the dynamic property needs to be abstract enough to contain all non-dynamic properties .

Sean AH
  • 486
  • 4
  • 8