扩展实现接口阴影名称的 class

Extend class that implements Interface shadows name

考虑以下打字稿 classes:

interface ITest {
  example(): string;
}

class A implements ITest {
  example() {
    return 'Test A';
  }
}

class B extends A {
  example() {
    return 'Test B';
  }
}

这转换为以下 Javascript 代码(参见 http://www.typescriptlang.org/Playground):

var __extends = this.__extends || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    __.prototype = b.prototype;
    d.prototype = new __();
};
var A = (function () {
    function A() {
    }
    A.prototype.example = function () {
        return 'Test A';
    };
    return A;
})();
var B = (function (_super) {
    __extends(B, _super);
    function B() {
        _super.apply(this, arguments);
    }
    B.prototype.example = function () {
        return 'Test B';
    };
    return B;
})(A);

代码运行正确并给出结果

"Test A"
"Test B"

但是用 JSLint 检查此代码会给出警告

One warning 17

'B' is already defined.

JsLint 似乎与 __extends(B, _super) 有问题。但是当然这对于扩展 class 是必要的。那么如何确保在 TypeScript 中使用继承时 JSLint 不会报错呢?

不要运行 lint 生成的代码。 Lint 是在源代码上强制执行样式和最佳实践,因此它应该 运行 在任何代码生成工具的输入上。这些工具的输出很少(如果有的话)对 lint 足够友好,而且它不是您需要阅读或验证的东西。当使用编译语言时,你 运行 对源而不是二进制文件进行 lint,这是 JS 等价物。

您应该使用像 TSLint to check the Typescript before feeding it to the TS compiler. If you're using Gulp (or Grunt) there are TSLint plugins (gulp-tslint and grunt-tslint) 这样的工具。