index signature
A type rule such as { [key: string]: string } for objects whose property names are not known in advance.
In more detail
It fits dictionaries such as HTTP headers, query parameters and metadata. Every explicitly listed property must also fit the index signature's value type. Combine it with noUncheckedIndexedAccess so lookups return value | undefined.
Example
metadata: { [key: string]: string };Related terms
- Record — A built-in TypeScript type, Record<K, V>, for an object whose keys are of type K and values of type V.
- noUncheckedIndexedAccess — The compiler option that adds undefined to the type of every array index or dictionary lookup, because the entry might not exist.
- mapped type — A type that builds a new object type by looping over a set of keys, such as { [K in keyof T]: T[K] }.
Taught in
- Type Aliases Versus Interfaces: Declaration Merging — Production TypeScript
- Optional Properties, Readonly And Index Signatures — Production TypeScript