Expected 0 arguments, but got 1 error in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 18, 2022

banner

Photo from Unsplash

Expected 0 arguments, but got 1 error in TypeScript #

The error "Expected 0 arguments, but got 1" occurs when we pass an argument to a function that doesn't take any arguments. To solve the error define and type the arguments of the function inline or update the function's type in the specific interface or type alias.

expected 0 arguments but got 1

Here are 2 examples of how the error occurs.

index.ts
function greet() { return 'hello world'; } // ⛔️ Error: Expected 0 arguments, but got 1.ts(2554) greet('James Doe'); // --------------------------------------- type GreetAgain = () => string; const greetAgain: GreetAgain = () => { return 'hello again'; }; // ⛔️ Error: Expected 0 arguments, but got 1.ts(2554) console.log(greetAgain('James'));

The greet function in the example does not define any arguments, but we try to call it with one and get the error.

To solve the error in this situation, we have to define and type the arguments on the function.

index.ts
function greet(name: string) { return `Hello ${name}`; } // 👇️ "Hello James Doe" console.log(greet('James Doe'));

We defined the name parameter and set it to have a type of a string.

If you don't know the type of the parameter and simply want to disable type checking for it, set its type to any.

index.ts
function greet(name: any) { return `Hello ${name}`; } // 👇️ "Hello James Doe" console.log(greet('James Doe'));
The any type disables type checking, so you could pass a value of any type to the function without getting a type checking error.

Here is an example of how you would type an object parameter.

index.ts
function getEmployee({ name, age, salary, }: { name: string; age: number; salary: number; }) { return { name, age, salary }; } const result = getEmployee({ name: 'Alice', age: 30, salary: 100, });

We separate the parameter definition and its type with a colon.

If your function definition gets busy, use a type alias or an interface.

index.ts
type EmployeeProps = { name: string; age: number; salary: number; }; function getEmployee({ name, age, salary }: EmployeeProps) { return { name, age, salary }; }

Sometimes, you might have an interface that contains a function property.

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

The increaseSalary function in the example takes a parameter of type number and returns a number.

You can also define a function type using a type alias.

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

The greet function takes a name parameter of type string and returns a parameter that is a string.

Notice that when using this approach we didn't have to type the function's parameter inline - we have already declared the type of the parameter in the type alias.

Conclusion #

The error "Expected 0 arguments, but got 1" occurs when we pass an argument to a function that doesn't take any arguments. To solve the error define and type the arguments of the function inline or update the function's type in the specific interface or type alias.

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.