What are index signatures in TypeScript?
Why Interviewers Ask This
This question targets practical, hands-on experience with TypeScript. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.
Answer
Index signatures describe types that can have any number of properties with a consistent key and value type. Syntax: interface StringMap { [key: string]: string; } — any string key maps to a string value. Numeric index: interface NumberArray { [index: number]: string; }. Index signatures can be combined with specific properties, but specific property types must be assignable to the index signature value type: interface Config { [key: string]: string | number; version: number; }. Access: const val = map["anyKey"] — TypeScript knows val is string. noUncheckedIndexedAccess option in tsconfig makes TypeScript include undefined in the type of indexed access (since the key might not exist): val becomes string | undefined — safer but more verbose. Prefer Map for runtime key-value storage; use index signatures for describing existing JavaScript patterns. Record<string, V> is a cleaner alternative for typed dictionaries.
Pro Tip
Back up your answer with a specific project or situation. Saying 'In my last TypeScript project, I used this when...' immediately makes your answer more credible and memorable.