Q60: What are Index Signatures and how do they handle arbitrary dynamic keys?
Index Signatures allow defining types for objects that accept dynamic, un-named keys using bracket notation: [key: KeyType]: ValueType.
Dynamic String Index Signature
interface StringDictionary {
[key: string]: string;
name: string; // Valid
// age: number; // Error! Property 'age' of type 'number' is not assignable to string index type 'string'.
}💡 All explicit properties must match the return value type declared in the index signature.
Key Takeaways
- Index signatures allow typing dynamic key-value dictionary objects.