How do you create and use index signatures in TypeScript?
Instruction: Explain what index signatures are and demonstrate their use with an example.
Context: This question evaluates the candidate's understanding of index signatures in TypeScript and their ability to apply them in dynamic property scenarios.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
An index signature describes values associated with dynamic property keys. It does not guarantee that every possible key is present. Known fields must also be compatible with a string index signature's value type.
interface UserProfile { [key: string]: string | number | undefined; } const profile: UserProfile = { name: "Ada", age: 30 }; function ownAttribute(object: UserProfile, key: string):...