Skip to content
Tutorials Center
My learning

Topics

TypeScript Reference ยท tsconfig: Type Checking Options

TypeScript strictFunctionTypes option

Reference for strictFunctionTypes in TypeScript: contravariant parameter checks for function types, why method syntax stays bivariant, and how to fix errors.

strictFunctionTypes makes the compiler compare the parameters of function types contravariantly. In plain terms: a function that handles only a narrow type cannot stand in for a function that must handle a wider type, because it would be called with values it cannot cope with.

Without the flag, parameters are compared bivariantly (either direction is accepted), which is unsound but convenient. The flag is part of strict. It applies to function type literals and function-typed properties, but deliberately not to methods declared with method syntax, so that common class and interface patterns keep working.

TypeScript
type Handler = (event: string | number) => void;
const onlyStrings = (event: string) => {};
// const h: Handler = onlyStrings;   // error with strictFunctionTypes

Try it

TypeScript A narrower handler is rejected

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.

TypeScript Wider parameters are fine; methods are bivariant

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.

Syntax

JSON
{ "compilerOptions": { "strictFunctionTypes": true } }
TypeScript
type A = (x: Animal) => void;     // checked contravariantly
interface B { f: (x: Animal) => void }   // property with function type: strict
interface C { f(x: Animal): void }       // method: still bivariant
Place Parameter comparison
Function type alias or literal Contravariant (strict)
Property whose type is a function Contravariant (strict)
Method signature or method shorthand Bivariant
Constructor types Contravariant (strict)
Return types Always covariant, flag or not

Edge cases: the flag does not apply to the parameters of overloads compared by assignment inside classes in a few historical corners, and it never changes this checks. For Array<T>, methods such as push are declared as methods, which is why Array<Dog> is still assignable to Array<Animal> even though that is technically unsafe.

Usage notes

  • Rule of thumb. A function is assignable to a function type if it accepts at least what the type promises to pass in, and returns at most what the type promises to return.
  • Fixing the error. Widen the parameter type of the implementation, narrow the declared function type, or narrow at runtime inside the function (typeof or a type guard).
  • Callbacks. Event handler maps (Record<string, (e: Event) => void>) are where this error appears most. A generic or a discriminated union of handlers usually models them better.
  • Method versus property syntax changes variance, so choose deliberately; use property syntax when you want strictness.
  • in and out variance annotations on type parameters (interface Box<in T>) let you state variance explicitly and get checking against it.
  • No runtime effect. Like every checking flag it only changes the diagnostics, not the emitted JavaScript.