Skip to content

this in methods not type of enclosing class #14936

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

Closed
EricTetz opened this issue Mar 30, 2017 · 3 comments
Closed

this in methods not type of enclosing class #14936

EricTetz opened this issue Mar 30, 2017 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@EricTetz
Copy link

TypeScript Version: 2.2.1

Code

function consumer(valueProducer: (this: void) => number) {
    console.log(valueProducer());
}

class Foo {
    private _value: number = 100;

    // this method requires `this: this` or `this: Foo` to provide type safety
    getValue(): number {
        return this._value;
    }

    constructor() {
        // callback does not have `this: void`, but Typescript can't warn me
        consumer(this.getValue);
    }
}

Expected behavior:

Class methods default to this: this.

Actual behavior:

Class methods default to this: any?

I'm not familiar with Git so I'm not good at distinguishing discussions vs actually implemented code, but #6739 appears to address the problem directly and includes the line:

"if --strictThis is specified, then functions will default this: void and methods will default this: this."

Which is exactly what I want. However, that page has an edit saying that the feature was "removed from this release". That was over a year ago. Was a fix reintroduced somewhere else? Catching this error seems like exactly the thing you'd want a type system for.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Mar 30, 2017
@RyanCavanaugh
Copy link
Member

Unfortunately the perf cost of automatically putting the this type in class methods was extremely high (30% or worse degradation in all scenarios involving classes) and we weren't able to find a solution that had acceptable performance trade-offs.

@EricTetz
Copy link
Author

EricTetz commented Mar 30, 2017

I am not sure what you mean, this works

Aluanhaddad, if you instantiate foo, you'll log "undefined".

Read the comments in the supplied example code, and the expected vs actual behavior.

I want typescript to recognize that the method passed to consumer will be called with the wrong this. I can achieve this by explicitly typing this in the method, e.g. (getValue(this: this): number), but that requires annotating every class method in my codebase with something Typescript seemingly should already know. #6739 addresses this directly, but it looks like the crucial part was pulled at the last minute. This was over a year ago, and there are other proposals about this issue referenced there. I was (Ryan answer the question) trying to figure out if this was ever actually addressed and how I can enable it.

we weren't able to find a solution that had acceptable performance trade-offs

Thank, Ryan. That's a bummer.

@aluanhaddad
Copy link
Contributor

Sorry I misunderstood your post. Sorry for wasting your time.

@microsoft microsoft locked and limited conversation to collaborators Jun 21, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants