Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to write generic data types with typescript for string

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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 .

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading