TypeScriptのTS2769エラーの読み方と直し方
error TS2769: No overload matches this call. は、呼び方(オーバーロード)を複数持つ関数に、どの呼び方にも合わない引数を渡したときに出ます。メッセージの続きには、合わなかった引数の型と、関数が受け取る型が書かれています。渡す値の型をどれかの呼び方に合わせるか、関数の側で受け取れる型を広げると、エラーは消えます。
addEventListener にイベントと合わない型の関数を渡した、replace の置き換える文字列に数値を渡した、というように、関数を呼んだ行に VS Code などのエディターで赤い波線が出たり、型チェック(tsc)が失敗したりしたときに、メッセージの読み方と直し方が分かります。私は検証環境でエラーを起こしてメッセージをそのまま載せ、直したあとにエラーが消えることを確かめました。
検証環境は Ubuntu 24.04、Node.js 22.23.4、npm 10.9.9、TypeScript 7.0.2 です。メッセージの出方を比べるため、TypeScript 5.9.3 も使いました。VS Code などのエディター本体の画面は確かめていません。代わりに TypeScript 7 の言語サーバー(tsc --lsp --stdio)を直接動かし、返ってくる診断を確かめました。React などのライブラリの型や、Vite などのバンドラーを通したビルドは試していません。
エラーを起こしたコードとメッセージ
空のフォルダーで npm init -y を実行し、package.json に "type": "module" を足して、TypeScript 7.0.2 を入れました。npx tsc --init で作った tsconfig.json には "strict": true が入っていて、ここでは手を加えずに使っています。
toLabel は、文字列を受け取る呼び方と、数値と単位を受け取る呼び方の2つを持つ関数です。TypeScript ハンドブックの、関数のオーバーロードの説明にあるとおり、本体の前に呼び方(オーバーロードのシグネチャー)を並べて書きます。この関数に true を渡した src/label.ts です。
function toLabel(value: string): string;
function toLabel(value: number, unit?: string): string;
function toLabel(value: string | number, unit = ""): string {
return typeof value === "number" ? `${value}${unit}` : value;
}
console.log(toLabel(true));
npx tsc --noEmit を実行すると次のように出て、終了コードは 1 でした。
$ npx tsc --noEmit
src/label.ts(7,21): error TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type 'boolean' is not assignable to parameter of type 'number'.
--pretty を付けると、エラーの場所と、最後のオーバーロードを宣言した場所が並んで出ます(色は省いています)。
src/label.ts:7:21 - error TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type 'boolean' is not assignable to parameter of type 'number'.
7 console.log(toLabel(true));
~~~~
src/label.ts:2:10 - The last overload is declared here.
2 function toLabel(value: number, unit?: string): string;
~~~~~~~
Found 1 error in src/label.ts:7
TypeScript 7 の言語サーバー(tsc --lsp --stdio)にも同じファイルを開かせました。返ってきた診断は1件で、tsc と同じ文面です。下の表示は、返ってきた診断を、私が書いた確認用のスクリプトで、行と列を1から数える形に直したものです。
diagnostics (1):
7:21-7:25 severity=1 source=ts code=2769 No overload matches this call.
The last overload gave the following error.
Argument of type 'boolean' is not assignable to parameter of type 'number'.
related src/label.ts:2:10 The last overload is declared here.
診断の範囲は 7:21-7:25 で、7行目の true の4文字です。Language Server Protocol の仕様にある DiagnosticSeverity の定義では、severity の 1 はエラーを表します。
メッセージの読み方
1行目の No overload matches this call. は、「この呼び出しに合うオーバーロードがない」という意味です。2行目より下は、合わなかった理由です。
The last overload gave the following error.:最後のオーバーロード(ここでは2行目のtoLabel(value: number, unit?: string))で調べたときのエラーが、下に続くということArgument of type 'boolean' is not assignable to parameter of type 'number'.:渡したbooleanの値を、numberの引数には渡せないということThe last overload is declared here.:その最後のオーバーロードを宣言している場所
検証環境の TypeScript 7.0.2 では、この記事で試したどの例でも、メッセージに出たのは最後のオーバーロードの理由だけでした。1つ目のオーバーロード(string を受け取る呼び方)にも合わなかったことは、このメッセージからは読めません。ほかの呼び方は、The last overload is declared here. が指す宣言の近くを見て確かめます。
TypeScript 5.9 では、引数の数が合う呼び方が2〜3個ならオーバーロードごとに理由が出る
同じ tsconfig.json のまま、TypeScript 5.9.3 の tsc で同じ src/label.ts を調べると、オーバーロードごとに理由が並びました。
$ npx tsc --noEmit
src/label.ts(7,21): error TS2769: No overload matches this call.
Overload 1 of 2, '(value: string): string', gave the following error.
Argument of type 'boolean' is not assignable to parameter of type 'string'.
Overload 2 of 2, '(value: number, unit?: string | undefined): string', gave the following error.
Argument of type 'boolean' is not assignable to parameter of type 'number'.
Overload 1 of 2 は「2つあるうちの1つ目のオーバーロード」で、その後ろに呼び方の型が書かれています。ただし、5.9.3 でもいつもこの形になるわけではありません。
5.9.3 の typescript.js では、引数の数が合う呼び方(候補)の数で、出し方が分かれています。if (candidatesForArgumentError.length === 1 || candidatesForArgumentError.length > 3) { のうち、No overload matches this call と The last overload gave the following error. を付けるのは、候補が4個以上のとき(length > 3)だけです。4個以上なら TS2769 のまま最後のオーバーロードの理由だけを出し、1個なら TS2769 の見出しを付けずに、その呼び方と比べたエラーを出します(後の節の例では、TypeScript 7.0.2 で TS2345 になりました)。
後で取り上げる replace(この tsconfig.json では呼び方が4つ)を 5.9.3 で調べると、TypeScript 7.0.2 と同じ The last overload gave the following error. の形でした。候補が2個以上なら、理由をオーバーロードごとに並べる形でも最後の1つだけを出す形でも、番号は同じ TS2769 です。
渡す値の型を、どれかのオーバーロードに合わせて直す
まず確かめるのは、渡したい値がどの呼び方に当てはまるかです。toLabel は string か number しか受け取らないので、boolean の値は、表示したい文字列に変えてから渡します(検証では、直したものを別のファイルにして試しました)。
const done = true;
console.log(toLabel(done ? "完了" : "未完了"));
console.log(toLabel(3, "匹"));
npx tsc --noEmit は何も出力せずに終了コード 0 で終わり、言語サーバーの診断も 0 件になりました。JavaScript に書き出して実行すると、次のように出ます。
完了
3匹
引数の数で候補が1つに絞られた例では、TS2345 になった
オーバーロードを持つ関数でも、いつも TS2769 になるわけではありません。2つ目の呼び方の unit を省略できないようにして(unit: string)、同じ toLabel(true) を調べました。
src/single.ts(7,21): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'.
引数が1つの呼び出しに引数の数が合うのは、引数を1つ受け取る1つ目の呼び方だけです。この例では、その呼び方の string と比べた TS2345 になりました。オーバーロードのある関数で TS2345 が出たときは、引数の数で別の呼び方が外れていないかも見てください。
string | number の値を、型ごとに分けた呼び方に渡すと TS2769 が出る
自分で書いたオーバーロードでは、次の形でも TS2769 が出ます。formatValue は string 用と number 用の呼び方を持ち、本体は string | number を受け取ります。この関数に、string | number 型の input をそのまま渡しました。
function formatValue(value: string): string;
function formatValue(value: number): string;
function formatValue(value: string | number): string {
return typeof value === "number" ? value.toFixed(1) : value.trim();
}
function show(input: string | number) {
return formatValue(input);
}
console.log(show(" 猫の手 "), show(3));
src/union.ts:8:22 - error TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type 'string | number' is not assignable to parameter of type 'number'.
Type 'string' is not assignable to type 'number'.
8 return formatValue(input);
~~~~~
src/union.ts:2:10 - The last overload is declared here.
2 function formatValue(value: number): string;
~~~~~~~~~~~
src/union.ts:3:10 - The call would have succeeded against this implementation, but implementation signatures of overloads are not externally visible.
3 function formatValue(value: string | number): string {
~~~~~~~~~~~
Found 1 error in src/union.ts:8
最後の The call would have succeeded against this implementation, but implementation signatures of overloads are not externally visible. は、「本体(3行目)の型なら呼べたが、本体の型は外からは見えない」という意味です。言語サーバーの診断にも、related として同じ2か所が付いていました。
ハンドブックの、オーバーロードの良い書き方の説明にあるとおり、TypeScript は1つの呼び出しを1つのオーバーロードに当てはめます。string かもしれないし number かもしれない値は、string 用にも number 用にも当てはまりません。直し方は3つ試しました。どれも npx tsc --noEmit が終了コード 0 で通り、実行結果は 猫の手 3.0 でした。
1つ目は、呼ぶ前に typeof で型を絞る直し方です。それぞれの formatValue の呼び出しでは、input の型が number か string の1つに決まっています。
function show(input: string | number) {
if (typeof input === "number") {
return formatValue(input);
}
return formatValue(input);
}
2つ目は、string | number を受け取る呼び方を、オーバーロードに足す直し方です。本体と同じ型ですが、本体の型は外から見えないので、呼び方として書き足します。
function formatValue(value: string): string;
function formatValue(value: number): string;
function formatValue(value: string | number): string;
function formatValue(value: string | number): string {
return typeof value === "number" ? value.toFixed(1) : value.trim();
}
3つ目は、オーバーロードをやめて、引数を string | number の1つにする直し方です。
function formatValue(value: string | number): string {
return typeof value === "number" ? value.toFixed(1) : value.trim();
}
ハンドブックは、この例のようにどの呼び方も引数の数と戻り値の型が同じなら、オーバーロードを使わずユニオン型の引数にした書き方を示しています。そのうえで、できるときはオーバーロードよりユニオン型の引数を使うよう勧めています。
addEventListener では、メッセージが別のオーバーロードの理由を説明していることがある
標準の型定義(lib)にも、オーバーロードを持つ関数がたくさんあります。keydown のイベントを、MouseEvent を受け取る関数で受けようとしました。
document.addEventListener("keydown", (e: MouseEvent) => {
console.log(e.button);
});
src/listener.ts:1:38 - error TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type '(e: MouseEvent) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
Type '(e: MouseEvent) => void' is not assignable to type 'EventListener'.
Types of parameters 'e' and 'evt' are incompatible.
Type 'Event' is missing the following properties from type 'MouseEvent': altKey, button, buttons, clientX, and 23 more.
1 document.addEventListener("keydown", (e: MouseEvent) => {
~~~~~~~~~~~~~~~~~~~~
node_modules/@typescript/typescript-linux-x64/lib/lib.dom.d.ts:13200:5 - The last overload is declared here.
13200 addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error in src/listener.ts:1
メッセージに出てくるのは Event 型で、KeyboardEvent は出てきません。document.addEventListener の型定義を見ると、オーバーロードは2つあります。
addEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
"keydown" のようなイベント名には1つ目が使われ、同じ型定義の DocumentEventMap に "keydown": KeyboardEvent; と書かれています。メッセージが説明しているのは、どんな名前でも受け取る2つ目のほうです。TypeScript 5.9.3 で調べると、1つ目のオーバーロードの理由も出て、KeyboardEvent との違いが分かりました。出力のうち、1つ目のオーバーロードの部分を抜き出すと次のとおりです。
Overload 1 of 2, '(type: "keydown", listener: (this: Document, ev: KeyboardEvent) => any, options?: boolean | AddEventListenerOptions | undefined): void', gave the following error.
Argument of type '(e: MouseEvent) => void' is not assignable to parameter of type '(this: Document, ev: KeyboardEvent) => any'.
Types of parameters 'e' and 'ev' are incompatible.
Type 'KeyboardEvent' is missing the following properties from type 'MouseEvent': button, buttons, clientX, clientY, and 14 more.
引数の型を KeyboardEvent にし、button の代わりに key を使うと、エラーは消えました。
document.addEventListener("keydown", (e: KeyboardEvent) => {
console.log(e.key);
});
TypeScript 7 のメッセージに出てくる型が、渡そうとした値と結びつかないときは、型定義で最後のオーバーロードより上に並んでいる呼び方も見てください。
replace の置き換える文字列に数値を渡したとき
文字列の replace も、オーバーロードを持っています。この tsconfig.json(target が esnext)では、lib.es5.d.ts に2つ、Symbol.replace を持つオブジェクトで探すための呼び方が lib.es2015.symbol.wellknown.d.ts に2つ、合わせて4つでした。文字列で探すときに使うのは、置き換える文字列を渡す呼び方と、置き換える文字列を返す関数を渡す呼び方です。数値の count をそのまま渡しました。
const count = 0;
console.log("残り{n}匹".replace("{n}", count));
src/replace.ts:2:37 - error TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type 'number' is not assignable to parameter of type '(substring: string, ...args: any[]) => string'.
2 console.log("残り{n}匹".replace("{n}", count));
~~~~~
node_modules/@typescript/typescript-linux-x64/lib/lib.es5.d.ts:468:5 - The last overload is declared here.
468 replace(searchValue: string | RegExp, replacer: (substring: string, ...args: any[]) => string): string;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Found 1 error in src/replace.ts:2
ここでもメッセージは、関数を渡す呼び方の説明です。lib.es5.d.ts の1つ前の宣言(461行目)には、replace(searchValue: string | RegExp, replaceValue: string): string; と、文字列を受け取る呼び方があります。数値を String(count) で文字列にして渡すと、エラーは消え、実行すると 残り0匹 と出ました。
console.log("残り{n}匹".replace("{n}", String(count)));





