Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check when a function that uses this is referenced without binding it #59994

Open
6 tasks done
u130b8 opened this issue Sep 17, 2024 · 1 comment
Open
6 tasks done

Check when a function that uses this is referenced without binding it #59994

u130b8 opened this issue Sep 17, 2024 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@u130b8
Copy link

u130b8 commented Sep 17, 2024

πŸ” 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:

class MyServer {
    server: net.Server;
    counter: number;

    constructor() {
        this.server = net.createServer(this.onConnection);
        this.counter = 0;
    }

    onConnection(socket: net.Socket) {
        this.counter++;
    }
}

This will always fail because we're passing a reference to a class function that references this in the class. The onConnection callback will always throw a TypeError unless it's called with the proper this binding:

this.server = net.createServer(() => this.onConnection()); // OK
this.server = net.createServer(this.onConnection.bind(this)); // OK

In the example above, net.createServer(this.onConnection) is a bug that could be caught at compile time: a function is referencing this, passing it by reference (or binding it to a variable) does not preserve its this context, so any calls through it would produce an error (unless the function is never called, or the caller will call it with a correct this).

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 same this 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

  1. What do you want to use this for?
    To catch and prevent type errors

  2. What shortcomings exist with current approaches?
    This is currently not checked

  3. What workarounds are you using in the meantime?
    Checking it manually

@MartinJohns
Copy link
Contributor

Duplicate of #7968.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants