- Error
- Reported by the type checker
TS7053
Element implicitly has an 'any' type because expression of type '{0}' can't be used to index type '{1}'.
An object is indexed with a key that its type doesn't declare, so the result would be `any`. Narrow the key or add an index signature.
What it means
Under noImplicitAny, indexing an object with an expression whose type isn't a known key (for example a general string on an object type with fixed properties) has no declared result type, so it would be implicitly any.
Example
const prices = { apple: 1, pear: 2 };
declare const fruit: string;
export const p = prices[fruit];
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(3,18): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ apple: number; pear: number; }'.
No index signature with a parameter of type 'string' was found on type '{ apple: number; pear: number; }'.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Narrow the key to the known keys:
declare const fruit: keyof typeof prices. - If any string key is valid, give the object an index signature:
const prices: Record<string, number> = { ... }. - Use a
Mapfor dynamic keys.