Home > Net >  How to write generic data types with typescript for string
How to write generic data types with typescript for string

Time:07-06

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?

CodePudding user response:

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 .

  • Related