TypeScript says a string key cannot index your object
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.Short answer
TS7053 appears under noImplicitAny when you read obj[key] and the object's type has no property or index signature that matches the key's type. Give the object a real type: an index signature or Record for dynamic keys, or type the key as keyof the object for known keys.
Fix it fast
1Find out which cause you have
Find the object in the failing line and look at its declared type. If it was created as {} (a plain empty literal or useState({})), that is cause one. If it has named fields and the key is just 'string', that is cause two.
npx tsc --noEmit grep -n "noImplicitAny\|\"strict\"" tsconfig.json2Match what you see, then jump to its fix
- 1
const errors = {} or useState({}), then errors[name] failsThe object was created empty, so its type has no keysFix - 2
const labels = { a: 'A', b: 'B' } and labels[props.kind] where kind is stringThe object has known fields, but the key is typed as plain stringFix - 3
The keys are a known set (for example roles or statuses) and a key is missing on one objectA fixed list of keys should be enforced on the objectFix
- 1
What does the error look like?
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.TS7053 on a line like errors[name] or obj[key]
Cause 1 of 3
The object was created empty, so its type has no keys
1Check
Hover the variable. An empty literal gets the type {}, which has no properties at all, so no string key can be valid.
grep -n "= {}\|useState({})" src -r2Fix
Declare what the keys and values are. An index signature says any string key is allowed and what its value looks like.
const [errors, setErrors] = useState<{ [field: string]: string }>({});
function show(name: string) {
return errors[name];
}3Verify
The error is gone. Reading a key that is not set now gives a string type, so check the value at runtime before you rely on it.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 2 of 3
The object has known fields, but the key is typed as plain string
1Check
A string can be any text, so TypeScript cannot know it is one of the real keys. The key type is the problem, not the object.
grep -n "keyof" src -r2Fix
Narrow the key to the union of the real keys with keyof, or declare the prop with that union.
const labels = { a: "A", b: "B" };
function label(kind: keyof typeof labels) {
return labels[kind];
}3Verify
Compile. Calling label('c') is now reported where you wrote it, not at runtime.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 3 of 3
A fixed list of keys should be enforced on the object
1Check
If every key in a union must have a value, say so with Record, so a missing entry fails at the object and not later.
grep -n "type .* = \"" src -r2Fix
Type the object with Record<Keys, Type>. Index it with a value of the Keys type.
type Status = "draft" | "live";
const colours: Record<Status, string> = {
draft: "grey",
live: "green",
};
const c = colours["live"];3Verify
Remove one entry from the object and compile: the error now points at the object.
npx tsc --noEmitConfirmed by typescriptlang.org
How do I stop it coming back?
- Type objects at the point you create them instead of leaving them as {}.
- Keep noImplicitAny on (it is part of strict) and fix the errors, rather than casting to any.
- Prefer a union of literal keys over string for any prop that selects an entry.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fixWeb apps
ESLint reports no-unused-vars for React components and imports that are used
no-unused-vars reported for a component that is rendered as a JSX tag
ESLint's core no-unused-vars does not see variables used only as JSX tags, so a component imported and rendered as <Hello /> looks unused.
3 causes, each with a fixWeb apps
TypeScript rejects MUI Data Grid columns that set a type
A TypeScript error inside a column's renderCell callback
A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.
2 causes, each with a fixWeb apps
The renderInput prop stopped working on the MUI X DatePicker
The custom TextField (variant, size, helperText) is ignored
renderInput was removed from the MUI X date pickers in version 6.
3 causes, each with a fix