1

It seems that within an instance method for a typescript class, the typescript compiler believes that 'this' is the same type as the declaring class.

For example:

class Person {
    private name: string;

    constructor(name) {
        this.name = name;
    }

    public showName() {
        //Typescript believes 'this' is a 'Person', but it could be anything!

        var me: Person = this;

        alert("My name is " + this.name);
    }
}


class Introducer {
    public introduce(person: Person) {
        setTimeout(person.showName, 1);
    }
}

var introducer: Introducer = new Introducer();
var bob = new Person("Bob");

this.name = "Dave";

introducer.introduce(bob);

In the showName function, typescript things that 'this' is a 'Person'. However, that is not always guaranteed to be the case - as with the example shown by the Introducer class (the output is "My name is Dave", NOT "My name is Bob").

Given this possibility, why is the type of 'this' not 'any' within class instance methods?

5
  • 2
    Following your reasoning, any types could be removed. Static typing can be bypassed at runtime. This is not a reason for not using static typing.
    – Paleo
    Feb 19, 2015 at 13:28
  • 2
    To add to what @Tarh said.... The type checking is done while coding/compilation and is not part of the rendered code, so while it could be called with anything, it will be checked to be of type Person during compilation.
    – Brocco
    Feb 19, 2015 at 13:33
  • @Brocco: My issue is that the type of 'this' can't possibly be enforced at compile type because its type is unknown. So why then does the compiler assume it is Person? Static typing can be bypassed at run time, but this is a compile time issue.
    – Cakez0r
    Feb 19, 2015 at 14:38
  • Because TypeScript is essentially JavaScript with types added on top of it it maintains JS's scoping as well. Which means that this within the showName function is scoped to the class Person hence giving it the type of Person, but the this where you are setting name to Dave is in a different scope where this is not of type Person
    – Brocco
    Feb 19, 2015 at 19:01
  • "Which means that this within the showName function is scoped to the class Person hence giving it the type of Person" - that would be the case if I were using the fat-arrow / lambda syntax, but I'm not doing that in my example.
    – Cakez0r
    Feb 19, 2015 at 20:04

1 Answer 1

2

The compiler has the reasonable assumption that you will be within the scope of the class that you created when the code runs. There are several tools available to you to make sure that your methods are called within that scope including arrow-functions () =>, call and apply. You can use these to bind the method calls to the class context in situations such as you describe.

Here is an example using the arrow-function, this gives you a class guarantee:

class Person {
    private name: string;

    constructor(name) {
        this.name = name;
    }

    showName = () => {
        //'this' is a definitely a 'Person'

        var me: Person = this;

        alert("My name is " + this.name);
    }
}

Although I prefer to resolve the scope from the caller when the caller is causing a change in scope, like this:

class Introducer {
    public introduce(person: Person) {
        setTimeout(() => person.showName(), 1);
    }
}
1
  • Thanks! I posted a bug on the TypeScript issues page about this and it was marked as by design. I understand that in most cases, the value of this will be of the correct type but I think that just makes it all the more confusing when it's not of the correct type. Especially given that they've implemented a language feature to specifically deal with this problem, but ignored the problem anyway for class instance functions!
    – Cakez0r
    Feb 19, 2015 at 20:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.