Check when a function that uses this
is referenced without binding it
#59994
Labels
Duplicate
An existing issue was already created
π Search Terms
this binding
β Viability Checklist
β Suggestion
I've recently started using TypeScript and was porting ES5 function classes to ES6 classes and I've noticed an unchecked case related to passing references to class methods and
this
binding that could be statically analyzed.Consider this example:
This will always fail because we're passing a reference to a class function that references
this
in the class. TheonConnection
callback will always throw aTypeError
unless it's called with the properthis
binding:In the example above,
net.createServer(this.onConnection)
is a bug that could be caught at compile time: a function is referencingthis
, passing it by reference (or binding it to a variable) does not preserve itsthis
context, so any calls through it would produce an error (unless the function is never called, or the caller will call it with a correctthis
).At compile time, any references to a function using
this
in its scope can result in errors or warnings unless it is bound or is called from the samethis
scope context (can be silenced with!
)π Motivating Example
This feature helps prevent bugs that will result in runtime type errors at compile time.
π» Use Cases
What do you want to use this for?
To catch and prevent type errors
What shortcomings exist with current approaches?
This is currently not checked
What workarounds are you using in the meantime?
Checking it manually
The text was updated successfully, but these errors were encountered: