- Error
- Reported by the type checker
TS2538
Type '{0}' cannot be used as an index type.
A value whose type can't be a property key is used inside [ ] as an index. Use a string, number or symbol key.
What it means
Element access obj[key] and indexed access types T[K] need a key type that can name a property. Using an object type, a boolean or similar as the index is reported.
Example
const prices: Record<string, number> = {};
const item = { id: "a" };
export const p = prices[item];
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(3,25): error TS2538: Type '{ id: string; }' cannot be used as an index type.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Index with a string, number or symbol:
prices[item.id]. - Use a
Mapif you want objects as keys.