- Error
- Reported by the type checker
TS7015
Element implicitly has an 'any' type because index expression is not of type 'number'.
An array is indexed with a value that isn't a number, so the element type is `any`. Index with a number, or use an object or Map.
What it means
Under noImplicitAny, indexing an array or tuple with a key that isn't of type number (for example a string) can't find an element type, so the result would be an implicit any.
Example
const names = ["a", "b"];
declare const key: string;
export const n = names[key];
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(3,24): error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'.
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
- Convert the key:
names[Number(key)]. - If you meant a lookup by name, use an object type with an index signature or a
Map.