想要扩展空接口但出现 lint 错误:no-empty-interface

Gib*_*hah 10 lint interface typescript angular

今天,我尝试在我的 angular 6 应用程序中执行此操作:

export interface AuthConfig {}

export interface BasicAuthConfig extends AuthConfig {
    username: string;
    password: string;
}

export interface OAuth2AuthConfig extends AuthConfig {
    tokenName: string;
    url: string;
    callbackUrl: string;
    clientId: string;
    scope: string[];
    grantType: grantTypes;
}
Run Code Online (Sandbox Code Playgroud)

并得到一个 lint 错误:

An empty interface is equivalent to `{}`. (no-empty-interface)
Run Code Online (Sandbox Code Playgroud)

我想用我的端点接口做到这一点:

Export interface Endpoint {
…
authConfig: AuthConfig;
…
}
Run Code Online (Sandbox Code Playgroud)

但被简化为这样做:

Export interface Endpoint {
…
authConfig: BasicAuthConfig | OAuth2AuthConfig;
…
}
Run Code Online (Sandbox Code Playgroud)

不同类型的授权可能会变得冗长,因此我不想继续将类型添加到 Endpoint 接口的 authConfig 属性中。有什么方法可以声明一个空接口以扩展它而不会对我大喊大叫吗?

谢谢。

Bor*_*par 15

您可以在以下位置禁用此规则tslint.json

"rules": { "no-empty-interface": false }
Run Code Online (Sandbox Code Playgroud)

或者只是禁用一行的 linting:

// tslint:disable-next-line
export interface AuthConfig {}
Run Code Online (Sandbox Code Playgroud)

  • 或者使用 `// tslint:disable-next-line:no-empty-interface` _only_ 禁用 `no-empty-interface` 规则。 (5认同)
  • 这条规则存在的原因是 typescript 使用结构化类型,因此定义一个空接口无助于区分两种类型。我会注意警告并避免空接口 (2认同)

acd*_*ior 6

还有另一种方式:

// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface AuthConfig {}
Run Code Online (Sandbox Code Playgroud)


V1N*_*NNY 5

如何禁用

Example:
$ cat .eslintrc.js
  ...
  "rules": {
  '@typescript-eslint/no-empty-interface': 'off'
  }
  ...
Run Code Online (Sandbox Code Playgroud)