strictFunctionTypes
Parameters compare the other way round
A function that accepts less cannot stand in for one that must accept more — obvious when stated, and unchecked without this flag.
- Since
- TypeScript 2.6
- strict
- In strict
- In your tsconfig
"strictFunctionTypes": true- Compiled with
"strict": true
The same snippet both times. Only the option changed.
With it off
"strictFunctionTypes": falsetype Handler = (value: string | number) => void declare const narrow: (value: string) => void export const handler: Handler = narrow
Compiles clean
With it on
"strictFunctionTypes": truetype Handler = (value: string | number) => void declare const narrow: (value: string) => void export const handler: Handler = narrow
Emits TS2322
Why the compiler bothers
Return types compare covariantly, which everyone expects. Parameters compare contravariantly, which nobody does until it is pointed out: the replacement has to handle every input the original promised to. TypeScript checked parameters bivariantly for years because real code — especially the DOM — relies on it, and this flag turns on the sound rule for function types while deliberately leaving method parameters bivariant, which is why `interface A { f(x: string): void }` is still accepted where the arrow form is not.
Takeaway
Write callbacks as arrow-typed properties, not methods, if you want them checked.
Where to go next
22 options

