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
At a glance
- Purpose
- Static types for JavaScript
- File extension
- .ts · .tsx
- Runs in
- Compiled to JavaScript
- Usually used with
- JavaScript and its ecosystem
Specifications & further reading
Related TypeScript documentation
Interfaces
Interfaces in TypeScript define the structure of objects by specifying property names and their types. They act as contracts that ensure objects conform to specific shapes, providing type safety and better IDE support. Interfaces can be extended, merged, and used to type-check function parameters, return values, and object literals.type vs interface
Both type and interface can describe object shapes, and for that common case they are largely interchangeable. The key differences: interface supports declaration merging (multiple declarations with the same name combine automatically) and can only describe object-like shapes, while type can alias anything - unions, tuples, primitives - but cannot be re-opened once declared.Basic Types
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.
Interfaces in TypeScript define the structure of objects by specifying property names and their types. They act as contracts that ensure objects conform to specific shapes, providing type safety and better IDE support. Interfaces can be extended, merged, and used to type-check function parameters, return values, and object literals.type vs interface
Both type and interface can describe object shapes, and for that common case they are largely interchangeable. The key differences: interface supports declaration merging (multiple declarations with the same name combine automatically) and can only describe object-like shapes, while type can alias anything - unions, tuples, primitives - but cannot be re-opened once declared.Basic Types
TypeScript extends JavaScript with static types, letting the compiler catch type errors before code ever runs. Beyond the familiar string, number, and boolean, TypeScript adds any (disables checking entirely), unknown (a safer any that requires narrowing), void (a function returning nothing), and never (a value that can never occur, like a function that always throws).Type Aliases
The type keyword creates a named alias for any type - not just object shapes like interface, but also unions, primitives, tuples, and function signatures. Type aliases make complex types reusable and give them a meaningful name, improving both readability and error messages.