Variance in TypeScript
Variance in TypeScript — the core idea
Variance describes how assignability changes when a type appears in different positions. Under strictFunctionTypes, a function accepting a narrow input is not generally safe where a broader input may be supplied. Callback parameter checks protect the caller's freedom to pass any value allowed by the contract.
What you’ll learn
- Explain variance in typescript with a concrete use case.
- Run the example and account for every line of its output.
- Recognize the difference between compile-time checking and runtime behavior.
Understand the rules and when to use them
Reading the code: A function that accepts any Animal can safely process a Dog, because Dog contains the required name. Reversing the direction is unsafe: a dog-only function might call bark on an ordinary Animal.
Keep the boundary clear: Method parameter positions and mutable arrays have special compatibility rules; do not assume every generic container is perfectly safe under all assignments.
See the idea as a flow
Run the complete TypeScript example
Save the code as variance.ts. The result below was captured after compiling this exact example with strict TypeScript checking and executing the emitted JavaScript in Node.js.
TypeScript code
type Animal = { name: string };
type Dog = Animal & { bark(): string };
const animals: Animal[] = [{ name: "Fox" }];
const describe: (a: Animal) => string = a => a.name;
const dog: Dog = { name: "Rex", bark: () => "woof" };
console.log(describe(dog));
console.log(animals.map(describe).join(","));Verified output
Rex
FoxBuild: npx tsc --strict --target es2022 --module commonjs --lib es2022,dom,esnext.disposable variance.ts; run: node variance.js. The exact filename may be changed freely. When a multi-file project exists, prefer its tsconfig.json and build scripts.
Trace the example line by line
A function that accepts any Animal can safely process a Dog, because Dog contains the required name. Reversing the direction is unsafe: a dog-only function might call bark on an ordinary Animal.
The output shown above comes from the initialized values in this program. Replacing a type annotation alone does not transform a JavaScript value.
Avoid this common mistake
Method parameter positions and mutable arrays have special compatibility rules; do not assume every generic container is perfectly safe under all assignments.
Check both the TypeScript diagnostic and the emitted JavaScript behavior. A program can compile correctly and still need runtime validation or error handling.
Practice independently
Try to assign a (dog: Dog) => string function to a variable typed (animal: Animal) => string with strictFunctionTypes enabled.
Before running your change, write down the type you expect TypeScript to infer and the output you expect JavaScript to print. Then check both predictions.
Check your understanding
What to remember
Variance describes how assignability changes when a type appears in different positions.
Try the practice change and re-run the exact program to test your understanding.
Reaching the recap and completing the quizzes are separate milestones.