Type 'void' is not assignable to type in TypeScript [Fixed]

Borislav Hadzhiev

Last updated: Feb 27, 2024

Reading timeยท5 min

- Type 'void' is not assignable to type in TypeScript
- Argument type 'void' is not assignable to parameter of type

If you got the error

"Argument of type 'void' is not assignable to parameter of type", click on the second subheading.

**The "Type 'void' is not assignable to type" TypeScript error occurs when we
forget to return a value from a function, so the function gets an implicit
return type of void.**

**To solve the error, make sure you return a value of the correct type from your
functions before the assignment.**

Here is an example of how the error occurs.

index.ts

`// ๐๏ธ const sum: (a: number, b: number) => void const sum = (a: number, b: number) => { const result = a + b; // ๐๏ธ function doesn't return anything }; // Error: โ๏ธ Type 'void' is not assignable to type 'number'.ts(2322) const num: number = sum(50, 50);`

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 type of a function that 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 "Type 'void' is not assignable to type 'number'" means that we
have a value that expects an assignment of type `number`

and we're trying to
assign a value of type `void`

to it.

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

index.ts

`const sum = (a: number, b: number) => { const result = a + b; return result; // ๐๏ธ explicitly return }; const num: number = sum(50, 50); console.log(num); // ๐๏ธ 100`

The code for this article is available on GitHub

Now the `sum`

function has a return type of `number`

.

Note that the return type of the function has to be compatible with the type of
the `num`

variable.

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

index.ts

`// โ๏ธ Error: A function whose declared type // is neither 'void' nor 'any' must return a value.ts(2355) const sum = (a: number, b: number): number => { const result = a + b; };`

We explicitly set the function's return type to `number`

but forgot to return a
value, so TypeScript informs us that we must return a value from the function.

If you have an implicit return in an arrow function, make sure the function returns the expected value.

index.ts

`// ๐๏ธ implicit return with objects const getObj = () => ({ name: 'Bobby Hadz', country: 'Chile', }); // ๐๏ธ implicit return with primitives const getNum = (a: number, b: number) => a + b;`

The code for this article is available on GitHub

The same error also occurs when working with class methods.

index.ts

`class DoMath { sum(a: number, b: number) { const result = a + b; } } const m = new DoMath(); // โ๏ธ Error: Type 'void' is not // assignable to type 'number'.ts(2322) const num: number = m.sum(10, 15);`

The solution is the same - make sure that the class method returns a value of the expected type.

You might also be using callback functions that expect you to 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.

Want to learn more about using the void type in TypeScript? Check out these resources: Property does not exist on type void in TypeScript,Did you forget to include 'void' in your type argument to 'Promise'.

**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.**

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 a 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 type of a function that 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`

The code for this article is available on GitHub

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 the function's return type function sum(a: number, b: number): number { const result = a + b; return result; // ๐๏ธ explicitly return } // ๐๏ธ explicitly set the 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: 'Bobby Hadz', 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`

The code for this article is available on GitHub

You might also be using callback functions that expect you to 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.

You can learn more about the related topics by checking out the following tutorials:

- Function lacks ending return statement Error in TS
- Declare functions returning Object or Array in TypeScript
- Declare a function with a Promise return type in TypeScript
- How to Declare a Function that throws an Error in TypeScript
- Did you forget to include 'void' in your type argument to 'Promise'
- An expression of type 'void' cannot be tested for truthiness
- Property does not exist on type void in TypeScript
- Property does not exist on type '{}' in TypeScript