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.
const s: string = "hello"; const u: string | number = s; // string is assignable to the wider union
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.
The main rules
- Identity and subtypes: a type is assignable to itself and to any supertype.
neveris assignable to everything; nothing butneveris assignable tonever. - Unions:
Sis assignable to a union if it is assignable to at least one member. A unionSis assignable toTonly if every member is. - Intersections:
Sis assignable toA & Bonly 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:readonlyis not checked for assignability, so areadonlyproperty 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 tostring;stringis not assignable to"a". anyis assignable to and from nearly everything (but not tonever).unknownaccepts anything but is assignable only tounknownandany.- 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
Equalhelper, when you need identity. - Arrays are covariant, so
Dog[]is assignable toAnimal[]even though writing anAnimalinto 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.