Argument type 'void' is not assignable to parameter of type

avatar

Borislav Hadzhiev

Wed Mar 23 20222 min read

banner

Photo by Phil Plante

Argument type 'void' is not assignable to parameter of type #

The error "Argument of type 'void' is not assignable to parameter of type" occurs when we forget to return from a function and pass a void argument to the calling function. To solve the error, make sure you return a value of the expected type, before passing it to the caller.

argument type void not assignable parameter type

Here is an example of how the error occurs.

index.ts
// 👇️ function sum(a: number, b: number): void function sum(a: number, b: number) { const result = a + b; // 👉️ forgot to return value } function getNumber(num: number) { return num; } // ⛔️ Error: Argument of type 'void' is not assignable to // parameter of type 'number'.ts(2345) getNumber(sum(10, 10));

We forgot to return a value from the sum function, so it implicitly has a return type of void.

The void type represents the return value of a function, which doesn't return a value.

When you don't return a value from a function, you implicitly return undefined, and the function's return type is inferred to be void.

The error message "Argument of type 'void' is not assignable to parameter of type" means that we are passing an argument of type void to a function that expects a parameter of a different type.

To solve the error, make sure to return a value from your functions.

index.ts
// 👇️ function sum(a: number, b: number): number function sum(a: number, b: number) { const result = a + b; return result; // 👈️ explicitly return } function getNumber(num: number) { return num; } console.log(getNumber(sum(10, 10))); // 👉️ 20

Now the sum function has a return type of number.

Note that the return type of the sum function has to be compatible with the type of the parameter in the getNumber function.

A helpful way to debug this is to explicitly set the function's return type.

index.ts
// 👇️ explicitly set function's return type function sum(a: number, b: number): number { const result = a + b; return result; // 👈️ explicitly return } // 👇️ explicitly set function's return type function getNumber(num: number): number { return num; } console.log(getNumber(sum(10, 10))); // 👉️ 20
We explicitly set the return type of the functions. This is useful, because if the functions don't return a value of the specified type, we would get a helpful error.

If you're using an implicit return with an arrow function, make sure the function returns the expected value.

index.ts
// 👇️ implicit return of object 👇️ const getPerson = () => ({ name: 'Alice', language: 'TypeScript', }); // 👇️ implicit return 👇️ const sum = (a: number, b: number): number => a + b; function getNumber(num: number) { return num; } console.log(getNumber(sum(10, 10))); // 👉️ 20

You might also be using callback functions that expects that you return a value of a specific type. Hover over the function and make sure to return a value of the correct type.

Sometimes you have complex functions with many conditionals and different code paths. Note that all of the function's code paths have to return a value.

Use the search field on my Home Page to filter through my more than 1,000 articles.