Skip to content

Index Signatures in TypeScript

// String index signature
interface StringDict {
[key: string]: string;
}
const translations: StringDict = {
hello: "Hola",
goodbye: "Adiós",
};
interface NumberDict {
[index: number]: string;
}
const arr: NumberDict = ["a", "b", "c"];
console.log(arr[0]); // "a"
interface MixedDict {
[index: string]: number | string;
length: number; // OK: number is in the union
name: string; // OK: string is in the union
}
interface ReadonlyDict {
readonly [key: string]: number;
}
const scores: ReadonlyDict = { alice: 95, bob: 87 };
// scores.alice = 100; // ❌ Error: Index signature is readonly
// Form errors dictionary
interface FormErrors {
[field: string]: string | undefined;
}
// API headers
interface Headers {
[header: string]: string | string[] | undefined;
"Content-Type": string;
Authorization?: string;
}

Easy: What does [key: string]: string mean?

Medium: What are the restrictions on index signature parameter types?

Hard: How would you type a dictionary that maps string keys to specific value types?