TypeScript Bug Hunt Medium +20 XP
Index Signature Error
This object access gives a TypeScript error.
TypeScriptFind the bug
1
const config = { host: 'localhost' };2
const key = 'host';3
console.log(config[key]); The bug is on line 3
Select the correct fix
Show answer and explanation
Answer: Add index signature to type or use keyof typeof
TypeScript doesn't allow arbitrary string indexing without an index signature. Use (config as any)[key] or type the key properly with keyof typeof config.
Hint: TypeScript can't verify that 'key' is a valid property name.
What this challenge tests
Bug Hunt in TypeScript: index-signatures, types.