TypeScript Reference ยท Error Codes Worth Knowing
TypeScript error TS2554: Expected N arguments, but got M
Reference for TypeScript error TS2554: calling a function with too few or too many arguments, how optional, default and rest parameters change the count, and fixes.
TS2554 reports a call with the wrong number of arguments. JavaScript quietly fills missing parameters with undefined and ignores extra arguments; TypeScript treats both as mistakes. The message is Expected 2 arguments, but got 1, or Expected 1-2 arguments, but got 3 when some parameters are optional.
The range reflects the signature: required parameters set the minimum; optional, defaulted and rest parameters stretch the maximum.
function add(a: number, b: number) { return a + b; } add(1); // TS2554
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
| Parameter kind | Effect on the argument count |
|---|---|
Required a: T |
Counts toward the minimum. |
Optional a?: T |
May be omitted; its type includes undefined. |
Default a: T = value |
May be omitted; the type inside the function is T. |
Rest ...a: T[] |
Any number of extra arguments, including none. |
this parameter |
Not an argument; it never counts. |
Rules: optional and defaulted parameters must follow required ones (a required parameter after an optional one is TS1016). A defaulted parameter before a required one forces callers to pass undefined explicitly.
Usage notes
- Callbacks are different. A function with fewer parameters can be passed where more are expected:
[1, 2].map(() => 0)is fine because the extra arguments are ignored. The reverse fails with TS2345. - Constructors and
newfollow the same rule, as do tagged templates and decorators. - Overloads report TS2575 when no overload accepts that count, or TS2769 when counts match but types do not.
- Spread calls:
f(...args)needs a tuple type with the right length; a plain array of unknown length reports TS2556. - Option objects scale better. Past three or four parameters, one object parameter (
{ a, b, c }) is easier to call correctly and to extend. - Argument order mistakes are not caught by count; give parameters distinct types, or a single options object.