TypeScript Reference ยท Error Codes Worth Knowing
TypeScript error TS2345: Argument is not assignable to parameter
Reference for TypeScript error TS2345: what Argument of type is not assignable to parameter of type means, common causes with unions and null, and the fixes.
TS2345 is the function-call twin of TS2322. The compiler found a call whose argument has a type that does not fit the corresponding parameter. The message reads Argument of type 'A' is not assignable to parameter of type 'P'.
It points at the argument, not at the function, so the fix is usually either to change the value you pass (convert, narrow, default) or to widen the parameter type if the function really can handle more.
function repeat(times: number) {} repeat("3"); // TS2345
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.
Syntax
The error attaches to a single argument expression in a call, new expression or tagged template.
| Cause | Fix |
|---|---|
| A string where a number is needed (or the reverse) | Convert with Number(), String() or parsing. |
T | undefined passed to T |
Check first, or supply a default with ??. |
| Object literal with a wrong property type | Fix that property; the nested lines name it. |
| A callback with incompatible parameters | Match the expected signature. |
| A generic inferred too narrowly | Pass explicit type arguments: f<string | number>(x). |
Related errors: wrong number of arguments is TS2554; no matching overload is TS2769.
Usage notes
- Indexing arrays returns
string | undefinedonly withnoUncheckedIndexedAccess; otherwisenames[5]is typedstring, so the first call in the second example is accepted. Thefindresult is always possiblyundefined. - Literal widening.
const mode = "fast"keeps the literal type, butlet mode = "fast"widens tostringand then cannot be passed where"fast" | "slow"is required. - Callbacks. Parameter types are checked bivariantly for methods and contravariantly for function properties under
strictFunctionTypes; mismatches show up as TS2345 on the callback argument. - Spread arguments need a tuple type.
f(...arr)with annumber[]for fixed parameters is an error (TS2556); useas constor a tuple. - Do not cast away the problem.
shout(first as string)compiles and then throws at run time whenfirstisundefined. - Overloads report the last overload's mismatch in some cases; read all overload signatures to find the one you meant.