Borislav Hadzhiev
Last updated: Mar 27, 2022
Check out my new book
The error "An argument for 'X' was not provided" occurs when we don't provide a value for a required parameter when calling a function or class method. To solve the error, provide an argument when calling the function or mark the parameter as optional.
Here are 2 examples of how the error occurs.
class Employee { constructor(public name: string, public salary: number) { this.name = name; this.salary = salary; } } // ⛔️ Error: Expected 2 arguments, but got 1.ts(2554) // index.ts(2, 36): An argument for 'salary' was not provided. const employee = new Employee('James'); // ------------------------------------------------------------ function sum(a: number, b: number) { return a + b; } // ⛔️ Error: index.ts(12, 25): An argument for 'b' was not provided. console.log(sum(15));
The issue in the first example is - the class has 2 required parameters, but we only passed it a single argument upon instantiation.
One way to solve the error is to provide a value for all of the required parameters.
class Employee { constructor(public name: string, public salary: number) { this.name = name; this.salary = salary; } } // ✅ works now const employee = new Employee('James', 100); // ------------------------------------------------------------ function sum(a: number, b: number) { return a + b; } // ✅ works now console.log(sum(15, 25)); // 👉️ 40
Alternatively, you can mark a parameter as optional if you want to omit it when calling the function.
class Employee { // 👇️ mark `salary` as optional constructor(public name: string, public salary?: number) { this.name = name; this.salary = salary; } } const employee = new Employee('James'); console.log(employee.salary); // 👉️ undefined // ------------------------------------------------------------ // 👇️ mark `b` as optional function sum(a: number, b?: number) { return a + (b || 0); } console.log(sum(15)); // 👉️ 15
We used a question mark to mark a function parameter as optional.
undefined
.This approach allows us to omit passing the argument when calling the function.
class Employee { // 👇️ provide a default value for the `salary` parameter constructor(public name: string, public salary: number = 100) { this.name = name; this.salary = salary; } } const employee = new Employee('James'); console.log(employee.salary); // 👉️ 100 // ------------------------------------------------------------ // 👇️ provide a default value for the `b` parameter function sum(a: number, b = 100) { return a + (b || 0); } console.log(sum(15)); // 👉️ 115
Notice that we didn't even have to explicitly type the b
parameter in the
sum
function.
However, if you provide a default value for array or object parameters, you would have to type them explicitly.
function getNames(names: string[] = []) { return names; } console.log(getNames()); // 👉️ [] console.log(getNames(['Alice', 'Bob'])); // 👉️ ['Alice', 'Bob']
We explicitly typed tha names
parameter even though we set a default value for
it.
This is because if you provide an empty array as a default value, TypeScript
infers the type of the parameter to be never[]
, which is an array that will
never contain any elements.