Skip to content

Index signature missing for dynamic property access

Quick summary

You used obj[dynamicKey] but the type of obj has no index signature — TypeScript cannot guarantee the key exists.

Why this happens

  • The object type is a specific interface without an index signature, and you access it with a variable key

  • You iterate over keys with Object.keys() and try to access the object with each key

  • A Record type was expected but a specific object type was provided, lacking the index signature

Minimal example

✗ Broken code
const config = { host: 'localhost', port: 8080 };
const key = 'host';
console.log(config[key]); // error: no index signature
✓ Fixed code
const config = { host: 'localhost', port: 8080 };
const key = 'host' as keyof typeof config;
console.log(config[key]); // safe: config[key] is string | number

Casting the key to keyof typeof config tells TypeScript the key is one of the known property names, allowing safe access.

How to diagnose

  • Read the TS error: 'Element implicitly has an any type because type has no index signature'

  • Check whether the object type should use Record<string, T> instead of a specific interface

  • Use keyof typeof obj to get the known keys and check if the dynamic key is one of them

How to fix

  • Add an index signature to the type: { [key: string]: ValueType; knownProp: string }

  • Cast the key to keyof typeof obj: obj[key as keyof typeof obj]

  • Use Record<string, ValueType> if all values have the same type and dynamic access is needed

How to prevent

  • Use Record<string, T> for objects that need dynamic string key access

  • Type Object.keys() results as (keyof typeof obj)[] and use type-safe key access

Related Resources

Related Glossary

Related Lessons

← Back to language