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?

2

1 Answer 1

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 .

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.