Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Error Codes Worth Knowing

TypeScript error TS2322: Type is not assignable to type

Reference for TypeScript error TS2322: what Type is not assignable to type means, the usual causes (literals, unions, null, objects) and how to fix each one.

TS2322 is the most common TypeScript error. It says that a value of one type is being put somewhere that expects a different type: a variable, a property, a return value, an array element. The message always has the form Type 'A' is not assignable to type 'B', and often a second line explains exactly which part does not fit.

The compiler checks assignability by shape. A value is accepted when everything required by the target type is present with a compatible type. When it is not, TS2322 appears, and the indented lines under it point at the first mismatch.

TypeScript
const age: number = "42"; // TS2322

Try it

TypeScript Simple mismatches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript A mismatch deep inside an object

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

TypeScript Fixed versions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

Syntax

The error has no syntax of its own; it appears wherever an assignment-like check happens.

Where it appears Example
Variable initializer or assignment const n: number = "x"
Return statement function f(): string { return 1; }
Object or array literal member { role: "owner" } for a union of literals
Callback result A function passed where another return type is expected
Class member overriding a base class See TS2416

Related message lines: Type 'X' is not assignable to type 'Y'. Type 'Z' is not assignable to type 'W'. The last line is the real cause.

Usage notes

  • Read the last line first. Nested messages narrow down to the one property or union member that fails.
  • Widened literals. let role = "member" has type string, so it is not assignable to "admin" | "member". Use const, as const, or an annotation.
  • null and undefined. Under strictNullChecks, string | undefined is not assignable to string. Narrow it with a check, or give a default with ??.
  • Excess properties in a fresh object literal are reported as TS2353 instead; missing ones as TS2741.
  • Do not cast to silence it. as string or as any hides the mismatch; convert the value (Number(x), String(x), parsing) or fix the declared type.
  • Generics. When a generic parameter infers a narrower type than you want, specify it explicitly: new Set<number | string>().
  • Function types are compared by parameters (contravariantly under strictFunctionTypes) and return type; a callback returning Promise<void> is not a void function in every position.