Expected 1 argument, but got 0 error in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 23, 2022

banner

Photo from Unsplash

Expected 1 argument, but got 0 error in TypeScript #

The error "Expected 1 argument, but got 0" occurs when we invoke a function that takes 1 parameter, without passing it any arguments. To solve the error, pass the required argument to the function, provide a default value for it or mark the parameter as optional.

expected 1 argument but got 0

Here is an example of how the error occurs.

index.ts
function getMessage(message: string) { return message || ''; } // ⛔️ Error: Expected 1 arguments, but got 0.ts(2554) // index.ts(1, 21): An argument for 'message' was not provided. getMessage();

The getMessage function takes 1 parameter, but we tried to invoke it without passing any, which caused the error.

One way to solve the error is to pass an argument of the correct type to the function.

index.ts
function getMessage(message: string) { return message || ''; } // 👇️ call function with parameter getMessage('hello world');

If you don't want to always have to provide the argument when calling the function, set a default value for it.

index.ts
function getMessage(message = 'hello world') { return message || ''; } getMessage();

We provided a default value for the message parameter, so now we are able to call the getMessage function without passing it any parameters.

This implicitly marks the message parameter as optional and sets its type to string. TypeScript is able to infer the type of the parameter based on the type of the default value.

If there is no default value that suits your use case, you can mark the parameter as optional.

index.ts
function getMessage(message?: string) { return message || ''; } getMessage();

We used a question mark to set the message parameter to optional.

This means that we are able to call the getMessage function without passing it an argument for message, which would set its value to undefined.

If your function takes an object as a parameter, you can provide a default value for the entire object.

index.ts
type Person = { name: string; age: number; }; function getPerson(person: Person = { name: '', age: 0 }) { return person; } getPerson();

If the getPerson function is called without a value for the person parameter, it will default to the specified object.

You can also use destructuring to not have to access the properties on the person object.

index.ts
type Person = { name: string; age: number; }; function getPerson({ name, age }: Person = { name: '', age: 0 }) { return { name, age }; } getPerson();

If you want to not have to set a default value for each property in the object, you can mark some or all of the properties as optional.

index.ts
type Person = { name?: string; // 👈️ optional age?: number; // 👈️ optional }; function getPerson(person: Person = {}) { return person; } getPerson();

All of the properties in the Person type are marked as optional, so we are able to set an empty object as the default value of the person parameter.

If you have an interface that contains a function property, the syntax is very similar.

index.ts
interface Employee { increaseSalary: (salary?: number) => number; } const emp: Employee = { increaseSalary(salary = 100) { return salary + 123; }, }; console.log(emp.increaseSalary()); // 👉️ 246

We defined an Employee interface that has an increaseSalary property of type function.

We marked the salary property as optional, so the caller of the function is not required to provide it.

We set a default value of 100 for the salary property in the increaseSalary function.

The same syntax can be used if you used a type alias to type your function.

index.ts
type Greet = (name?: string) => string; const greet: Greet = (name = 'James Doe') => { return `Hello ${name}`; }; console.log(greet()); // 👉️ "Hello James Doe"

The greet function has a name parameter that is marked as optional.

We've set a default value for the parameter, so the caller is not required to provide it.

Conclusion #

The error "Expected 1 argument, but got 0" occurs when we invoke a function that takes 1 parameter, without passing it any arguments. To solve the error, pass the required argument to the function, provide a default value for it or mark the parameter as optional.

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.