Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท Concepts and Comparisons

Assignability rules in TypeScript

The rules behind TS2322 and TS2345: when a value of one type can be assigned to another, including unions, optional members, functions and literals.

Assignability answers one question: may a value of type S be used where type T is expected? Variable initializers, assignments, function arguments, return statements and property writes all ask it. When the answer is no, you see errors such as TS2322 (Type 'X' is not assignable to type 'Y') or TS2345 for arguments.

The rule is directional. S to T can work while T to S fails. Think of T as a promise about what the consumer will do with the value.

TypeScript
const s: string = "hello";
const u: string | number = s; // string is assignable to the wider union

Try it

TypeScript Which direction works

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 rejected assignment

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.

The main rules

  • Identity and subtypes: a type is assignable to itself and to any supertype. never is assignable to everything; nothing but never is assignable to never.
  • Unions: S is assignable to a union if it is assignable to at least one member. A union S is assignable to T only if every member is.
  • Intersections: S is assignable to A & B only if it is assignable to both.
  • Objects: the source needs all required target properties with assignable types; extra properties are fine except on fresh literals. Optional target properties may be missing.
  • Optional and readonly: readonly is not checked for assignability, so a readonly property accepts a mutable one and the reverse. An optional property in the source cannot satisfy a required one in the target.
  • Functions: parameters are compared by direction depending on the strictness flag; return types are covariant; fewer parameters are fine; see strictFunctionTypes.
  • Literals: "a" is assignable to string; string is not assignable to "a".
  • any is assignable to and from nearly everything (but not to never). unknown accepts anything but is assignable only to unknown and any.
  • Enums: numeric enums accept numbers; string enums accept only their own members.

Usage notes

  • Assignable does not mean identical. Use mutual assignability checks in type tests, or an Equal helper, when you need identity.
  • Arrays are covariant, so Dog[] is assignable to Animal[] even though writing an Animal into it could break the dog array. This is a deliberate unsoundness; see Soundness.
  • Method parameters are bivariant, even under strictFunctionTypes, which only tightens function-typed properties.
  • Generics compare by variance of each type parameter; see Variance.
  • Reading error details: TypeScript prints the chain of nested mismatches; the last line is the real culprit.