Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

TypeScript TS7053: Element implicitly has an 'any' type

TS7053 fires when a wide key indexes a typed object. Narrow the key with keyof, declare an index signature, or guard the value before indexing.

~5 min readTypeScript
#TypeScript#Compiler Errors#Types#Debugging
A TypeScript object crate with a locked key slot, a cyan compiler gate and an amber key card beside it

Introduction & Quick Summary

text
src/theme.ts:18:24 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Theme'. Property 'primary' does not exist on type 'Theme'.

TS7053 is not a bug in your object. It is TypeScript refusing to invent a type for an access it cannot prove. Bracket access with a string key only type-checks when the target type has an index signature, is an array-like, or the key is a literal that matches a known property. With strict on, noImplicitAny turns the resulting implicit any into this error.

Three fixes cover almost every case:

  • The key really is one of the object's own keys — type the parameter as keyof T and the lookup becomes checked.
  • The object is a dictionary with arbitrary keys — declare the index signature (Record<string, T>), and expect T | undefined once noUncheckedIndexedAccess is on.
  • The key comes from user input or an API payload — keep the wide string type at the boundary and narrow it with a runtime guard before indexing.

What does not work is as any. It silences the compiler at exactly the place where an unknown key produces undefined at runtime, so the failure moves to a later line with a worse stack trace.

SymptomRoot causeCorrect fix
Key is a variable of type stringNo index signature, no literalkeyof T or a guard
Object.keys(obj), for...in, URLSearchParamsThose APIs return string by designTyped keys() helper
CSS module / JSON importDeclaration missingdeclare module or import assertion
Works in .js, breaks in .tsstrict now reports itFix the access, not the config
Access is legal but value may be missingnoUncheckedIndexedAccessExplicit undefined handling

Core Mechanics, Signatures & Reproducible Setup

A property access obj.k on a type with known properties returns the declared type of k. Bracket access obj[e] first resolves the type of e. TypeScript allows the access if the key type is assignable to keyof T, or if T declares a string/number index signature. Otherwise there is no element type to return, and under noImplicitAny the compiler reports TS7053 instead of quietly returning any.

The difference between the two spellings is deliberate:

ts
type Theme = { primary: string; surface: string }; const t: Theme = { primary: "#0af", surface: "#111" }; const a = t.primary; // string const key = "primary"; // widened to string const b = t[key]; // error TS7053 const c = t["primary"]; // string, literal key

A reproducible minimum:

json
// tsconfig.json { "compilerOptions": { "strict": true, "noUncheckedIndexedAccess": true, "target": "ES2022", "module": "NodeNext", "moduleResolution": "NodeNext", "noEmit": true } }
ts
// theme.ts type Theme = { primary: string; surface: string }; const theme: Theme = { primary: "#0af", surface: "#111" }; export function read(name: string) { return theme[name]; // TS7053 } export function readSafely(name: keyof Theme) { return theme[name]; // string }

Two variants of the message exist and they point at different mistakes. Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'X' means the key type is too wide. ...because type 'X' has no index signature appears when the object type is generic or comes from an interface without one. Both take the same three fixes.

Key Comparison Table

StrategySignatureRuntime safetyBest for
keyof parameterget<K extends keyof T>(k: K): T[K]Fully checkedKnown property sets
Index signatureRecord<string, V>Values can be undefinedDictionaries, caches
MapMap<string, V>.get() returns a value or undefinedDynamic key sets, non-string keys
Narrowing guardfunction isKey(x: string): x is keyof TChecked at the boundaryUser input, env vars
satisfies + lookupconst x = {...} satisfies Record<string, V>Keeps literal keysConfig tables
as anynoneNoneNever in application code
ConfigurationReports TS7053 on t[key]Value type after a valid index
strict: falseNoany
strict: trueYesdeclared element type
strict: true + index signatureNoV
+ noUncheckedIndexedAccessYes where uncheckedV or undefined
noPropertyAccessFromIndexSignature: trueDot access also rejectedunchanged

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

Case 1: A helper that accepts a key name from a caller

ts
function getSetting(settings: Record<string, unknown>, key: string) { return settings[key]; // fine, index signature exists } const theme = { primary: "#0af", surface: "#111" }; getSetting(theme, "primary"); // error: Theme is not assignable to Record<string, unknown>

The assignability failure has the same origin: an object literal type is not an index signature, so it is not assignable to Record<string, unknown> under excess-property strictness. Make the helper generic over the object:

ts
function getOwn<T extends object, K extends keyof T>(obj: T, key: K): T[K] { return obj[key]; } const primary = getOwn(theme, "primary"); // string // getOwn(theme, "primry"); // error: not assignable to keyof Theme

K extends keyof T keeps the return type precise per key, which the plain keyof T version loses on unions.

Case 2: Object.keys returns string[] on purpose

ts
const counts = { error: true, warn: true } as const; type Level = keyof typeof counts; Object.keys(counts).forEach((k) => { console.log(counts[k]); // TS7053 });

Object.keys is typed as string[] because JavaScript objects can carry keys TypeScript does not know about. Cast at the boundary, once, with a comment:

ts
const levels = Object.keys(counts) as Level[]; for (const level of levels) { console.log(counts[level]); // boolean }

The cast is honest only when the object is a fresh literal with no prototype keys. For objects built at runtime, filter instead:

ts
const isLevel = (k: string): k is Level => k in counts; const levels = Object.keys(counts).filter(isLevel);

Case 3: Optional properties and noUncheckedIndexedAccess

ts
type Flags = { debug?: boolean }; const flags: Flags = {}; const key: keyof Flags = "debug"; const value = flags[key]; // boolean | undefined under strict

Optional properties already include undefined, so this needs no error handling trick — but the compiler now forces a decision at the point of use. if (value) is wrong for false; compare explicitly with !== undefined.

With an index signature the same rule applies, and this is where most regressions appear after enabling noUncheckedIndexedAccess:

ts
const cache: Record<string, number> = {}; const hit = cache["a"]; // number | undefined const doubled = hit * 2; // error: possibly undefined

Case 4: Untyped third-party modules and CSS modules

ts
import styles from "./Button.module.css"; styles.primary; // TS7053 when the plugin is missing

For CSS modules, use the bundler's type support (next-env.d.ts for Next.js, vite/client for Vite). For a plain JS package without types, declare the shape rather than casting the value:

ts
declare module "legacy-config" { const config: Record<string, string>; export default config; }

An ambient declaration makes the index signature true for the compiler; confirm the package really returns an object of strings before writing it.

Case 5: Indexing a union of objects

ts
type A = { kind: "a"; a: number }; type B = { kind: "b"; b: number }; function readField(x: A | B, key: string) { return x[key]; // TS7053 }

Indexing a union with a wide key is unsafe: the key may exist on only one member. keyof (A | B) is the intersection of keys, so keyof A & keyof B — here "kind". Discriminate first, then index with a literal type per branch:

ts
function readA(x: A | B) { if (x.kind === "a") return x.a; // number return x.b; // number }

Case 6: Map when keys are genuinely dynamic

ts
const pages = new Map<string, number>(); pages.set("/about", 12); const views = pages.get("/about"); // number | undefined

A Map is the right container for keys you do not know at compile time, and it does not need noUncheckedIndexedAccess to be honest: .get always returns T | undefined.

Common Compilation/Runtime Mistakes & Version Gotchas

  • t[key as keyof typeof t] as a blanket fix. It compiles and then returns undefined for a typo. If the key is external, a guard (k in t) is the only thing that makes the cast true.
  • Turning off noImplicitAny. TS7053 disappears and every unchecked lookup becomes any, which spreads through call sites. Fix the access instead of the flag.
  • noUncheckedIndexedAccess enabled mid-project. Expect a wave of possibly undefined errors; that is the point. Fix them with explicit checks rather than non-null assertions.
  • noPropertyAccessFromIndexSignature. With this flag, obj.foo is rejected when foo comes from an index signature, forcing obj["foo"].
  • TypeScript 5.x template literal keys. Record<route-${string}, Handler> lets dynamic route strings index without error; use it instead of casting when the pattern is real.
  • satisfies keeps literals. const theme = {...} satisfies Record<string, string> preserves explicit keys, so keyof typeof theme stays narrow while the values are still checked.

When the failing value arrives as a JSON payload, pretty-print it with the JSON formatter to confirm which keys actually exist, and use the regex tester to check the runtime guard that proves a key before indexing. The diff tool is useful when a tsconfig change is the suspected trigger.

Primary references: noImplicitAny documents the flag that produces TS7053, and Indexed Access Types specifies how bracket access resolves to a type.

Official references

primary documentation

The TypeScript documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit TypeScript work — they run entirely in this browser tab, so nothing you paste leaves your device.

All TypeScript guides

More in TypeScript

All TypeScript guides

The guides filed next to this one in TypeScript, in library order.