Index Signatures in Interfaces
You'll learn: How to allow arbitrary string or number keys in interfaces with a specific value type constraint.
The Core Idea
You've already seen index signatures in object types. Interfaces support the exact same index signature syntax, letting you define objects that can have any number of keys (as long as they match the signature type).
This is perfect for dictionary-like structures where you don't know all the property names upfront, but you know what type of values they should hold.
The Syntax in Interfaces
interface StringDictionary {
[key: string]: string;
}
const userRoles: StringDictionary = {
alice: "admin",
bob: "editor",
charlie: "viewer"
};
// Any string key is allowed
const role = userRoles.newUser; // Type: string
Just like with object types, the index signature says "any string key is allowed, and the value must be a string."
Combining Known Properties with Index Signatures
interface Config {
name: string;
version: number;
[key: string]: string | number; // Index signature must include known property types
}
const appConfig: Config = {
name: "MyApp",
version: 1,
apiUrl: "https://api.example.com",
timeout: 3000
};
The index signature must be compatible with all known properties in the interface—that's why it's string | number here.
Numeric Index Signatures
interface NumberArray {
[index: number]: string;
}
const items: NumberArray = ["apple", "banana", "cherry"];
const first = items[0]; // Type: string
This works for array-like objects where numeric indices make sense.
Key Takeaway: Index signatures in interfaces work identically to object types—they allow arbitrary keys with type constraints, and any known properties must be compatible with the signature.