ProjectStack
typescript

TS2322: Type 'void' is not assignable to type 'X'

You're using the return value of a function that returns void — either by storing it in a variable, passing it to another function, or assigning it somewhere. Functions that return void produce no useful return value and the result cannot be used as data.

Common causes

  • Storing the result of a void-returning function: const result = array.forEach(...)
  • Passing a void-returning callback where a value-returning callback is expected
  • Chaining methods on a void result: arr.forEach() returns void, not the array
  • A function was changed to return void but its return value is still being used

How to fix it

  1. Don't capture the return value of void functions — just call without assignment
  2. Use a value-returning method instead: map() returns values, forEach() does not
  3. Restructure the code to not depend on the return value of the void function
  4. If the function should return a value, update its implementation and return type

Example

const result: string[] = ['a', 'b'].forEach(x => x.toUpperCase()); // error TS2322: Type 'void' is not assignable to type 'string[]'.

Assigning the result of forEach, which returns void, to a typed string[] variable

Browse more errors

The Developer Hub covers 150+ errors across Git, npm, Node.js, Python, TypeScript, and Docker — with plain-English explanations and fix steps.