Fixing TS7053 when indexing an object with a string key in TypeScript
In TypeScript, if you read an object property with a variable of type string, such as labels[status], you get error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type .... Changing the key type to keyof typeof labels so the function accepts only the keys the object has makes the error go away. This error shows up in code that uses an object like a dictionary: a lookup table that turns a status name into a display string, or a loop that takes keys from Object.keys and reads the values.
I reproduced this error with both tsc and the TypeScript language server, and confirmed the full message and that the fix removes it.
The test environment was Ubuntu 24.04, Node.js 22.23.4, and TypeScript 7.0.2. For comparison, I also ran tsc on the same files with TypeScript 5.9.3. The tsconfig.json was generated with npx tsc --init (it includes "strict": true and "noUncheckedIndexedAccess": true). The editor errors were checked by running the TypeScript 7 language server directly (tsc --lsp --stdio) and talking to it over LSP (Language Server Protocol). I did not test how the errors look inside an editor UI itself.
The full error message
In src/bad.ts, I wrote a function that takes a status (draft or published) and returns a Japanese display name.
const labels = {
draft: "下書き",
published: "公開",
};
export function getLabel(status: string) {
return labels[status];
}
console.log(getLabel("draft"));
Running npx tsc --noEmit produced the following (exit code 1):
src/bad.ts(7,10): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ draft: string; published: string; }'.
No index signature with a parameter of type 'string' was found on type '{ draft: string; published: string; }'.
The message has two lines. The first says that an expression of type string cannot be used to index the type { draft: string; published: string; }, so the type of the value read is implicitly any. The second says that this type has no index signature that accepts a string key. With --pretty, the whole labels[status] expression was underlined (I removed the color control characters):
src/bad.ts:7:10 - error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ draft: string; published: string; }'.
No index signature with a parameter of type 'string' was found on type '{ draft: string; published: string; }'.
7 return labels[status];
~~~~~~~~~~~~~~
Found 1 error in src/bad.ts:7
With tsc from TypeScript 5.9.3, the same error with the same text appeared at the same position (exit code 2).
The same error appears in the language server
tsc from TypeScript 7 runs as a language server when you pass --lsp --stdio. I had this language server open src/bad.ts using the LSP steps (initialize, then didOpen). A small script I wrote printed the diagnostics it returned, along with the hover (type information) at getLabel on line 6:
server: typescript-go 7.0.2
diagnostics (1):
7:10-7:24 severity=1 source=ts code=7053 Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ draft: string; published: string; }'.
No index signature with a parameter of type 'string' was found on type '{ draft: string; published: string; }'.
hover 6:17: function getLabel(status: string): any
severity=1 is the value that the LSP specification uses for errors. The message matches the two lines from tsc, and the return type of getLabel is any.
Why reading with a string key is an error
The type of labels is determined from its contents as { draft: string; published: string; }. The only keys this type has are draft and published. Because status is of type string, a key that is not in labels, such as "archived", could be passed in. TypeScript therefore cannot determine what labels[status] can read, so it makes the value type any.
The error is reported by the noImplicitAny setting (English), which is included in strict. In the test environment, running tsc with --noImplicitAny false produced no error. However, that only hides the error. It also means you would miss code that reads keys which are not in labels.
Even with the error, tsc still emitted JavaScript. Running the emitted JavaScript with Node.js printed 下書き. TS7053 is not an error about code that cannot run. It marks a place where the type checker stops checking.
Narrowing the key type with keyof typeof
If only keys from labels should ever be passed in as status, change the parameter type to keyof typeof labels:
-export function getLabel(status: string) {
+export function getLabel(status: keyof typeof labels) {
When used in a type position, typeof labels refers to the type of the variable labels (the handbook's explanation of the typeof type operator (English)). keyof creates a type that lists the key names of an object type (the handbook's explanation of the keyof type operator (English)). Combined, they give a type that represents only the keys of labels.
Running npx tsc --noEmit on the fixed file produced no output and exited with code 0. The language server also reported zero diagnostics. On hover, the parameter became (parameter) status: "draft" | "published", and the function became function getLabel(status: keyof typeof labels): string. The return type changed from any to string. With TypeScript 5.9.3, the fixed file also produced no errors.
With this change, calls that pass a key that does not exist become errors. In src/fix-keyof-call.ts, which is the fixed file with line 10 changed to getLabel("archived"), I got this error:
src/fix-keyof-call.ts(10,22): error TS2345: Argument of type '"archived"' is not assignable to parameter of type '"draft" | "published"'.
Check strings from outside with a type guard before reading
Strings whose content you cannot know until runtime, such as URL query parameters or values in JSON returned by an API, end up typed as string. In that case, write a function that checks whether the string is a key of labels, and read the value only after that check.
const labels = {
draft: "下書き",
published: "公開",
};
type Status = keyof typeof labels;
function isStatus(value: string): value is Status {
return Object.hasOwn(labels, value);
}
export function getLabel(status: string) {
if (isStatus(status)) {
return labels[status];
}
return "不明";
}
console.log(getLabel("draft"));
console.log(getLabel("archived"));
console.log(getLabel("toString"));
The return type value is Status is the form the handbook calls a type predicate (English). When the function returns true, it tells TypeScript that value is of type Status. tsc reported no error, and hovering over status on line 14 showed (parameter) status: "draft" | "published". After compiling to JavaScript and running it, the output was:
下書き
不明
不明
To check whether a key exists, I used Object.hasOwn() (MDN). The in operator also returns true for properties an object inherits through its prototype. In the test environment, I built a labels object with the same contents as the article in Node.js and tried it: "toString" in labels was true, and Object.hasOwn(labels, "toString") was false. Using in as the check would let toString through as a lookup table key.
if (status in labels) alone does not remove the error
When I wrote if (status in labels) { return labels[status]; } without a type guard function, the same TS7053 still appeared inside the if block. On hover, status inside the if was still (parameter) status: string. With TypeScript 7.0.2, using in did not narrow the key type to the keys of labels.
Use Record for an object that accepts any key
For an object used as a dictionary with no fixed set of keys, annotate the variable as Record<string, string>. As the utility types documentation (English) describes, this is an object type with string keys and string values.
const labels: Record<string, string> = {
draft: "下書き",
published: "公開",
};
With this type, reading with a string key does not produce TS7053. However, the hover for the function showed function getLabel(status: string): string | undefined. This is because noUncheckedIndexedAccess (English), which is in the config generated by tsc --init, adds undefined to the type of a value read with a key that was not declared. Actually calling getLabel("archived") returned undefined. Calling toUpperCase() directly on the read value, as in labels[status].toUpperCase(), produced error TS2532: Object is possibly 'undefined'. The error went away after checking for undefined before using the value:
export function getLabel(status: string) {
const label = labels[status];
if (label === undefined) {
return "不明";
}
return label.toUpperCase();
}
getLabel("draft") returned 下書き, and getLabel("archived") returned 不明. (toUpperCase() does not change Japanese characters, so 下書き stays the same.)
If the set of keys in labels is fixed, keyof typeof or a type guard, as described above, catches wrong keys at compile time, which is better than Record<string, string>.
Writing labels[status as keyof typeof labels] with an as cast also removes TS7053. But this asserts that the key is valid without checking it. The return type still shows string on hover, yet calling getLabel("archived") returned undefined at runtime. For strings from outside your code, use a type guard.
When the error comes from a loop over Object.keys, use Object.entries
Reading the same object's values with keys taken from Object.keys produces the same TS7053:
const stock = {
apple: 3,
banana: 5,
};
for (const name of Object.keys(stock)) {
console.log(name, stock[name]);
}
src/keys-bad.ts(7,21): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ apple: number; banana: number; }'.
No index signature with a parameter of type 'string' was found on type '{ apple: number; banana: number; }'.
The hover for name was const name: string. The type declaration in TypeScript 7.0.2 (lib.es5.d.ts) declares Object.keys as keys(o: object): string[];, so it returns an array of string, not "apple" | "banana". The for (const name in stock) form produced the same error at the same position.
If you need both the keys and the values, use Object.entries() (MDN) to get key-value pairs:
for (const [name, count] of Object.entries(stock)) {
console.log(name, count);
}
This file produced no tsc errors, and the hover for count was const count: number. After compiling to JavaScript and running it, the output was two lines: apple 3 and banana 5.





