Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript says a string key cannot index your object

ErrorElement 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.

  • Applies to TypeScript 5.x, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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.json
  2. 2Match what you see, then jump to its fix

    1. 1const errors = {} or useState({}), then errors[name] failsThe object was created empty, so its type has no keysFix
    2. 2const 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. 3The 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

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 -r

2Fix

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 --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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 -r

2Fix

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 --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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 -r

2Fix

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 --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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:

Related fixes

Web 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.