Sub topic 12
strictFunctionTypes and Method Bivariance
This chapter assumes you know how assignability works for object types and function types, and that you have read the previous chapter on covariance, contravariance, and invariance. You should be comfortable reading TypeScript errors such as "Type X is not assignable to type Y".
The problem: parameters are contravariant, but JavaScript code is not
A function (a: Animal) => void can safely stand in for (d: Dog) => void, because every Dog is an Animal. The reverse is unsafe: a function that calls d.bark() would receive a plain Animal that cannot bark. This is contravariance of parameters.
Before TypeScript 2.6, the compiler did not enforce this. It checked function parameters bivariantly: a parameter type was acceptable if the source parameter was assignable to the target parameter or the target to the source. This was convenient for existing JavaScript patterns such as event handlers, but it allowed unsound code. The strictFunctionTypes flag, which is part of strict, turns on contravariant checking for most function types.
What the flag changes
With strictFunctionTypes enabled, parameters of function type comparisons are checked contravariantly. Return types are always checked covariantly.
interface Animal { name: string }
interface Dog extends Animal { bark(): void }
declare let animalFn: (a: Animal) => void;
declare let dogFn: (d: Dog) => void;
dogFn = animalFn; // OK: parameters are contravariant
animalFn = dogFn; // Error with strictFunctionTypes (part of strict)The first assignment is safe because animalFn only uses what every Animal has. The second fails because dogFn may call bark, and callers of animalFn may pass any Animal.
Without the flag, both lines compile. The second one is the unsound case the flag exists to catch.
Method syntax stays bivariant
The flag does not apply to everything. TypeScript exempts declarations written with method syntax. This covers methods in interfaces, in type literals, and in classes. Only function-typed properties, function type aliases, and function expressions assigned to them get the strict check.
interface Animal { name: string }
interface Dog extends Animal { bark(): void }
interface MethodStyle { handle(a: Animal): void }
interface PropertyStyle { handle: (a: Animal) => void }
const impl = { handle(d: Dog) { d.bark(); } };
const m: MethodStyle = impl; // OK: method parameters stay bivariant
const p: PropertyStyle = impl; // Error under strictFunctionTypesHere impl.handle expects a Dog, but both targets promise to pass any Animal. PropertyStyle rejects the assignment. MethodStyle accepts it, and a call such as m.handle({ name: "cat" }) would then crash at runtime on d.bark().
The comparison is decided by how the target member is declared. The source impl uses method shorthand in both cases, so the difference comes from MethodStyle and PropertyStyle.
| Declaration form | Parameter check under `strictFunctionTypes` |
|---|---|
| `handle(a: T): void` (method) | Bivariant |
| `handle: (a: T) => void` (property) | Contravariant |
| `type F = (a: T) => void` | Contravariant |
| Constructor declarations | Bivariant |
Why methods are exempt
The exemption is deliberate. Many built-in types and a great deal of existing code rely on bivariant methods. The clearest example is Array<T>. Its methods, such as push(...items: T[]), are declared with method syntax. If they were checked contravariantly, Dog[] would not be assignable to Animal[], since push on Dog[] accepts only Dog. Treating arrays as covariant is practical, and the exemption is what permits it.
interface Animal { name: string }
interface Dog extends Animal { bark(): void }
const dogs: Dog[] = [];
const animals: Animal[] = dogs; // OK: arrays are treated as covariant
animals.push({ name: "cat" }); // compiles, but dogs now holds a non-DogThe DOM works the same way. Types such as NodeList and event-related interfaces have methods whose parameters would become incompatible if checked strictly. The TypeScript team chose to keep the common hierarchy-based assignments working over full soundness.
Seeing the three layers
The check flows from the flag, to the declaration form, to the result.
Choosing between method and property syntax
Pick the form according to what the member promises.
- Use property syntax (
handle: (a: Animal) => void) when you want the stricter check. This suits callbacks and handlers that callers supply, where a wrongly narrow parameter is a real bug. - Use method syntax (
handle(a: Animal): void) when the type is meant to be extended or subclassed with narrower parameters, or when you need a wide hierarchy of types to remain assignable to each other.
Property syntax also changes other behavior. A function-typed property can be reassigned on an object, and subclasses cannot override it as a method. Check that these side effects are acceptable before switching an existing interface.
Common errors and gotchas
- Turning on
strictsurfaces new errors. Callback assignments that compiled before now fail. Fix them by widening the callback parameter to the declared type, then narrowing inside with a check, rather than by loosening the target. - Bivariance is not a general fallback. Only the target's declaration form matters. Wrapping a strict callback in an interface with method syntax can hide the error, which defeats the check.
- Parameter count is a separate rule. A source with fewer parameters is always allowed, regardless of variance. A source that requires more parameters than the target supplies is an error.
- Return types are never bivariant. Under every setting, the source's return type must be assignable to the target's.
- Generic callbacks interact with inference. When a type parameter appears in a callback parameter, the compiler infers the variance of that parameter from usage. Declaring such a callback with method syntax can make the parameter bivariant, which changes what can be assigned.
Check your understanding
Exercise
Given interface A { run(x: Animal): void } and interface B { run: (x: Animal) => void }, and a value v = { run(x: Dog) {} }, which of const a: A = v and const b: B = v compile under strict, and why?
Show answer
const a: A = v compiles and const b: B = v fails. A.run is declared with method syntax, so its parameter is checked bivariantly, and Dog is assignable to Animal, which satisfies one direction. B.run is a function-typed property, so strictFunctionTypes checks the parameter contravariantly. Animal is not assignable to Dog, so the assignment is rejected.
Exercise
Why does Dog[] remain assignable to Animal[] even with strictFunctionTypes enabled?
Show answer
The methods of Array<T>, such as push, are declared with method syntax, so their parameters are checked bivariantly. This lets arrays behave covariantly. The cost is that you can push a non-Dog through an Animal[] alias without a compile error.