On How TypeScript Handles Variance

This lesson gives the theory of how TypeScript handles variance.

Type system variances

TypeScript variances use the nomenclature of another language, which is confusing by nature. It’s not something you must master since the compiler will stop you from doing anything crazy.

However, it’s good to understand before having the compiler manage the complexity for us. Before getting started, let’s create some interfaces to examine how TypeScript handles (or doesn’t handle) covariant, contravariant, bivariant, and invariant types.

We will create three interfaces which will be all inherited in the chain. So, InterfaceA inherits InterfaceB which inherits InterfaceC.

interface InterfaceA {
  a1: number;

interface InterfaceB extends InterfaceA {
  b2: string;

interface InterfaceC extends InterfaceB {
  c3: boolean;

Type 1: Invariance

The concept of invariance is the simplest of the type systems. This means the variable’s type is the only type accepted.

Invariance accepts supertypes; it also does not accept subtypes.

Get hands-on with 1200+ tech skills courses.