Tslint: Discussion: separated checking of variables and types in no-shadowed-variable

Created on 9 Jul 2017  ยท  8Comments  ยท  Source: palantir/tslint

Bug Report

  • __TSLint version__: 5.5.0
  • __TypeScript version__:
  • __Running TSLint via__: (pick one) CLI / Node.js API / VSCode / grunt-tslint / Atom / Visual Studio / etc

TypeScript code being linted

namespace A {
    interface A {} // error: shadowed name 'A'
    let v = A; // namespace A
    let t: A; // interface A
}

Actual behavior

Error for shadowed name 'A'

Expected behavior

Technically there's no shadowing, because both names can coexist in different domains.
Do we want to allow this at all? By default or with an option?
The implementation would not be too difficult.

P3 Feature Request ๐ŸŒน R.I.P. ๐ŸŒน

Most helpful comment

I just updated to tslint 5.6.0 from 4.0 - I've previously had 'shadowed variables' checks enabled however they're now failing for the following pattern of code:

  constructor(private $i18next: ngI18Next.I18NextService,
              private $scope: angular.IScope,
              private $uibModal: angular.ui.bootstrap.IModalService,
              private AuthenticationService: AuthenticationService)

The use of the type and parameter as the same name (AuthenticationService - this is angular 1.x code) cause the message "Shadowed name: 'AuthenticationService'"

I think it's the same issue as this bug? I tried adjusting the options for no-shadow-variable but adding "typeAlias": false && "typeParameter": false didn't seem to make any difference?

All 8 comments

eh, I guess you're right, we could make this change to be technically correct. I don't see this "shadowing" pattern being very valuable in practice so I would prefer it to be opt-in by default (keep the rule strict by default).

I just updated to tslint 5.6.0 from 4.0 - I've previously had 'shadowed variables' checks enabled however they're now failing for the following pattern of code:

  constructor(private $i18next: ngI18Next.I18NextService,
              private $scope: angular.IScope,
              private $uibModal: angular.ui.bootstrap.IModalService,
              private AuthenticationService: AuthenticationService)

The use of the type and parameter as the same name (AuthenticationService - this is angular 1.x code) cause the message "Shadowed name: 'AuthenticationService'"

I think it's the same issue as this bug? I tried adjusting the options for no-shadow-variable but adding "typeAlias": false && "typeParameter": false didn't seem to make any difference?

Is there a solution to this issue? Other than going back to 4.0?

@mahmoudm The quick fix is to disable checking of everything but parameters and variables to (almost) restore the behavior of 4.x

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

Thanks @ajafff works like a charm!

The use of the type and parameter as the same name (AuthenticationService - this is angular 1.x code) cause the message "Shadowed name: 'AuthenticationService'"

I'd love to see this addressed too. let SomeModel: SomeModel or similar shouldn't fail. There's no shadowing or reader ambiguity, and it makes sense for the type and the variable to have the same name in some cases.

โ˜ ๏ธ TSLint's time has come! โ˜ ๏ธ

TSLint is no longer accepting most feature requests per #4534. See typescript-eslint.io for the new, shiny way to lint your TypeScript code with ESLint. โœจ

It was a pleasure open sourcing with you all!

๐Ÿค– Beep boop! ๐Ÿ‘‰ TSLint is deprecated ๐Ÿ‘ˆ _(#4534)_ and you should switch to typescript-eslint! ๐Ÿค–

๐Ÿ”’ This issue is being locked to prevent further unnecessary discussions. Thank you! ๐Ÿ‘‹

Was this page helpful?
0 / 5 - 0 ratings