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.
Official answer available
Preview the opening of the answer, then unlock the full walkthrough.
To clarify, an index signature is defined in an object type to specify the types of possible values that can be stored using dynamic keys. It is akin to declaring a contract for the object, stating that it can accept keys of a specific type (usually string or number) and will return values of a certain type.
For instance, let's consider a scenario where we're developing an application that dynamically fetches user profiles, and each profile has different attributes that are not known at compile time. Here, index signatures come to the rescue....