TypeScriptでプロパティ名を間違えたときのTS2551の直し方
TypeScript の error TS2551: Property 'emial' does not exist on type 'User'. Did you mean 'email'? は、プロパティの名前を打ち間違えたときに出るエラーです。メッセージの最後の Did you mean に、型の中でいちばん近い名前が出ます。その名前に書き直すと、エラーは消えます。
API のレスポンスに付けた型のプロパティや、document.getElementById のような DOM のメソッドを使っていて、エディターに赤い波線が出たり、npm run build が tsc のところで失敗したりしたときに見かけるエラーです。ただし、候補がいつも正しいとは限りません。型の定義のほうを打ち間違えていると、間違ったほうの名前が候補に出ます。
検証環境は Ubuntu 24.04、Node.js 22.23.5、npm 10.9.9、TypeScript 7.0.2 です。エディターのクイックフィックスは、TypeScript 5.9.3 の言語サービス(tsserver)でも確かめました。VS Code などのエディターの画面では確かめていません。エディターが裏で使う言語サーバーを、スクリプトから直接動かして確かめています。
TS2551 を起こして、メッセージを読む
npm init -y で作ったフォルダーに TypeScript 7.0.2 を入れ、次の tsconfig.json を置きました。
{
"compilerOptions": {
"target": "es2022",
"module": "esnext",
"moduleResolution": "bundler",
"strict": true,
"noEmit": true
},
"include": ["src"]
}
src/user.ts に、email を持つ User 型と、メールのリンクを作る関数を書きました。8行目で email を emial と打ち間違えています。
export type User = {
id: number;
name: string;
email: string;
};
export function mailLink(user: User): string {
return `mailto:${user.emial}`;
}
npx tsc を実行すると、次のエラーが出て、終了コードは 1 でした。
$ npx tsc
src/user.ts(8,25): error TS2551: Property 'emial' does not exist on type 'User'. Did you mean 'email'?
(8,25) は8行目の25文字目で、user.emial の emial の位置です。メッセージは「User 型に emial というプロパティはありません。email のことですか」という意味です。
--pretty を付けると、候補の名前を型のどこで宣言しているかも出ます(色を付けるための制御文字は消しています)。
$ npx tsc --pretty
src/user.ts:8:25 - error TS2551: Property 'emial' does not exist on type 'User'. Did you mean 'email'?
8 return `mailto:${user.emial}`;
~~~~~
src/user.ts:4:3 - 'email' is declared here.
4 email: string;
~~~~~
Found 1 error in src/user.ts:8
'email' is declared here. の行が、候補の email を宣言している場所です。この行を見れば、どの型のどのプロパティと比べられたのかが分かります。型を別のファイルに書いている例は、あとの節で試します。
候補の名前に書き直すと消える
8行目の user.emial を user.email に直して npx tsc を実行すると、何も出力せずに終了コード 0 になりました。
ブラウザーの API でも同じです。getElementById の最後の d を大文字にした getElementByID と書くと、TS2551 になり、正しい名前が候補に出ました。
const app = document.getElementByID("app");
if (app) {
app.textContent = "読み込みました";
}
$ npx tsc
src/page.ts(1,22): error TS2551: Property 'getElementByID' does not exist on type 'Document'. Did you mean 'getElementById'?
getElementById に直すと、終了コード 0 になりました。
エディターのクイックフィックスで直す
VS Code のリファレンスにある、Code Actions の説明によると、赤い波線にカーソルを置いて電球のアイコンを選ぶか、Quick Fix のコマンドを使うと、その場で直す方法(クイックフィックス)の一覧が出ます。TypeScript の場合、この一覧を作るのは TypeScript の言語サービスです。そこで、言語サービスに同じ src/user.ts を開かせ、クイックフィックスを問い合わせました。
TypeScript 5.9.3 の tsserver からは、3つのクイックフィックスが返ってきました。返ってきた JSON を、私が書いた小さなスクリプトで並べて表示しています。
typescript: 5.9.3
diagnostics (1):
8:25-8:30 code=2551 Property 'emial' does not exist on type 'User'. Did you mean 'email'?
codeFix [spelling] Change spelling to 'email'
edit src/user.ts 8:25-8:30 -> "email"
codeFix [fixMissingMember] Declare property 'emial'
edit src/user.ts 1:21-1:21 -> "\n emial: any;"
codeFix [fixMissingMember] Add index signature for property 'emial'
edit src/user.ts 1:21-1:21 -> "\n [x: string]: any;"
選ぶのは、1つ目の Change spelling to 'email' です。8行目の emial を email に置き換えます。
2つ目の Declare property 'emial' と3つ目の Add index signature for property 'emial' も、選べばエラーは消えます。ただし、書き換わるのは User 型のほうです。打ち間違えた emial: any; が型に足されたり、どんな名前でも通る [x: string]: any; が足されたりします。打ち間違いを直したいときには選びません。
TypeScript 7.0.2 の言語サーバー(tsc --lsp --stdio)にも同じように問い合わせました。診断のメッセージは tsc と同じで、'email' is declared here. の場所も返ってきました。一方、クイックフィックスは1つも返ってきませんでした。7.0.2 のソースでは、登録されているクイックフィックスは、import の追加など3種類だけです(typescript-go の v7.0.2 の codeactions.go)。この言語サーバーを使うエディターでは、メッセージの候補を見て手で書き直します。
型の定義のほうが間違っているときは、名前の変更で直す
候補に出るのは、型に書いてある名前です。型の定義のほうを打ち間違えていると、間違った名前が候補になります。
次の例では、src/types.ts の User 型に emial と書いてしまっています。前からある src/profile.ts は、その名前に合わせて user.emial と書いていて、エラーは出ていません。新しく書いた src/mail.ts で、正しい user.email を書きました。
// src/types.ts
export type User = {
id: number;
name: string;
emial: string;
};
// src/profile.ts
import type { User } from "./types";
export function profileLine(user: User): string {
return `${user.name}(${user.emial})`;
}
// src/mail.ts
import type { User } from "./types";
export function mailLink(user: User): string {
return `mailto:${user.email}`;
}
$ npx tsc
src/mail.ts(4,25): error TS2551: Property 'email' does not exist on type 'User'. Did you mean 'emial'?
エラーは正しく書いた mail.ts に出て、候補は打ち間違えた emial です。候補のとおりに直すと、間違った名前を広げることになります。--pretty を付けると、候補を宣言している場所として、別のファイルの types.ts が出ました。
$ npx tsc --pretty
src/mail.ts:4:25 - error TS2551: Property 'email' does not exist on type 'User'. Did you mean 'emial'?
4 return `mailto:${user.email}`;
~~~~~
src/types.ts:4:3 - 'emial' is declared here.
4 emial: string;
~~~~~
Found 1 error in src/mail.ts:4
Did you mean の名前がおかしいと思ったら、この is declared here. の行から、型の定義を見に行きます。
直すのは型の定義です。ただ、types.ts の emial だけを email に書き換えると、今度は profile.ts の user.emial がエラーになりました。
$ npx tsc
src/profile.ts(4,31): error TS2551: Property 'emial' does not exist on type 'User'. Did you mean 'email'?
VS Code のリファレンスにある、Rename Symbol の説明のとおり、名前の変更のコマンドを使うと、ほかのファイルで使っているところも一緒に書き換わります。
TypeScript 7.0.2 の言語サーバーで、types.ts の4行目の emial を email に名前変更するよう求めると、次の2か所の書き換えが返ってきました。
rename 4:3 -> email:
edit src/types.ts 4:3-4:8 -> "email"
edit src/profile.ts 4:31-4:36 -> "email"
型の定義と、profile.ts の user.emial です。エラーが出ていた mail.ts は、もともと正しい名前なので含まれていません。この2か所をそのとおりに書き換えると、npx tsc は終了コード 0 になりました。
候補が出ないときは TS2339 になる
名前が似ていても、いつも TS2551 になるわけではありません。候補が見つからないと、Did you mean の付かない TS2339 になります。同じ User 型で、いくつかの間違い方を試しました。
type User = {
id: number;
name: string;
email: string;
};
declare const user: User;
user.nmae;
user.nam;
user.userName;
user.Name;
const { emial } = user;
$ npx tsc
src/no-hint.ts(9,6): error TS2339: Property 'nmae' does not exist on type 'User'.
src/no-hint.ts(10,6): error TS2551: Property 'nam' does not exist on type 'User'. Did you mean 'name'?
src/no-hint.ts(11,6): error TS2339: Property 'userName' does not exist on type 'User'.
src/no-hint.ts(12,6): error TS2551: Property 'Name' does not exist on type 'User'. Did you mean 'name'?
src/no-hint.ts(13,9): error TS2339: Property 'emial' does not exist on type 'User'.
name の2文字を入れ替えた nmae には、候補が出ませんでした。5文字の emial では email が出たのに、4文字の名前では、同じ入れ替えでも候補になりません。候補を選ぶ処理のソース(typescript-go の v7.0.2)を見ると、名前の長さに応じて、どこまでの違いなら候補にするかの上限を決めています。短い名前ほど、許される違いは小さくなります。長さが大きく違う名前(userName と name)も、候補にしません。
大文字と小文字だけが違う Name は、TS2551 になりました。2つの名前の違いを数える部分のソースでも、大文字と小文字の違いは、ほかの文字との違いよりずっと小さく数えています。
分割代入(const { emial } = user;)では、5文字の emial でも候補が出ず、TS2339 になりました。TS2339 が出たときも、打ち間違いではないかを、まず型の定義と見比べます。
候補が出る、ほかの番号のエラー
Did you mean で候補を教えてくれるエラーは、TS2551 のほかにもあります。オブジェクトのリテラルでプロパティ名を間違えたときと、変数の名前を間違えたときを試しました。
type User = {
id: number;
name: string;
email: string;
};
const user: User = { id: 1, name: "たま", emial: "tama@example.com" };
const userName = user.name;
console.log(usrName);
$ npx tsc
src/other-codes.ts(7,41): error TS2561: Object literal may only specify known properties, but 'emial' does not exist in type 'User'. Did you mean to write 'email'?
src/other-codes.ts(10,13): error TS2552: Cannot find name 'usrName'. Did you mean 'userName'?
型を付けた変数にオブジェクトを書くときの間違いは TS2561、変数の名前の間違いは TS2552 です。番号は違っても、直し方は TS2551 と同じで、候補の名前と型の定義を見比べて書き直します。



