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

Incorrect type inferred for 'this' in instance methods #2078

Closed
Cakez0r opened this issue Feb 19, 2015 · 1 comment
Closed

Incorrect type inferred for 'this' in instance methods #2078

Cakez0r opened this issue Feb 19, 2015 · 1 comment
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@Cakez0r
Copy link

Cakez0r commented Feb 19, 2015

Given a class

class Foo {
    private baz: string;

    public bar() {
        var x = this;

        console.log(x.baz);
    }
}

The compiler infers 'this' to be of type 'Foo' within the 'bar' function. However, the type of 'this' is not guaranteed to be 'Foo'.

Consider

var y = new Foo();
setTimeout(y.bar, 1000);

Given this possibility, should the type of 'this' within 'bar' be 'any' instead of 'Foo'?

@danquirk
Copy link
Member

In practice the majority of the time people use this in a class they'll want it to be typed as Foo. There're ways around this as you noted but it would be unfortunate to degrade the core class scenarios for that. There are a variety of improvements we are hoping to make to this typing one day though, see #513

@danquirk danquirk added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Feb 19, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

2 participants