Syntax
interface Name {
[key: string]: ValueType;
}Examples
A Basic Index Signature
Describing an object where keys are unknown but values share a common type.
interface StringDictionary {
[key: string]: string;
}
const translations: StringDictionary = {
hello: "Hola",
goodbye: "Adiós"
// any number of string keys are allowed, all values must be strings
};
console.log(translations.hello); // "Hola"
console.log(translations["goodbye"]); // "Adiós"Combining Known and Dynamic Properties
An interface can mix specific required properties with an index signature for the rest.
interface Config {
name: string; // a specific, required property
[key: string]: string | number; // plus any number of other string or number properties
}
const settings: Config = {
name: "MyApp",
version: 2,
environment: "production"
};Best practices
- Use index signatures for genuinely dynamic-key data, like a dictionary or lookup table - not as a shortcut to avoid defining specific properties
- Consider Record<KeyType, ValueType> as a more concise alternative to an index signature for simple dictionary types
- Remember all specifically named properties must be compatible with the index signature's value type when both are combined
- Prefer a Map over an index-signature object when keys are added/removed frequently, or when you need reliable iteration and size tracking
Runtime & versions
Compiled to JavaScript. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
TypeScript Handbook