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.
const age: number = "42"; // TS2322
Try it
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.
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.
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 typestring, so it is not assignable to"admin" | "member". Useconst,as const, or an annotation. nullandundefined. UnderstrictNullChecks,string | undefinedis not assignable tostring. 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 stringoras anyhides 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 returningPromise<void>is not avoidfunction in every position.
Related
- TS2345
- TS2741
- TS2353
- Tutorial: Type compatibility, Literal types