TSConfig
strictFunctionTypes
When enabled, this flag causes functions parameters to be checked more correctly.
Here’s a basic example with strictFunctionTypes off:
tsTryfunctionfn (x : string) {console .log ("Hello, " +x .toLowerCase ());}typeStringOrNumberFunc = (ns : string | number) => void;// Unsafe assignmentletfunc :StringOrNumberFunc =fn ;// Unsafe call - will crashfunc (10);
With strictFunctionTypes on, the error is correctly detected:
tsTryfunctionfn (x : string) {console .log ("Hello, " +x .toLowerCase ());}typeStringOrNumberFunc = (ns : string | number) => void;// Unsafe assignment is preventedletType '(x: string) => void' is not assignable to type 'StringOrNumberFunc'. Types of parameters 'x' and 'ns' are incompatible. Type 'string | number' is not assignable to type 'string'. Type 'number' is not assignable to type 'string'.2322Type '(x: string) => void' is not assignable to type 'StringOrNumberFunc'. Types of parameters 'x' and 'ns' are incompatible. Type 'string | number' is not assignable to type 'string'. Type 'number' is not assignable to type 'string'.: func StringOrNumberFunc =fn ;
During development of this feature, we discovered a large number of inherently unsafe class hierarchies, including some in the DOM. Because of this, the setting only applies to functions written in function syntax, not to those in method syntax:
tsTrytypeMethodish = {func (x : string | number): void;};functionfn (x : string) {console .log ("Hello, " +x .toLowerCase ());}// Ultimately an unsafe assignment, but not detectedconstm :Methodish = {func :fn ,};m .func (10);