Index Signatures
An index signature states the type of properties accessed by a dynamic key, written [key: string]: V or [key: number]: V. Known properties must be assignable to that V.
Detailed Explanation
When you do not know the key names in advance, an object type still needs a rule for obj[key]. An index signature is that rule.
type Headers = { [name: string]: string };
Any string key is allowed. The value must be a string. headers["Accept"] is string (or string | undefined with noUncheckedIndexedAccess). A known property on the same type, contentType: string, is fine. A known property count: number is an error, because count is also reachable through the string index and number is not assignable to string.
That last rule is the one that blocks mixed dictionaries. If you need some keys typed specially and others open, the open values have to be a union that includes the special ones: { id: string; [key: string]: string | undefined } or, often cleaner, do not mix them — keep id on a wrapper and the bag in a field.
Number index signatures describe array-like objects. [index: number]: T is what Array<T> uses. A string index is more general, because JavaScript coerces numeric keys to strings; if both are present, the number value type must be assignable to the string value type.
Template literal index signatures ([key: header-${string}]: string) let you constrain the key grammar. Symbol index signatures exist for symbol keys.
Record<string, V> is an index signature. An interface with only an index signature is a dictionary. Adding an index signature to a previously closed interface is a breaking change: extra keys become allowed, and listed properties have to match V.
Index signatures and keyof interact badly for autocomplete. keyof { [k: string]: V; id: string } is string | number, which swallows "id". Prefer a closed Record<SpecificUnion, V> when the key set is known, and an index signature when it is not.
At runtime there is no index signature. Extra keys, missing keys, and prototype keys behave as JavaScript always has. The signature is a claim about what you intend to read and write.
Code Examples
1. A string dictionary, and a conflicting known property.
// 1. A string dictionary, and a conflicting known property.
type Counts = { [word: string]: number };
const counts: Counts = { ada: 3, grace: 1 };
counts["ada"] += 1;
// type Bad = { [word: string]: number; label: string };
// Error: 'label' is incompatible with the string index type 'number'.2. Known keys plus an open bag via a union value.
// 2. Known keys plus an open bag via a union value.
type Row = {
id: string;
[column: string]: string | undefined;
};
const row: Row = { id: "u1", email: "ada@example.com" };3. Template-literal keys.
// 3. Template-literal keys.
type CssVars = { [name: `--${string}`]: string };
const vars: CssVars = { "--brand": "#111" };
// vars.brand = "#111";
// Error: property 'brand' does not match the index signature.Common Gotchas
Every listed property must be assignable to the index value type
a mixed object needs a union value type or a nested bag field.
`keyof` on a string-indexed type collapses to `string`
you lose a useful union of known keys. Keep closed keys on a separate type.
Reads may still be `undefined` at runtime
turn on `noUncheckedIndexedAccess` if you want the type to say so.
Adding an index signature to a public type allows extra keys
excess-property checks on literals get weaker. That can hide typos.