TypeScriptのobj[key]で出るTS7053の直し方
TypeScript で、オブジェクトのプロパティを labels[status] のように string 型の変数で読むと、error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type ... が出ます。キーの型を keyof typeof labels にして、オブジェクトにあるキーだけを受け取るようにすると、エラーは消えます。状態の名前から画面に出す文字を引く対応表や、Object.keys でキーを取り出して値を読むループなど、オブジェクトを辞書のように使うコードで出るエラーです。
私は検証環境で tsc と TypeScript の言語サーバーの両方でこのエラーを起こし、メッセージの全文と、直すと消えることを確かめました。
検証環境は Ubuntu 24.04、Node.js 22.23.4、TypeScript 7.0.2 です。比べるために TypeScript 5.9.3 でも同じファイルを tsc にかけました。tsconfig.json は npx tsc --init で作ったもの("strict": true と "noUncheckedIndexedAccess": true が入っています)です。VS Code などのエディターに出るエラーは、エディター本体ではなく、エディターと LSP(Language Server Protocol)でやりとりする TypeScript 7 の言語サーバー(tsc --lsp --stdio)を直接動かして確かめました。エディターの画面での見え方は試していません。
エラーの全文
src/bad.ts に、記事の状態(draft や published)から、画面に出す日本語の名前を返す関数を書きました。
const labels = {
draft: "下書き",
published: "公開",
};
export function getLabel(status: string) {
return labels[status];
}
console.log(getLabel("draft"));
npx tsc --noEmit を実行すると、次のように出ました(終了コードは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; }'.
メッセージは2行です。1行目は「string 型の式は { draft: string; published: string; } 型のキーには使えないので、読んだ値の型は暗黙のうちに any になります」という意味です。2行目は「この型には、string 型のキーを受け付けるインデックスシグネチャがありません」という意味です。--pretty を付けると、labels[status] の全体に波線が付きました(色を付けるための制御文字は消しています)。
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
TypeScript 5.9.3 の tsc でも、同じ位置に同じ文面のエラーが出ました(終了コードは2)。
言語サーバーでも同じエラーが出る
TypeScript 7 の tsc は、--lsp --stdio を付けると言語サーバーとして動きます。この言語サーバーに、LSP の手順(initialize → didOpen)で src/bad.ts を開かせました。返ってきた診断と、6行目の getLabel の位置のホバー(型の情報)を、私が書いた小さなスクリプトで表示すると、次のとおりです。
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 は、LSP の仕様でエラーを表す値です。メッセージは tsc と同じ2行で、getLabel の戻り値の型は any になっています。
なぜ string のキーで読むとエラーになるのか
labels の型は、書いた中身から { draft: string; published: string; } に決まります。この型が持っているキーは draft と published の2つだけです。status は string 型なので、"archived" のような、labels にないキーが入ることもあります。そのため TypeScript は、labels[status] で何が読めるのかを決められず、値の型を any にします。
any になってしまう場所をエラーにしているのは、strict に含まれる noImplicitAny の設定(英語)です。検証環境でも、--noImplicitAny false を付けて tsc を実行すると、エラーは出ませんでした。ただ、これはエラーを表示しなくなるだけで、labels にないキーを読むコードを見逃すことになります。
エラーが出ていても、tsc は JavaScript を出力しました。出力された JavaScript を Node.js で動かすと 下書き と出ます。TS7053 は、実行できないコードを示すエラーではなく、型のチェックが働かない場所を示すエラーです。
キーの型を keyof typeof で絞る
status に labels のキーしか入らないなら、引数の型を keyof typeof labels にします。
-export function getLabel(status: string) {
+export function getLabel(status: keyof typeof labels) {
typeof labels は、型を書く場所で使うと、変数 labels の型を表します(ハンドブックの typeof 型演算子の説明(英語))。keyof は、オブジェクトの型からキーの名前を並べた型を作ります(ハンドブックの keyof 型演算子の説明(英語))。2つを合わせると、labels のキーだけを表す型になります。
直したファイルを npx tsc --noEmit にかけると、何も出ずに終了コード0で終わりました。言語サーバーの診断も0件です。ホバーで見た型は、引数が (parameter) status: "draft" | "published"、関数が function getLabel(status: keyof typeof labels): string に変わりました。戻り値が any から string になっています。TypeScript 5.9.3 でも、直したファイルではエラーは出ませんでした。
こうしておくと、ないキーを渡す呼び出しがエラーになります。直したファイルの10行目を getLabel("archived") に変えたもの(src/fix-keyof-call.ts)では、次のエラーが出ました。
src/fix-keyof-call.ts(10,22): error TS2345: Argument of type '"archived"' is not assignable to parameter of type '"draft" | "published"'.
外から来る文字列は、型ガードで確かめてから読む
URL のクエリーパラメーターや、API が返した JSON の値のように、中身が実行するまで分からない文字列は、string 型で受け取ることになります。この場合は、labels のキーかどうかを確かめる関数を書き、確かめたあとで読みます。
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"));
戻り値の型に書いた value is Status は、ハンドブックで type predicate と呼ばれている書き方(英語)です。この関数が true を返したら、value は Status 型だと TypeScript に伝えます。tsc のエラーは出ず、14行目の status のホバーは (parameter) status: "draft" | "published" でした。JavaScript にして動かすと、次のように出ました。
下書き
不明
不明
キーがあるかどうかは、Object.hasOwn()(MDN)で確かめています。in 演算子は、オブジェクトがプロトタイプから受け継いだプロパティでも true を返します。検証環境で、記事と同じ中身の labels を Node.js で作って試すと、"toString" in labels は true、Object.hasOwn(labels, "toString") は false でした。in で確かめると、toString を対応表のキーとして通してしまいます。
if (status in labels) だけではエラーは消えない
型ガードの関数を書かずに、if (status in labels) { return labels[status]; } と書いた場合は、if の中でも同じ TS7053 が出ました。ホバーで見ると、if の中の status は (parameter) status: string のままです。in で調べても、TypeScript 7.0.2 ではキーの側の型は labels のキーに絞られませんでした。
どんなキーでも受けるオブジェクトには Record で型を書く
キーが決まっていない辞書として使うオブジェクトなら、変数に Record<string, string> と型を書きます。ユーティリティ型の説明(英語)にあるとおり、キーが string、値が string のオブジェクトの型です。
const labels: Record<string, string> = {
draft: "下書き",
published: "公開",
};
この型なら string のキーで読めるので、labels[status] は TS7053 になりませんでした。ただし、関数のホバーは function getLabel(status: string): string | undefined でした。tsc --init で作った設定に入っている noUncheckedIndexedAccess(英語)が、宣言していないキーで読んだ値の型に undefined を足すためです。実際に getLabel("archived") を動かすと、undefined と出ました。読んだ値の toUpperCase() を、labels[status].toUpperCase() のようにそのまま呼ぶと、error TS2532: Object is possibly 'undefined'. になりました。次のように、undefined かどうかを確かめてから使うと、エラーは消えました。
export function getLabel(status: string) {
const label = labels[status];
if (label === undefined) {
return "不明";
}
return label.toUpperCase();
}
getLabel("draft") は 下書き、getLabel("archived") は 不明 を返しました(日本語の文字は toUpperCase() では変わらないので、下書き はそのままです)。
labels にあるキーが決まっているなら、Record<string, string> にするよりも、ここまでに紹介した keyof typeof や型ガードのほうが、キーの間違いをエラーで見つけられます。
labels[status as keyof typeof labels] のように as で型を書き換えても、TS7053 は消えます。ただ、中身を確かめずに「キーだ」と言い切る書き方なので、関数の戻り値の型は string とホバーに出るのに、getLabel("archived") を動かすと undefined が返りました。外から来る文字列には、型ガードを使います。
Object.keys のループで出る場合は Object.entries を使う
Object.keys で取り出したキーで、同じオブジェクトの値を読むと、同じ 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; }'.
name のホバーは const name: string でした。TypeScript 7.0.2 に入っている型定義(lib.es5.d.ts)を見ると、Object.keys は keys(o: object): string[]; と宣言されていて、戻り値は "apple" | "banana" ではなく string の配列です。for (const name in stock) と書いた場合も、同じエラーが同じ位置に出ました。
キーと値の両方が要るなら、Object.entries()(MDN)で、キーと値の組を取り出します。
for (const [name, count] of Object.entries(stock)) {
console.log(name, count);
}
このファイルでは tsc のエラーは出ず、count のホバーは const count: number でした。JavaScript にして動かすと、apple 3 と banana 5 の2行が出ました。





