Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tslint error Shadowed name: 'Observable'

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

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. 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.

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

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!

like image 858
Devin Crossman Avatar asked Oct 17 '17 15:10

Devin Crossman


1 Answers

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 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.

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?

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.

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.

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

like image 73
Fenton Avatar answered Sep 22 '22 19:09

Fenton