Skip to content

Is the value of 'this' in switch statement in Typescript 1.6.2 and atom-typescript emited incorrectly? #723

Closed
@kpgarrod

Description

@kpgarrod

I asked a question in SO: http://stackoverflow.com/questions/33710856/is-the-value-of-this-in-switch-statement-in-typescript-1-6-2-and-atom-typescri

The issue doesn't seem to occur in VSCode on my system or in WebStorm on somebody else's, so I guess it is looking like an atom-typescript issue. Here is a copy of the SO post:

I have some Typescript code that looks something like this:

class Test {
    private userId: string
    constructor() {
      this.userId = 'test'
    }
    test() {
      return new Promise((resolve, reject) => {
        let sharing = 'private'
        console.log("before switch this.userId", this.userId);
        switch (sharing) {
          case "private":
            resolve(this.userId);
            break;
        }
      })
    }
}

If I edit it in the Typescript playground the following Javascript code is emitted as I expect:

var Test = (function () {
  function Test() {
    this.userId = 'test';
  }
  Test.prototype.test = function () {
    var _this = this;
    return new Promise(function (resolve, reject) {
        var sharing = 'private';
        console.log("before switch this.userId", _this.userId);
        switch (sharing) {
            case "private":
                resolve(_this.userId);
                break;
        }
    });
  };
  return Test;
})();

However, if I edit the same code in my installed Atom editor, what is emitted is:

var Test = (function () {
  function Test() {
    this.userId = 'test';
  }
  Test.prototype.test = function () {
    var _this = this;
    return new Promise(function (resolve, reject) {
        var sharing = 'private';
        console.log("before switch this.userId", _this.userId);
        switch (sharing) {
            case "private":
                resolve(this.userId); // This resolve is missing a _
                break;
        }
    });
  };
  return Test;
})();

The difference is in the resolve statement 12 lines down. What is being resolved is this.userId instead of _this.userId.

I am running a new installation with:

  • Atom 1.1.0
  • atom-typescript 7.8.0
  • Ubuntu 15.10
  • typescript 1.6.2 installed with npm

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions