Skip to article content
FrontendXLesson 20 / 40

Assignability and variance

Go from the first annotated variable to writing your own conditional, mapped and infer-based utility types, while keeping a clear line between compile-time checking and runtime behavior.

advanced · about 50 minutes · 3 exercises

  • Predict
  • Explain
  • Implement

What you will be able to do

#
  • Explain covariance, contravariance and invariance in plain terms.
  • Predict when a function type is assignable to another.
  • Use readonly and in/out variance annotations deliberately.

Overview

#

Assignability

TypeScript uses structural, mostly covariant assignability: a value is assignable if its type has at least the required members with compatible types.

snippet.ts
type Animal = { name: string };
type Dog = { name: string; bark(): void };
const d: Animal = { name: "Rex", bark: () => {} }; // Dog assignable to Animal

Function parameters are contravariant under strictFunctionTypes

A function with a wider parameter is assignable to one with a narrower parameter.

snippet.ts
type Handler = (e: MouseEvent) => void;
type General = (e: Event) => void;
const g: General = () => {};
const h: Handler = g; // ok: General handles a wider Event

Arrays and readonly

Array<T> is treated covariantly for convenience, which is unsound but practical. readonly T[] is safely covariant.

Variance annotations

snippet.ts
interface Producer<out T> { get(): T }
interface Consumer<in T> { set(value: T): void }

out asserts covariance, in asserts contravariance. They document intent and let the checker compute variance faster; an incorrect annotation is an error.

Why it matters

Variance explains surprising assignability errors, especially around callbacks, event handlers and generic containers.

Exercises

#

Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.

  1. Exercise 1Predict

    Is `g` assignable to `h`? ```ts type H = (e: MouseEvent) => void; type G = (e: Event) => void; const g: G = () => {}; const h: H = g; ```

    Choose the best answer
  2. Exercise 2Implement

    Write a generic `Producer<out T>` with `get(): T` and a generic `Consumer<in T>` with `set(value: T): void`.

    starter.typescript
    export interface Producer<out T> {
      // your code
    }
    
    export interface Consumer<in T> {
      // your code
    }
    

    Reveal the reference answer
    reference.typescript
    export interface Producer<out T> {
      get(): T;
    }
    
    export interface Consumer<in T> {
      set(value: T): void;
    }
    

    Authored expected output: Compile-time: Producer is covariant in T and Consumer is contravariant in T. No runtime output.

    out documents a type that only produces T; in documents one that only consumes T.

  3. Exercise 3Explain

    Explain why `Array<T>` is covariant in TypeScript even though it is mutable.

    Reveal the reference answer
    reference.typescript
    A mutable array is invariant in a sound type system, because writing through a covariant alias would let a wrong element type in. TypeScript allows covariant assignment for arrays anyway because it matches common JavaScript practice and keeps the language usable. The safe alternative is a readonly array, which cannot be written through, so covariance is sound.

    Authored expected output: A written answer naming the deliberate unsoundness and readonly as the safe alternative.

    Convenience was chosen over strict soundness for mutable arrays.

Common misconceptions

#
  • TypeScript is nominally typed.
  • Function parameters are covariant.
  • in and out change runtime behavior.

Follow-up practice

#
  • typescript-strict-migration

Related resources

#

Your progress

#

Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.

Verification: authored. Answers and outputs on this page are authored reference material, not executed results.

© 2026 FrontendX. All rights reserved.