Skip to content

[no-explicit-any] allow any in a keyof any expression #3206

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

Closed
3 tasks done
iliazeus opened this issue Mar 20, 2021 · 2 comments
Closed
3 tasks done

[no-explicit-any] allow any in a keyof any expression #3206

iliazeus opened this issue Mar 20, 2021 · 2 comments
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@iliazeus
Copy link

  • 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": {
    "@typescript-eslint/no-explicit-any": 2
  }
}
function getNumber<K extends keyof any, T extends { [P in K]: number }>(key: K, obj: T): number {
  return obj[key];
}

Currently, the expression keyof any is probably the only (sane) way of saying "a type of things that can be object keys". But it is disallowed under no-explicit-any.

The long way of string | number | symbol isn't portable enough because of the symbol part.

Versions

package version
@typescript-eslint/eslint-plugin 4.18.0
@typescript-eslint/parser 4.18.0
TypeScript 4.2.3
ESLint 7.22.0
node 14.15.4
@iliazeus iliazeus added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Mar 20, 2021
@iliazeus
Copy link
Author

A more realistic example would be a decorator that can only be applied to number properties:

const integer = <TKey extends keyof any, TTarget extends { [K in TKey]: number }>(
  target: TTarget,
  key: TKey
) => { /* ... */ };

class Foo {
  @integer foo = 123;
  @integer bar = "adsf"; // error: 
}

@bradzacher
Copy link
Member

You can use never instead if you must use the keyof syntax.
Or you could also just use the PropertyKey type declared as part of the TS types

const integer = <TKey extends PropertyKey , TTarget extends { [K in TKey]: number }>(
  target: TTarget,
  key: TKey
) => { /* ... */ };

class Foo {
  @integer foo = 123;
  @integer bar = "adsf"; // error: 
}

repl

@bradzacher bradzacher added wontfix This will not be worked on and removed triage Waiting for team members to take a look labels Mar 21, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants