TypeScript Compiler Diagnostics

TS7053: Unconstrained key cannot index object

A general string could be a property that the object does not contain. For a fixed-shape object, constrain the key to keyof its type. If keys really are arbitrary, model a dictionary with an appropriate index signature and handle absent entries. Do not silence the error using any. Baseline: TypeScript 5.8.3 with the shown configuration. Reproduce in one directory and create the corrected project in another. The diagnostic can have other causes; inspect its location and message before choosing a correction. Both projects keep strict checking enabled.

Formal Syntax & Structure

typescript syntaxtypescript
TS7053 (compiler diagnostic code, not TypeScript syntax)

Working Example

Example Snippettypescript
// INTENTIONALLY INVALID — reproduce// File: tsconfig.json (omit this label){  "compilerOptions": {    "strict": true,    "target": "ES2022",    "module": "CommonJS",    "types": [],    "lib": [      "ES2022",      "DOM"    ],    "noEmitOnError": true,    "outDir": "dist"  },  "files": [    "main.ts"  ]}
// File: main.ts (omit this label)const prices = { tea: 3, coffee: 5 };function price(key: string) { return prices[key]; }
// Run: tsc -p . --pretty false// Expected: TS7053; no output emitted.
// CORRECTED PROJECT — separate directory// File: tsconfig.json (omit this label){  "compilerOptions": {    "strict": true,    "target": "ES2022",    "module": "CommonJS",    "types": [],    "lib": [      "ES2022",      "DOM"    ],    "noEmitOnError": true,    "outDir": "dist"  },  "files": [    "main.ts"  ]}
// File: main.ts (omit this label)const prices = { tea: 3, coffee: 5 };function price(key: keyof typeof prices) { return prices[key]; }console.log(price("tea"), price("coffee"));
// Run: tsc -p . --pretty false// Expected: no diagnostics.// Run: node dist/main.js// Expected stdout: 3 5
TypeScript Reference