- TypeScript
TS2345「引数を割り当てられません」原因と直し方の早見表
TS2345とは、関数を呼び出すときに渡した値(引数)の型が、関数が受け取る型(パラメーター)に合わないと知らせるTypeScriptのエラーです。 エラー文は必ず「Argument of type 'A' is not assignable to parameter of type 'B'」の形をしていて、A が渡した値の型、B が関数の受け取る型です。日本語表示では「型 'A' の引数を型 'B' のパラメーターに割り当てることはできません」となります。
この記事では、A と B の組み合わせから原因を引ける早見表を先に示し、そのあとで8つの原因を1つずつ再現して、tsc の出力全文と修正後のコードを対で載せます。出力はすべて手元で tsc にかけて得たものです。
確認環境:Ubuntu 24.04 / Node.js 22.22.2 / TypeScript 7.0.2(npx tsc --version の表示は Version 7.0.2)。比較には TypeScript 5.9.3 と 6.0.3 を使いました。
TS2345の早見表(エラー文の型の組み合わせ→原因→直し方)
エラー文の「Argument of type 'A'」と「parameter of type 'B'」を見て、当てはまる行を探してください。
| A(渡した型) | B(受け取る型) | 原因 | 直し方 |
|---|---|---|---|
string | null |
string |
戻り値に null が含まれる(getItem、querySelector など) | if で null を分ける/?? で既定値を決める |
string | undefined |
string |
省略可能プロパティ、find の戻り値、環境変数 | if で絞り込む/?? で既定値を決める |
string |
"GET" | "POST" などのユニオン型 |
リテラル型が string に広がった | as const/satisfies/変数に型注釈 |
(name: string) => void など関数型 |
(item: Item) => void など関数型 |
コールバックの引数の型や個数が合わない | アロー関数を1枚はさんで受け渡す |
string | number |
string |
ユニオン型のままオーバーロードに渡した | typeof で絞り込んでから呼ぶ |
string や "typescript" |
never |
空配列 [] が never[] と推論された |
配列の要素の型を先に書く |
string |
number(ジェネリクス関数で) |
型引数 T が最初の引数で決まった | 型引数を明示する |
{ id: string; ... } |
User などオブジェクト型 |
プロパティの型が合わない | 渡す前に値を変換する/変数に型注釈 |
unknown |
string |
型が分からない値をそのまま渡した | typeof などで確かめてから渡す |
Promise<string> |
string |
await を付けていない | await を付ける |
エラー文が複数行あるときは、インデントが最も深い最後の行に、合わない理由の核心が書かれています。
本記事の内容は、ご自由にお使いください。
ご利用の際は、出典として本ページへのリンクを記載いただけますようお願いします。
(記載例)出典:株式会社RJC「TS2345「引数を割り当てられません」原因と直し方の早見表」
TS2345とTS2322・TS2769の見分け方
似たエラーは「どこで型が合わなかったか」で番号が変わります。次のコードを tsc にかけます。
function double(n: number): number {
return n * 2;
}
const text: string = "10";
const a: number = text; // 代入
double(text); // 引数
function f(): number {
return text; // 戻り値
}
09-2322.ts(7,7): error TS2322: Type 'string' is not assignable to type 'number'.
09-2322.ts(8,8): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
09-2322.ts(10,3): error TS2322: Type 'string' is not assignable to type 'number'.
| 番号 | 英語のエラー文 | 日本語のエラー文(--locale ja) |
出る場所 |
|---|---|---|---|
| TS2345 | Argument of type 'A' is not assignable to parameter of type 'B'. | 型 'A' の引数を型 'B' のパラメーターに割り当てることはできません。 | 関数呼び出しの引数 |
| TS2322 | Type 'A' is not assignable to type 'B'. | 型 'A' を型 'B' に割り当てることはできません。 | 変数への代入、return、オブジェクトリテラルのプロパティ |
| TS2769 | No overload matches this call. | この呼び出しに一致するオーバーロードはありません。 | オーバーロードのどれにも合わない呼び出し |
「Argument」「引数」という語があれば TS2345、なければ TS2322 と覚えると見分けやすくなります。原因の考え方はどちらも同じで、「A を B に合わせる」ことが直し方になります。
tsconfig と実行コマンド
以降の例は、次の tsconfig.json を置いたフォルダで実行しています。
{
"compilerOptions": {
"target": "es2022",
"module": "nodenext",
"strict": true,
"noEmit": true,
"types": ["node"],
"lib": ["es2022", "dom"]
}
}
npm i -D typescript@latest @types/node
npx tsc --noEmit -p . # 英語のエラー文
npx tsc --noEmit -p . --locale ja # 日本語のエラー文
修正後のコードは node ファイル名.ts でそのまま実行して出力を確かめています(Node.js 22.22.2 は型注釈を取り除いて .ts ファイルを実行できます)。
原因1:string | null を string の引数に渡している
localStorage.getItem のように「見つからなければ null」を返す関数の戻り値を、そのまま別の関数へ渡すと出ます。Node.js でも動くよう、同じ形の getItem を用意して再現します。
const store = new Map<string, string>([["cart", '["ノート","ペン"]']]);
// localStorage.getItem と同じ形: 見つからなければ null
function getItem(key: string): string | null {
return store.get(key) ?? null;
}
const saved = getItem("cart");
const cart = JSON.parse(saved);
01-null.ts(9,25): error TS2345: Argument of type 'string | null' is not assignable to parameter of type 'string'.
Type 'null' is not assignable to type 'string'.
2行目の「Type 'null' is not assignable to type 'string'」が核心です。string の部分は合っていて、null だけが余っていると読めます。null のときにどうするかを決めれば解決します。
const store = new Map<string, string>([["cart", '["ノート","ペン"]']]);
function getItem(key: string): string | null {
return store.get(key) ?? null;
}
// 直し方1: if で null を先に分ける
const saved = getItem("cart");
if (saved !== null) {
console.log(JSON.parse(saved));
}
// 直し方2: ?? で「無かったときの文字列」を決める
const history = JSON.parse(getItem("history") ?? "[]");
console.log(history);
[ 'ノート', 'ペン' ]
[]
原因2:undefined かもしれない値を渡している
省略可能なプロパティ(nickname?: string)、配列の find、環境変数(process.env)は、どれも undefined を含む型になります。
type Member = { name: string; nickname?: string };
function greet(name: string): string {
return `こんにちは、${name}さん`;
}
const members: Member[] = [{ name: "田中" }, { name: "佐藤", nickname: "サト" }];
const m = members.find((x) => x.name === "佐藤");
greet(m);
greet(m.nickname);
greet(process.env.USER_NAME);
02-undefined.ts(10,7): error TS2345: Argument of type 'Member | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.
02-undefined.ts(11,7): error TS18048: 'm' is possibly 'undefined'.
02-undefined.ts(11,7): error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.
02-undefined.ts(12,7): error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
Type 'undefined' is not assignable to type 'string'.
10行目は少し注意が必要です。A が Member | undefined なのに、2行目には undefined のことしか書かれていません。undefined を取り除いても Member は string に合わないので、渡す値そのもの(m ではなく m.name)を見直す必要があります。11行目は「m が undefined かもしれない(TS18048)」と「nickname が undefined かもしれない(TS2345)」の2つが同時に出ています。
type Member = { name: string; nickname?: string };
function greet(name: string): string {
return `こんにちは、${name}さん`;
}
const members: Member[] = [{ name: "田中" }, { name: "佐藤", nickname: "サト" }];
const m = members.find((x) => x.name === "佐藤");
if (m) {
console.log(greet(m.name));
console.log(greet(m.nickname ?? m.name));
}
const userName = process.env.USER_NAME;
if (userName === undefined) {
console.log("USER_NAME が設定されていません");
} else {
console.log(greet(userName));
}
USER_NAME=山田 node 02-undefined-fix.ts で実行した出力です。
こんにちは、佐藤さん
こんにちは、サトさん
こんにちは、山田さん
原因3:リテラル型が string に広がっている
A が string、B が "GET" | "POST" のようなユニオン型(ここでは Method という名前)のときは、値は正しいのに型だけが広がっています。
type Method = "GET" | "POST";
function request(url: string, method: Method): string {
return `${method} ${url}`;
}
const options = { url: "/items", method: "GET" };
request(options.url, options.method);
let method = "POST";
request("/items", method);
03-literal.ts(8,22): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Method'.
03-literal.ts(11,19): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Method'.
オブジェクトのプロパティや let の変数は、あとで書き換えられるため、"GET" というリテラル型ではなく string と推論されます。「この値は変わらない」または「この型のどれかである」と TypeScript に伝えると通ります。
type Method = "GET" | "POST";
function request(url: string, method: Method): string {
return `${method} ${url}`;
}
// 直し方1: as const でリテラル型のまま固定する
const a = { url: "/items", method: "GET" } as const;
console.log(request(a.url, a.method));
// 直し方2: satisfies で形を検査しつつリテラル型を残す
const b = { url: "/items", method: "POST" } satisfies { url: string; method: Method };
console.log(request(b.url, b.method));
// 直し方3: 変数に型注釈を付ける
let method: Method = "POST";
console.log(request("/items", method));
GET /items
POST /items
POST /items
| 直し方 | 向いている場面 |
|---|---|
as const |
書き換えない定数オブジェクト。すべてのプロパティが読み取り専用になる |
satisfies 型 |
値のつづりまちがいを定義した行で見つけたいとき |
| 変数の型注釈 | let の変数や、あとで別の値を入れる変数 |
原因4:コールバックの引数の型や個数が合わない
A と B がどちらも関数型のときは、2行目以降に「どの引数が合わないか」が出ます。
type Item = { id: number; name: string };
function each(items: Item[], fn: (item: Item) => void): void {
items.forEach(fn);
}
function printName(name: string): void {
console.log(name);
}
const items: Item[] = [{ id: 1, name: "ノート" }];
each(items, printName);
each(items, (item: Item, index: number) => console.log(index, item.name));
04-callback.ts(13,13): error TS2345: Argument of type '(name: string) => void' is not assignable to parameter of type '(item: Item) => void'.
Types of parameters 'name' and 'item' are incompatible.
Type 'Item' is not assignable to type 'string'.
04-callback.ts(14,13): error TS2345: Argument of type '(item: Item, index: number) => void' is not assignable to parameter of type '(item: Item) => void'.
Target signature provides too few arguments. Expected 2 or more, but got 1.
13行目の最後の行は「Item を string には渡せない」です。each は Item を1つ渡して呼び出すのに、printName は string を待っている、という向きで読みます。14行目は、コールバックが必須の引数を2つ求めているのに、呼び出す側は1つしか渡さないという意味です。反対に、コールバックの引数が少ないぶんにはエラーになりません。
type Item = { id: number; name: string };
function each(items: Item[], fn: (item: Item) => void): void {
items.forEach(fn);
}
function printName(name: string): void {
console.log(name);
}
const items: Item[] = [{ id: 1, name: "ノート" }];
// 受け取る型に合わせて、アロー関数で1枚はさむ
each(items, (item) => printName(item.name));
ノート
原因5:オーバーロードのどれにも合わない(TS2769との違い)
オーバーロード(同じ名前で引数の形が違う定義を複数持つ関数)では、TS2345 が出る場合と TS2769 が出る場合があります。
function toLabel(value: string): string;
function toLabel(value: number, digits: number): string;
function toLabel(value: string | number, digits?: number): string {
return typeof value === "number" ? value.toFixed(digits) : value;
}
toLabel(true);
declare const input: string | number;
toLabel(input);
const el = document.createElement("div");
el.addEventListener("click", (e: KeyboardEvent) => console.log(e.key));
05-overload.ts(7,9): error TS2345: Argument of type 'boolean' is not assignable to parameter of type 'string'.
05-overload.ts(10,9): error TS2345: Argument of type 'string | number' is not assignable to parameter of type 'string'.
Type 'number' is not assignable to type 'string'.
05-overload.ts(13,30): error TS2769: No overload matches this call.
The last overload gave the following error.
Argument of type '(e: KeyboardEvent) => void' is not assignable to parameter of type 'EventListenerOrEventListenerObject'.
Type '(e: KeyboardEvent) => void' is not assignable to type 'EventListener'.
Types of parameters 'e' and 'evt' are incompatible.
Type 'Event' is missing the following properties from type 'KeyboardEvent': altKey, charCode, code, ctrlKey, and 17 more.
toLabel(true) と toLabel(input) は引数が1つなので、引数の個数が合う候補は1つ目のオーバーロードだけです。候補が1つに絞れると、その候補に対する TS2345 が出ます。addEventListener のように個数が合う候補が複数あると TS2769 になり、その内側に「Argument of type …」の文が入ります。TS2769 は、TS2345 を包んだものと考えて、内側の文を同じ読み方で読めば原因が分かります。
10行目は、実装の引数は string | number を受け取れるのに通りません。呼び出しで照合されるのは上2行のオーバーロードだけで、実装のシグネチャは対象外だからです。typeof で絞り込んでから呼び分けます。
function toLabel(value: string): string;
function toLabel(value: number, digits: number): string;
function toLabel(value: string | number, digits?: number): string {
return typeof value === "number" ? value.toFixed(digits) : value;
}
function show(input: string | number): string {
// 絞り込んでから、それぞれのオーバーロードを呼ぶ
return typeof input === "number" ? toLabel(input, 1) : toLabel(input);
}
console.log(show("abc"), show(3.14159));
abc 3.1
13行目の例は、"click" で届くイベントに key プロパティがないことを知らせています。キー入力を受けたいならイベント名を "keydown" にする、クリックを受けたいなら引数の型注釈を外して推論に任せる、のどちらかで解決します。
原因6:空配列が never[] と推論されている
B が never のときは、ほぼ空配列 [] が原因です。
const state = { tags: [] };
state.tags.push("typescript");
const grouped = ["a", "b"].reduce((acc, cur) => {
acc.push(cur.toUpperCase());
return acc;
}, []);
06-never.ts(2,17): error TS2345: Argument of type '"typescript"' is not assignable to parameter of type 'never'.
06-never.ts(5,12): error TS2345: Argument of type 'string' is not assignable to parameter of type 'never'.
{ tags: [] } の tags は、要素の手がかりがないため never[](何も入れられない配列)と推論されます。reduce の初期値 [] も同じです。要素の型を先に書くと解決します。
// 直し方1: プロパティの型を先に決める
type State = { tags: string[] };
const state: State = { tags: [] };
state.tags.push("typescript");
// 直し方2: 空配列に as で要素の型を教える
const state2 = { tags: [] as string[] };
state2.tags.push("node");
// 直し方3: reduce は型引数で結果の型を渡す
const upper = ["a", "b"].reduce<string[]>((acc, cur) => {
acc.push(cur.toUpperCase());
return acc;
}, []);
console.log(state.tags, state2.tags, upper);
[ 'typescript' ] [ 'node' ] [ 'A', 'B' ]
原因7:ジェネリクスの型引数が思った型に決まらない
ジェネリクス(型を引数として受け取る仕組み)では、型引数は渡した値から自動で決まります。その決まり方が意図と違うと TS2345 になります。
function pair<T>(a: T, b: T): [T, T] {
return [a, b];
}
pair(1, "one");
function getProp<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
const book = { title: "入門", pages: 240 };
getProp(book, "price");
const ids = new Set([1, 2, 3]);
ids.add("4");
function createState<S>(initial: S): { get: () => S; set: (next: S) => void } {
let value = initial;
return { get: () => value, set: (next) => { value = next; } };
}
const list = createState([]);
list.set(["a"]);
07-generic.ts(4,9): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
07-generic.ts(10,15): error TS2345: Argument of type '"price"' is not assignable to parameter of type '"pages" | "title"'.
07-generic.ts(13,9): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.
07-generic.ts(20,11): error TS2322: Type 'string' is not assignable to type 'never'.
| 行 | 何が起きているか |
|---|---|
| 4行目 | 最初の引数 1 から T が number に決まり、2つ目の "one" が合わない |
| 10行目 | K は book のキーに限られる。"price" というキーは book にない(つづりを確認する) |
| 13行目 | new Set([1, 2, 3]) は number の Set と推論される |
| 20行目 | createState([]) の S が never[] になり、配列リテラルの中の "a" に TS2322 が出る |
20行目は引数の場面ですが、配列リテラルの要素が合わないため TS2322 になっています。原因6と同じ「空配列」が元なので、直し方も同じです。型引数を山かっこで明示します。
function pair<T>(a: T, b: T): [T, T] {
return [a, b];
}
// 型引数を書いて、2種類の型を受け取ると伝える
console.log(pair<number | string>(1, "one"));
const ids = new Set<number | string>([1, 2, 3]);
ids.add("4");
console.log(ids.size);
function createState<S>(initial: S): { get: () => S; set: (next: S) => void } {
let value = initial;
return { get: () => value, set: (next) => { value = next; } };
}
// 空配列だけでは要素の型が決まらないので、型引数で渡す
const list = createState<string[]>([]);
list.set(["a"]);
console.log(list.get());
[ 1, 'one' ]
4
[ 'a' ]
原因8:オブジェクトのプロパティが合わない
オブジェクトを渡す場面では、どう渡したかによって出る番号が変わります。まず、変数に入れてから渡す場合です。
type User = { id: number; name: string; email: string };
function save(user: User): void {
console.log(user.id);
}
const draft = { id: 1, name: "田中" };
save(draft);
const fromForm = { id: "1", name: "田中", email: "t@example.com" };
save(fromForm);
08b-object.ts(8,6): error TS2741: Property 'email' is missing in type '{ id: number; name: string; }' but required in type 'User'.
08b-object.ts(11,6): error TS2345: Argument of type '{ id: string; name: string; email: string; }' is not assignable to parameter of type 'User'.
Types of property 'id' are incompatible.
Type 'string' is not assignable to type 'number'.
プロパティの型が合わない11行目は TS2345 で、最後の行に「id が string なのに number が必要」と出ます。プロパティが足りない8行目は、TypeScript 7.0.2 では TS2741 が直接出ました。
次に、オブジェクトリテラルを直接渡す場合です。
save({ id: 1, name: "田中" });
save({ id: "1", name: "田中", email: "t@example.com" });
save({ id: 1, name: "田中", email: "t@example.com", age: 30 });
08-object.ts(7,6): error TS2741: Property 'email' is missing in type '{ id: number; name: string; }' but required in type 'User'.
08-object.ts(8,8): error TS2322: Type 'string' is not assignable to type 'number'.
08-object.ts(9,51): error TS2353: Object literal may only specify known properties, and 'age' does not exist in type 'User'.
リテラルを直接渡すと、合わないプロパティの位置そのものにエラーが付くため、TS2345 ではなく TS2322 や TS2353 になります。余分なプロパティ(age)の検査はリテラルを直接渡したときだけ働き、変数に入れてから渡した場合は通ります。
修正例です。
type User = { id: number; name: string; email: string };
function save(user: User): void {
console.log(user.id, user.email);
}
// 変数に型注釈を付けると、足りないプロパティが定義した行で分かる
const draft: User = { id: 1, name: "田中", email: "t@example.com" };
save(draft);
// 文字列で届いた値は、渡す前に数値へ変換する
const fromForm = { id: "1", name: "田中", email: "t@example.com" };
save({ ...fromForm, id: Number(fromForm.id) });
1 t@example.com
1 t@example.com
そのほかの組み合わせ(unknown・Promise・enum・readonly)
function sortAsc(nums: number[]): number[] {
return nums.sort((a, b) => a - b);
}
const fixed = [3, 1, 2] as const;
sortAsc(fixed);
declare const raw: unknown;
function upper(s: string) { return s.toUpperCase(); }
upper(raw);
async function load(): Promise<string> { return "ok"; }
upper(load());
enum Color { Red = "red" }
function paint(c: Color) { return c; }
paint("red");
10-readonly.ts(5,9): error TS4104: The type 'readonly [3, 1, 2]' is 'readonly' and cannot be assigned to the mutable type 'number[]'.
10-readonly.ts(9,7): error TS2345: Argument of type 'unknown' is not assignable to parameter of type 'string'.
10-readonly.ts(12,7): error TS2345: Argument of type 'Promise<string>' is not assignable to parameter of type 'string'.
10-readonly.ts(16,7): error TS2345: Argument of type '"red"' is not assignable to parameter of type 'Color'.
| 行 | 直し方 |
|---|---|
| 5行目(readonly な配列) | 受け取る側を readonly number[] にして、中では [...nums].sort(...) のようにコピーを並べ替える |
| 9行目(unknown) | if (typeof raw === "string") で確かめてから渡す |
| 12行目(Promise) | upper(await load()) のように await を付ける |
| 16行目(文字列の enum) | paint(Color.Red) と enum のメンバーで渡す |
TypeScript 5.9.3・6.0.3と7.0.2で表示が違った箇所
同じファイルを3つの版にかけて、出力を diff で比べました。TS2345 に関係する違いは次の3点でした。5.9.3 と 6.0.3 の出力は、今回のファイルでは同じでした。
| 場面 | 5.9.3 / 6.0.3 | 7.0.2 |
|---|---|---|
| プロパティが足りないオブジェクトを渡す | TS2345 の下に「Property 'email' is missing …」が続く | TS2741 が1行目に出る |
| readonly な配列を number[] に渡す | TS2345 の下に「The type … is 'readonly' …」が続く | TS4104 が1行目に出る |
| TS2769(addEventListener の例) | Overload 1 of 2、Overload 2 of 2 の両方の理由を表示 | 「The last overload gave the following error.」として最後の候補の理由だけを表示 |
5.9.3 での出力例です。
08-object.ts(7,6): error TS2345: Argument of type '{ id: number; name: string; }' is not assignable to parameter of type 'User'.
Property 'email' is missing in type '{ id: number; name: string; }' but required in type 'User'.
番号は変わっても、伝えている内容と直し方は同じです。 検索するときは、番号だけでなく「Property … is missing」のような文の一部でも探すと、版の違いに左右されにくくなります。
直し方を選ぶ判断表
| 状況 | まず試すこと | 理由 |
|---|---|---|
| A に null / undefined が含まれる | if か早期 return で分ける | 値がないときの動きを自分で決められる |
| 値がないときの既定値が決まっている | ?? |
1行で書ける |
| A が string で B がユニオン型 | as const/satisfies/型注釈 |
値は正しく、型の広がりだけを止めればよい |
| B が never | 空配列に要素の型を書く | 推論の手がかりを足す |
| ジェネリクス関数で起きる | 型引数を明示する | 推論の結果を自分で指定できる |
| A と B がまったく別の型 | 渡している値を見直す | 変数の取り違えや、プロパティの指定もれが多い |
as 型(型アサーション)や ! を付けるとエラーは消えますが、実行時の値は変わりません。null が実際に届けば、その先で TypeError になります。値を確かめる書き方(if、??、typeof)を先に検討し、アサーションは「この時点で必ず値がある」と説明できる場所に限ると、型検査の利点を保てます。
よくある質問
Q. 「Argument of type 'string' is not assignable to parameter of type 'never'」は何が原因ですか?
A. ほとんどの場合、型注釈のない空配列 [] です。{ tags: [] } や reduce の初期値 [] は never[] と推論されるため、何も push できません。[] as string[]、プロパティの型定義、reduce<string[]> のいずれかで要素の型を伝えると解決します。
Q. 値は "GET" なのに「'string' を 'Method' に割り当てられない」と出るのはなぜですか?
A. オブジェクトのプロパティや let の変数は、あとで書き換えられる前提で string と推論されるためです。as const を付ける、satisfies で検査する、変数に : Method と型注釈を付ける、のいずれかでリテラル型を保てます。
Q. TS2345 と TS2322 はどう違いますか? A. 合わなかった場所が違います。関数呼び出しの引数なら TS2345、変数への代入・return・オブジェクトリテラルのプロパティなら TS2322 です。どちらも「左の型を右の型に合わせる」という直し方は共通です。
Q. エラー文が長くて読めません。どこを見ればよいですか? A. 1行目で A と B の全体をつかみ、次にインデントが最も深い最後の行を読みます。最後の行に「どのプロパティ・どの引数の、何の型が合わないか」が書かれています。
まとめ
- TS2345 は「渡した型 A が、受け取る型 B に合わない」。エラー文の A と B の組み合わせで原因が決まる
- A に null / undefined があれば、if・早期 return・
??で値がない場合の動きを決める - A が string で B がユニオン型なら、
as const・satisfies・型注釈でリテラル型を保つ - B が never なら空配列が原因。要素の型を先に書く
- TS2769 は内側に TS2345 と同じ文を含むので、同じ読み方で原因が分かる
- 代入・return は TS2322、引数は TS2345。オブジェクトリテラルを直接渡すと、プロパティの位置に TS2322 や TS2353 が出る
- TypeScript 7.0.2 では、プロパティ不足は TS2741、readonly 配列は TS4104 が1行目に出る(5.9.3・6.0.3 では TS2345 の下に同じ文が続く)
本記事の内容は、ご自由にお使いください。
ご利用の際は、出典として本ページへのリンクを記載いただけますようお願いします。
(記載例)出典:株式会社RJC「TS2345「引数を割り当てられません」原因と直し方の早見表」
株式会社RJC ― SI事業・SES事業・AI駆動開発。RJCは一緒に成長を楽しめる会社です。
WE ARE HIRING
RJCで一緒に開発しながら、
成長を楽しみませんか?
RJCは、Web・モバイル・AIを活用した開発プロジェクトで、テックリードやPM・PMOも活躍するシステム開発会社です。会社を知る、待遇を確かめる、話を聞いてみる。気になるところ見てみてください!
- 127日年間休日
- 12時間平均残業時間
- 毎日ガチャ遊びココロも大切にする福利厚生。アマギフなどの賞品ラインナップ!
ほかにも、チケットレストラン、書籍読み放題、2年ごとの慰労報奨(休暇 or 金一封)、11期連続の黒字決算。