Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-shadow] false positive for class static generics #3312

Closed
3 tasks done
vmatyi opened this issue Apr 25, 2021 · 1 comment
Closed
3 tasks done

[no-shadow] false positive for class static generics #3312

vmatyi opened this issue Apr 25, 2021 · 1 comment
Labels
duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@vmatyi
Copy link

vmatyi commented Apr 25, 2021

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

{
  "rules": {
    "no-shadow": "off",
    "@typescript-eslint/no-shadow": "warn",
  }
}
export class SomeClass<K> {
  static create<K>() {
    return new SomeClass<K>(); // warn: 'K' is already declared in the upper scope. eslint(@typescript-eslint/no-shadow)
  }
}

Expected Result

No ESLint errors about shadowed name.
The generic parameter of a class is not available in static functions, thus there is no real shadowing:

export class SomeClass<K> {
  static create() {
    return new SomeClass<K>();  // error: Static members cannot reference class type parameters. ts(2302)
  }
}

Renaming the static members will reduce readability, as the function and meaning of the generic parameter is the same.
The use case is very similar to a class member and a member function parameter, which is allowed, and rightly so:

export class OtherClass {
  name: string | undefined;
  setName(name: string) {
    this.name = name;
  }
}

This issue was mentioned as part of #2480 (2.), but I suspect it was overlooked.

Actual Result

'K' is already declared in the upper scope. eslint(@typescript-eslint/no-shadow)

Versions

package version
@typescript-eslint/eslint-plugin-tslint 4.22.0
@typescript-eslint/parser 4.22.0
TypeScript 3.9.9
ESLint 7.24.0
node 12.19.1
@bradzacher
Copy link
Member

Duplicate of #2592

Please use the search

@bradzacher bradzacher added duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin labels May 15, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
duplicate This issue or pull request already exists package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

No branches or pull requests

2 participants