ProjectStack
typescript

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

TypeScript detected a type mismatch — you're assigning or returning a value whose type doesn't match what's expected. This is the most common TypeScript error and means the type system caught an inconsistency that could cause a runtime bug.

Common causes

  • Assigning a string to a variable declared as a number, or vice versa
  • A function's return value doesn't match its declared return type
  • Passing a union type where only one specific member is accepted
  • Assigning null or undefined to a non-nullable type with strict null checks enabled

How to fix it

  1. Update the value to match the expected type, or loosen the declared type to accept the actual value
  2. If the type annotation is wrong, fix it: change number to string if the value is always a string
  3. Use a type guard to narrow before assigning: if (typeof value === 'string') { ... }
  4. As a last resort, use a type assertion: value as TargetType — only when you're certain of the type

Example

const count: number = 'five'; // error TS2322: Type 'string' is not assignable to type 'number'.

Assigning a string literal to a variable declared as number

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.