简体   繁体   English

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

[英]Extend class that implements Interface shadows name

Consider the following Typescript classes: 考虑以下Typescript类:

interface ITest {
  example(): string;
}

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

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

This translates to the following Javascript code (see http://www.typescriptlang.org/Playground ): 这将转换为以下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);

The code runs correctly and gives the result 代码正确运行并给出结果

"Test A"
"Test B"

But checking this code with JSLint gives the warning 但是使用JSLint检查此代码会发出警告

One warning 17 一警告17

'B' is already defined. “ B”已经定义。

JsLint seems to have problems with __extends(B, _super) . JsLint似乎对__extends(B, _super)有问题。 But of course this is necessary for extending the class. 但是,当然,这对于扩展类很有必要。 So how can I make sure that JSLint does not complain when using inheritance in TypeScript? 因此,如何在TypeScript中使用继承时确保JSLint不抱怨?

Don't run lint on your generated code. 不要在生成的代码上运行lint。 Lint is to enforce style and best practices on source, so it should be run on the input to any code-generating tools. Lint将在源代码上实施样式和最佳实践,因此应在任何代码生成工具的输入上运行它。 The output of those tools will rarely, if ever, be friendly enough to lint and it's not something you need to read or validate anyway. 这些工具的输出很少(即使有的话)足够友好,不会掉毛,而且无论如何,您都不需要阅读或验证它们。 When working with compiled languages you run lint on the source rather than the binary and this is the JS equivalent. 使用编译语言时,您在源代码而不是二进制代码上运行lint,这等效于JS。

You should use a tool like TSLint to check the Typescript before feeding it to the TS compiler. 您应该使用TSLint之类的工具来检查Typescript,然后再将其提供给TS编译器。 If you're using Gulp (or Grunt) there are TSLint plugins ( gulp-tslint and grunt-tslint ). 如果您使用的是Gulp(或Grunt),则有TSLint插件( gulp-tslintgrunt-tslint )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 打字稿:为实现和接口或扩展DTO的对象添加原型方法 - Typescript: add prototype method for a object which implements and interface or extend DTO 为什么在类中必须声明字段,因为它实现了一个接口 - Why field declaration is must in class as it implements an interface WebUSB:请求的接口实现一个受保护的类 - WebUSB: The requested interface implements a protected class TypeScript:实现某个接口或方法的 class 的类型 - TypeScript: type of class that implements a certain interface or method 类实现接口,但流程显示“此类型不兼容” - class implements interface yet flow says “this type is incompatible” JS / TS确定JSON对象是否实现了类或接口 - JS / TS determine if a JSON object implements a class or interface 如何声明给定的类在Facebook Flow中实现接口? - how to declare that a given class implements an interface in Facebook Flow? TS:在实现接口的抽象类的实现中缺少可选成员的类型 - TS: missing typings for optional members in implementation of abstract class that implements interface 如何定义采用在流中实现接口的Class的函数? - How to define function that takes a Class that implements interface in flow? TypeScript Class 使用联合类型字段实现接口导致错误 (2322) - TypeScript Class Implements Interface with Union Type Field results in Error (2322)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM