Index Signatures
TS · Interfacessyntax
interface Name {
[key: string]: Type;
[index: number]: Type;
}example
interface TranslationMap {
[locale: string]: string;
}
const greetings: TranslationMap = {
en: "Hello",
es: "Hola",
ja: "こんにちは",
};
// Mixed: known + dynamic keys
interface AppConfig {
appName: string;
version: string;
[key: string]: string; // all values must be string
}output
// greetings["fr"] is string (even if missing at runtime)Note Index signatures allow any key of the given type. When combined with named properties, all named property types must be compatible with the index signature type. Enable noUncheckedIndexedAccess in tsconfig to get T | undefined instead of just T for indexed access — much safer.