TypeScript "strictFunctionTypes" is ignored for method syntax (#tilPost)

Let’s say you have this TypeScript type.

ts
type Example = {
// Method shorthand syntax
method(x: string | number): void
// Function (or object property) syntax
fn: (x: string | number) => void;
};

You might expect the function types method and fn to behave exactly the same, right? The only difference is that one is defined in method shorthand, whereas the other is defined in function syntax. Both accept an x param that could either be a string or a number

But now check this out.

ts
function fn(x: string) { /* ... */ }
 
const example: Example = {
// this is fine...
method: fn,
// this isn't...
fn: fn,
Type '(x: string) => void' is not assignable to type '(x: string | number) => void'. Types of parameters 'x' and 'x' 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 '(x: string | number) => void'. Types of parameters 'x' and 'x' are incompatible. Type 'string | number' is not assignable to type 'string'. Type 'number' is not assignable to type 'string'.
};

If you create an example object whose properties don’t match the Example type, only the function syntax will detect the type mismatch. What’s going on?

Apparently, there’s a strictFunctionTypes TypeScript config option (part of TypeScript’s strict config) that only works for the function syntax for legacy reasons.

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:

The TypeScript devs purposefully turned off a type check feature for legacy reasons. Wild!

It’s good to avoid the method syntax altogether and if you use TypeScript ESLint, there’s a rule for it.



Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis

Let's say you have this TypeScript type.

ts
type Example = {
// Method shorthand syntax
method(x: string | number): void
// Function (or object property) syntax
fn: (x: string | number) => void;
};

You might expect the function types method and fn to behave exactly the same, right? The only difference is that one is defined in method shorthand, whereas the other is defined in function syntax. Both accept an x param that could either be a string or a number

But now check this out.

ts
function fn(x: string) { /* ... */ }
 
const example: Example = {
// this is fine...
method: fn,
// this isn't...
fn: fn,
Type '(x: string) => void' is not assignable to type '(x: string | number) => void'. Types of parameters 'x' and 'x' 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 '(x: string | number) => void'. Types of parameters 'x' and 'x' are incompatible. Type 'string | number' is not assignable to type 'string'. Type 'number' is not assignable to type 'string'.
};

If you create an example object whose properties don't match the Example type, only the function syntax will detect the type mismatch. What's going on?

Apparently, there's a strictFunctionTypes TypeScript config option (part of TypeScript's strict config) that only works for the function syntax for legacy reasons.

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:

The TypeScript devs purposefully turned off a type check feature for legacy reasons. Wild!

It's good to avoid the method syntax altogether and if you use TypeScript ESLint, there's a rule for it.


Reply to Stefan


This content originally appeared on Stefan Judis Web Development and was authored by Stefan Judis


Print Share Comment Cite Upload Translate Updates
APA

Stefan Judis | Sciencx (2025-02-28T23:00:00+00:00) TypeScript "strictFunctionTypes" is ignored for method syntax (#tilPost). Retrieved from https://www.scien.cx/2025/02/28/typescript-strictfunctiontypes-is-ignored-for-method-syntax-tilpost/

MLA
" » TypeScript "strictFunctionTypes" is ignored for method syntax (#tilPost)." Stefan Judis | Sciencx - Friday February 28, 2025, https://www.scien.cx/2025/02/28/typescript-strictfunctiontypes-is-ignored-for-method-syntax-tilpost/
HARVARD
Stefan Judis | Sciencx Friday February 28, 2025 » TypeScript "strictFunctionTypes" is ignored for method syntax (#tilPost)., viewed ,<https://www.scien.cx/2025/02/28/typescript-strictfunctiontypes-is-ignored-for-method-syntax-tilpost/>
VANCOUVER
Stefan Judis | Sciencx - » TypeScript "strictFunctionTypes" is ignored for method syntax (#tilPost). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/02/28/typescript-strictfunctiontypes-is-ignored-for-method-syntax-tilpost/
CHICAGO
" » TypeScript "strictFunctionTypes" is ignored for method syntax (#tilPost)." Stefan Judis | Sciencx - Accessed . https://www.scien.cx/2025/02/28/typescript-strictfunctiontypes-is-ignored-for-method-syntax-tilpost/
IEEE
" » TypeScript "strictFunctionTypes" is ignored for method syntax (#tilPost)." Stefan Judis | Sciencx [Online]. Available: https://www.scien.cx/2025/02/28/typescript-strictfunctiontypes-is-ignored-for-method-syntax-tilpost/. [Accessed: ]
rf:citation
» TypeScript "strictFunctionTypes" is ignored for method syntax (#tilPost) | Stefan Judis | Sciencx | https://www.scien.cx/2025/02/28/typescript-strictfunctiontypes-is-ignored-for-method-syntax-tilpost/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.