简体   繁体   English

tslint错误阴影名称:'Observable'

[英]tslint error Shadowed name: 'Observable'

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 tutorial for adding a debug operator to Observable and it is working fine except I am getting this lint error. 我按照本教程向Observable添加了一个调试操作符,它正常工作,除了我得到这个lint错误。 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. 我一直在使用这个调试操作符一段时间没有得到lint错误,我不知道为什么我现在得到它。

Here is the code at line 27 to amend the type definition with the debug method 这是第27行的代码,用于使用调试方法修改类型定义

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

Does anyone know how I can clear this lint error? 有谁知道我怎么能清除这个lint错误? Thank you! 谢谢!

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 (ie both instances of Observable have the same common root) you are not technically shadowing, but if you have an Observable at 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... ie 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. 如果有人可以提供界面阴影会导致问题的真实示例,我很乐意改变我的观点。

Basically, Fenton explains it quite well with his example. 基本上, Fenton用他的例子很好地解释了它。 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 error parameters. 你看,很多error参数。

Not sure how this fixed it but I reinstalled my package dependencies including tslint and now I don't get the error anymore. 不知道这是如何修复它但我重新安装我的包依赖包括tslint现在我不再得到错误。 Thanks for your time trying to help :) 谢谢你的时间试图帮助:)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM