TypeScript error TS7053 occurs when you use a dynamic string key to access an object that has no index signature. TypeScript cannot guarantee that an arbitrary string key exists on the object, so it requires either a typed key (keyof T) or an explicit index signature.
Why it happens
- •Accessing an object property with a string variable instead of a literal key.
- •Using a computed property key without constraining its type to keyof T.
- •Iterating over Object.keys() results, which are typed as string[].
Examples
Code that triggers the error
interface Config {
host: string;
port: number;
}
const config: Config = { host: "localhost", port: 3000 };
const key = "host";
const value = config[key]; // dynamic accessError output
error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Config'.
How to fix it
Constrain the key type to keyof Config. Use Record<string, unknown> for objects that truly have arbitrary string keys. Cast with (obj as Record<string, unknown>)[key] when you need a quick escape hatch but add a runtime check.
Fixed code
interface Config {
host: string;
port: number;
}
const config: Config = { host: "localhost", port: 3000 };
const key: keyof Config = "host";
const value = config[key]; // typed correctlyRelated errors
Ready to practice your English at work?
Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.
Try Lingua-e for free