Solve - An argument for 'X' was not provided in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 27, 2022

banner

Check out my new book

Solve - An argument for 'x' was not provided in TypeScript #

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.

an-argument-for-was-not-provided

Here are 2 examples of how the error occurs.

index.ts
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.

The function in the second example also requires 2 arguments, but we only provided 1.

One way to solve the error is to provide a value for all of the required parameters.

index.ts
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.

index.ts
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.

This means that the parameter can either be of the specified type or be undefined.

This approach allows us to omit passing the argument when calling the function.

You can also specify a default value for a function parameter if you want to omit it when calling the function.
index.ts
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.

TypeScript is able to infer its type based on the provided default value.

However, if you provide a default value for array or object parameters, you would have to type them explicitly.

index.ts
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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.