Skip to main content
TypeScript Bug Hunt Medium +20 XP

Index Signature Error

This object access gives a TypeScript error.

TypeScriptFind the bug
1const config = { host: 'localhost' };
2const key = 'host';
3console.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.

Related challenges