typescript tslint 错误阴影名称:'Observable'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46794104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:58:42  来源:igfitidea点击:

tslint error Shadowed name: 'Observable'

angulartypescriptrxjsobservabletslint

提问by Devin Crossman

I am getting the following error when running tslint that I wasn't getting before..

运行 tslint 时出现以下错误,这是我以前没有遇到的。

ERROR: C:/...path..to../observable-debug-operator.ts[27, 13]: Shadowed name: 'Observable'

I followed this tutorialfor adding a debug operator to Observable and it is working fine except I am getting this lint error. I had been using this debug operator for a while without getting the lint error and I'm not sure why I am getting it now.

我按照本教程将调试运算符添加到 Observable 并且它工作正常,但我收到此 lint 错误。我已经使用这个调试操作符一段时间了,但没有出现 lint 错误,我不知道为什么我现在会得到它。

Here is the code at line 27 to amend the type definition with the debug method

这是第 27 行的代码,用于使用 debug 方法修改类型定义

declare module 'rxjs/Observable' {
  interface Observable<T> { // line 27
    debug: (...any) => Observable<T>;
  }
}

Does anyone know how I can clear this lint error? Thank you!

有谁知道我如何清除这个 lint 错误?谢谢!

回答by Fenton

Here is a quick example of variable shadowing, to make the warning clear.

这是变量阴影的一个快速示例,以明确警告。

var x = 4;

function example() {
    var x = 5; // x is shadowing the outer scope's x variable
}

If you are declaring an extension to an interface (i.e. both instances of Observablehave the same common root) you are not technically shadowing, but if you have an Observableat multiple levels, it may make it unclear to which you are referring.

如果您声明一个接口的扩展(即两个实例Observable具有相同的公共根),您在技术上并不是在遮蔽,但是如果您Observable在多个级别上有一个,它可能会使您不清楚您指的是哪个。

You can switch off shadowing warnings using the option:

您可以使用以下选项关闭阴影警告:

"no-shadowed-variable": [
  true,
  {
    "class": true,
    "enum": true,
    "function": true,
    "interface": false,
    "namespace": true,
    "typeAlias": false,
    "typeParameter": false
  }
]

Is interface shadowing a problem in TypeScript?

界面阴影是 TypeScript 中的一个问题吗?

Not really - you would catch a situation where an interface was declared inside a function, which you would also catch because if it was a problem the TypeScript compiler would already be telling you there is a problem... i.e. the member list would show you the correct members in both scopes.

不是真的 - 您会发现在函数内部声明了接口的情况,您也会发现这种情况,因为如果这是一个问题,TypeScript 编译器会已经告诉您存在问题......即成员列表会显示给您两个范围内的正确成员。

Interfaces are also erased - so there is no post-compile confusion, for example if someone were to use your TypeScript library in a JavaScript program.

接口也被删除了——所以没有编译后混淆,例如如果有人要在 JavaScript 程序中使用你的 TypeScript 库。

I'm happy to change my opinion if someone can supply a realistic example of where interface shadowing would cause a problem.

如果有人可以提供一个现实的例子来说明界面阴影会导致问题的地方,我很高兴改变我的观点。

回答by alex351

Basically, Fentonexplains it quite well with his example. Shadowing occurs with naming collisions.

基本上,芬顿用他的例子很好地解释了这一点。命名冲突时会出现阴影。

So why not name a nested variable/parameter something else than x? ;)

那么为什么不命名一个嵌套变量/参数而不是 x 呢?;)

My example:

我的例子:

...
.retryWhen(error => {
  return error
    .mergeMap((error: any) => {
      if (error.status === 500) {
...

You see, a lot of errorparameters.

你看,很多error参数。

回答by Devin Crossman

Not sure how this fixed it but I reinstalled my package dependencies including tslint and now I don't get the error anymore. Thanks for your time trying to help :)

不确定这是如何修复它的,但我重新安装了包括 tslint 在内的包依赖项,现在我不再收到错误消息。感谢您抽出时间提供帮助:)