You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The behavior of this in methods is one of the last features of JavaScript that is both unintuitive and not checked for by TypeScript. It is commonplace for both novice and experienced TypeScript programmers to accidentaly create a method (instead of an instance function) and then pass it as a callback. Currently, the only way to find out that one has made such a mistake is at runtime (yikes!).
I propose two small changes in the typechecking system which could be introduced as a strict rule (eg. strictThisArgument). Those changes would eliminate most of the mistakes that lead to unexpected values of this at runtime.
🔍 Search Terms
better this and bind, this.method is not a function, strict this, unbound method passed to map, filter, addEventListener compiles but fails at runtime, method as callback checking
✅ Viability Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code — not if it's introduced as a strictness rule
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
This example might look artificial, but in essence it's what every React developer does when they want their component to listen to some event. Passing methods as callbacks is a thing that happens quite often in the JS world.
How would this suggestion change this behavior? The first step would change the signature of the addToNum method to:
addToNum(this: ConstantShift,n: number): number
and the second step would make map accept (this: unknown, x: number) => number as the callback.
This would be enough to make TypeScript correctly recognize whether the code will work correctly at runtime, as can be seen in this playground.
💻 Use Cases
Eliminates mistakes when working with methods and callbacks, and ultimately increases productivity.
The text was updated successfully, but these errors were encountered:
Suggestion
The behavior of
this
in methods is one of the last features of JavaScript that is both unintuitive and not checked for by TypeScript. It is commonplace for both novice and experienced TypeScript programmers to accidentaly create a method (instead of an instance function) and then pass it as a callback. Currently, the only way to find out that one has made such a mistake is at runtime (yikes!).I propose two small changes in the typechecking system which could be introduced as a strict rule (eg.
strictThisArgument
). Those changes would eliminate most of the mistakes that lead to unexpected values ofthis
at runtime.🔍 Search Terms
better
this
andbind
,this.method
is not a function, strictthis
, unbound method passed tomap
,filter
,addEventListener
compiles but fails at runtime, method as callback checking✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Introduce a strict rule that will:
this
signature to methods in a classthis
in function signaturesunknown
by default – ie.() => void
would be equivalent to(this: unknown) => void
📃 Motivating Example
Consider the following example:
Link to playground.
This example might look artificial, but in essence it's what every React developer does when they want their component to listen to some event. Passing methods as callbacks is a thing that happens quite often in the JS world.
How would this suggestion change this behavior? The first step would change the signature of the
addToNum
method to:and the second step would make
map
accept(this: unknown, x: number) => number
as the callback.This would be enough to make TypeScript correctly recognize whether the code will work correctly at runtime, as can be seen in this playground.
💻 Use Cases
Eliminates mistakes when working with methods and callbacks, and ultimately increases productivity.
The text was updated successfully, but these errors were encountered: