typescript - Typescript 1.6.2 和 atom-typescript 的 switch 语句中的 'this' 值是否发出错误?

标签 typescript atom-editor

我有一些看起来像这样的 Typescript 代码:

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;
        }
      })
    }
}

如果我在 the Typescript playground 中编辑它以下 Javascript 代码如我所料发出:

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;
})();

但是,如果我在我安装的 Atom 编辑器中编辑相同的代码,发出的是:

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;
})();

区别在于下面 12 行的 resolve 语句。正在解决的是 this.userId 而不是 _this.userId

我正在运行一个新安装:

  • 原子 1.1.0
  • 原子 typescript 7.8.0
  • Ubuntu 15.10
  • 使用 npm 安装 typescript 1.6.2

最佳答案

这是两天前修复的 TypeScript 问题:https://github.com/Microsoft/TypeScript/issues/5637

刚刚在 atom-typescript 中修复了 https://github.com/TypeStrong/atom-typescript/releases/tag/v7.10.0

关于typescript - Typescript 1.6.2 和 atom-typescript 的 switch 语句中的 'this' 值是否发出错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33710856/

相关文章:

angular - 从 Angular 6 Material 树中的子节点获取父层次结构

editor - Sublime text 和 Github 的 Atom 有什么区别

windows - Atom 菜单丢失。我如何重新启用

没有模块加载器的 typescript ?

angular - 为组件 : ion-range 使用自定义颜色

node.js - 导出声明类型 ResourceType = Lowercase<Protocol.Network.ResourceType>,使用 Puppeteer 和 typescript 构建 nodejs 应用程序时出错

javascript - 为什么 node.js 无法定位其他 typescript React 组件

sublimetext - Atom Editor/Sublime Text - 尊重 .editorconfig 和 .jscsrc 的代码格式化程序

json - React Native 打包器找不到 package.json

atom-editor - 如何在 Atom 中编写自定义命令?