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.

Introduction & Quick Summary
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 Tand the lookup becomes checked. - The object is a dictionary with arbitrary keys — declare the index signature (
Record<string, T>), and expectT | undefinedoncenoUncheckedIndexedAccessis on. - The key comes from user input or an API payload — keep the wide
stringtype 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.
| Symptom | Root cause | Correct fix |
|---|---|---|
Key is a variable of type string | No index signature, no literal | keyof T or a guard |
Object.keys(obj), for...in, URLSearchParams | Those APIs return string by design | Typed keys() helper |
| CSS module / JSON import | Declaration missing | declare module or import assertion |
Works in .js, breaks in .ts | strict now reports it | Fix the access, not the config |
| Access is legal but value may be missing | noUncheckedIndexedAccess | Explicit 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:
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 keyA reproducible minimum:
// tsconfig.json
{
"compilerOptions": {
"strict": true,
"noUncheckedIndexedAccess": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"noEmit": true
}
}// 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
| Strategy | Signature | Runtime safety | Best for |
|---|---|---|---|
keyof parameter | get<K extends keyof T>(k: K): T[K] | Fully checked | Known property sets |
| Index signature | Record<string, V> | Values can be undefined | Dictionaries, caches |
Map | Map<string, V> | .get() returns a value or undefined | Dynamic key sets, non-string keys |
| Narrowing guard | function isKey(x: string): x is keyof T | Checked at the boundary | User input, env vars |
satisfies + lookup | const x = {...} satisfies Record<string, V> | Keeps literal keys | Config tables |
as any | none | None | Never in application code |
| Configuration | Reports TS7053 on t[key] | Value type after a valid index |
|---|---|---|
strict: false | No | any |
strict: true | Yes | declared element type |
strict: true + index signature | No | V |
+ noUncheckedIndexedAccess | Yes where unchecked | V or undefined |
noPropertyAccessFromIndexSignature: true | Dot access also rejected | unchanged |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
Case 1: A helper that accepts a key name from a caller
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:
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 ThemeK 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
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:
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:
const isLevel = (k: string): k is Level => k in counts;
const levels = Object.keys(counts).filter(isLevel);Case 3: Optional properties and noUncheckedIndexedAccess
type Flags = { debug?: boolean };
const flags: Flags = {};
const key: keyof Flags = "debug";
const value = flags[key]; // boolean | undefined under strictOptional 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:
const cache: Record<string, number> = {};
const hit = cache["a"]; // number | undefined
const doubled = hit * 2; // error: possibly undefinedCase 4: Untyped third-party modules and CSS modules
import styles from "./Button.module.css";
styles.primary; // TS7053 when the plugin is missingFor 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:
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
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:
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
const pages = new Map<string, number>();
pages.set("/about", 12);
const views = pages.get("/about"); // number | undefinedA 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 returnsundefinedfor 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 becomesany, which spreads through call sites. Fix the access instead of the flag. noUncheckedIndexedAccessenabled mid-project. Expect a wave ofpossibly undefinederrors; that is the point. Fix them with explicit checks rather than non-null assertions.noPropertyAccessFromIndexSignature. With this flag,obj.foois rejected whenfoocomes from an index signature, forcingobj["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. satisfieskeeps literals.const theme = {...} satisfies Record<string, string>preserves explicit keys, sokeyof typeof themestays 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 documentationThe TypeScript documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit TypeScript work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related TypeScript guides
All TypeScript guidesTypeScript TS2349: This expression is not callable
TS2349 means the value has no call signature at that point. Narrow unions, fix module default imports and guard optional calls instead of asserting.
TypeScript: Cannot find name 'process' with @types/node
TS2304 on process or Buffer means the compiler has no Node declarations. Install @types/node, stop excluding it, and align its version to the runtime.
TypeScript TS2580: Cannot find name 'require'
TS2580 means no Node types are loaded. Install @types/node, set the types option, and check module mode, tsconfig inheritance and hoisting.
TypeScript TS2532: Object is possibly 'undefined'
TS2532 means strict null checks cannot prove a value exists before reading through it. Guard it, add optional chaining, or narrow the type at the source.
More in TypeScript
All TypeScript guidesThe guides filed next to this one in TypeScript, in library order.