Skip to content

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

Closed
@Cakez0r

Description

@Cakez0r

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'?

Metadata

Metadata

Assignees

No one assigned

    Labels

    By DesignDeprecated - use "Working as Intended" or "Design Limitation" instead

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions