-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-explicit-any] suggest to replace keyof any with PropertyKey #11032
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
feat(eslint-plugin): [no-explicit-any] suggest to replace keyof any with PropertyKey #11032
Conversation
Thanks for the PR, @dbarabashh! typescript-eslint is a 100% community driven project, and we are incredibly grateful that you are contributing to that community. The core maintainers work on this in their personal time, so please understand that it may not be possible for them to review your work immediately. Thanks again! 🙏 Please, if you or your company is finding typescript-eslint valuable, help us sustain the project by sponsoring it transparently on https://opencollective.com/typescript-eslint. |
✅ Deploy Preview for typescript-eslint ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
View your CI Pipeline Execution ↗ for commit 2c6c6fa.
☁️ Nx Cloud last updated this comment at |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #11032 +/- ##
==========================================
- Coverage 88.03% 88.01% -0.02%
==========================================
Files 470 470
Lines 16801 16811 +10
Branches 4745 4749 +4
==========================================
+ Hits 14790 14797 +7
- Misses 1664 1667 +3
Partials 347 347
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Hey, I ran into a small issue. codecov is flagging line of code that is not covered by tests: function createPropertyKeyFixer(node: TSESTree.Node) {
return (fixer: TSESLint.RuleFixer) => {
if (node.parent && node.parent.type === AST_NODE_TYPES.TSTypeOperator) {
return fixer.replaceText(node.parent, 'PropertyKey');
}
return fixer.replaceText(node, 'unknown'); // ← This line isn't covered
};
} It is fallback that should never actually run I only call this function when we've already checked that the node has a parent that іs a keyof operator. what is the best way to handle this?
Any thoughts on what is usually preferred for these kinds of situations? Thanks! |
Great question! We've been hoping to write a blog post on the subject: #10388. Roughly, it's either of the following:
If the type checker is pressuring you to write code that you don't think is necessary (e.g. the
|
Thanks for the detailed reply @JoshuaKGoldberg I’ll go through all the topics you mentioned in your comment. |
Thanks again for the comment you left it had a lot of great insights, and I especially enjoyed your article. So, I pushed a commit with my solution. Of course, its not the best one out there, but following your article, I intentionally picked the worst and most unsafe approach 🙃. That said, the situation isn't as simple as it seems, so I will try to explain it in words maybe someone will find it helpful in the future. Using type assertion just to bypass type checking is generally a bad idea. Without solid test coverage, this approach can go wrong fast, and other node types might end up being handled incorrectly. in this particular case, I came to the following conclusions:
according to @JoshuaKGoldberg article, in this case, type assertion actually is the best strategy, because:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for keeping working on this - I think you're in the right track with the removing a runtime check. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Progress 🚀, but some un-resolved conversations still?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 thanks!
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.29.0 | 8.30.1 | | npm | @typescript-eslint/parser | 8.29.0 | 8.30.1 | ## [v8.30.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8301-2025-04-14) ##### 🩹 Fixes - **eslint-plugin:** fix mistake with eslintrc config generation ([#11072](typescript-eslint/typescript-eslint#11072)) ##### ❤️ Thank You - Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ## [v8.30.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8300-2025-04-14) ##### 🚀 Features - **eslint-plugin:** \[no-explicit-any] suggest to replace keyof any with PropertyKey ([#11032](typescript-eslint/typescript-eslint#11032)) ##### 🩹 Fixes - **eslint-plugin:** \[promise-function-async] use a different error message for functions with promise and non-promise types ([#10950](typescript-eslint/typescript-eslint#10950)) ##### ❤️ Thank You - Dima Barabash [@dbarabashh](https://github.com/dbarabashh) - Ronen Amiel You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ## [v8.29.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8291-2025-04-07) ##### 🩹 Fixes - **eslint-plugin:** \[no-deprecated] report on deprecated imported variable used as property ([#10998](typescript-eslint/typescript-eslint#10998)) ##### ❤️ Thank You - Ronen Amiel You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.29.0 | 8.30.1 | | npm | @typescript-eslint/parser | 8.29.0 | 8.30.1 | ## [v8.30.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8301-2025-04-14) ##### 🩹 Fixes - **eslint-plugin:** fix mistake with eslintrc config generation ([#11072](typescript-eslint/typescript-eslint#11072)) ##### ❤️ Thank You - Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ## [v8.30.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8300-2025-04-14) ##### 🚀 Features - **eslint-plugin:** \[no-explicit-any] suggest to replace keyof any with PropertyKey ([#11032](typescript-eslint/typescript-eslint#11032)) ##### 🩹 Fixes - **eslint-plugin:** \[promise-function-async] use a different error message for functions with promise and non-promise types ([#10950](typescript-eslint/typescript-eslint#10950)) ##### ❤️ Thank You - Dima Barabash [@dbarabashh](https://github.com/dbarabashh) - Ronen Amiel You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ## [v8.29.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8291-2025-04-07) ##### 🩹 Fixes - **eslint-plugin:** \[no-deprecated] report on deprecated imported variable used as property ([#10998](typescript-eslint/typescript-eslint#10998)) ##### ❤️ Thank You - Ronen Amiel You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.29.0 | 8.30.1 | | npm | @typescript-eslint/parser | 8.29.0 | 8.30.1 | ## [v8.30.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8301-2025-04-14) ##### 🩹 Fixes - **eslint-plugin:** fix mistake with eslintrc config generation ([#11072](typescript-eslint/typescript-eslint#11072)) ##### ❤️ Thank You - Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ## [v8.30.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8300-2025-04-14) ##### 🚀 Features - **eslint-plugin:** \[no-explicit-any] suggest to replace keyof any with PropertyKey ([#11032](typescript-eslint/typescript-eslint#11032)) ##### 🩹 Fixes - **eslint-plugin:** \[promise-function-async] use a different error message for functions with promise and non-promise types ([#10950](typescript-eslint/typescript-eslint#10950)) ##### ❤️ Thank You - Dima Barabash [@dbarabashh](https://github.com/dbarabashh) - Ronen Amiel You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ## [v8.29.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8291-2025-04-07) ##### 🩹 Fixes - **eslint-plugin:** \[no-deprecated] report on deprecated imported variable used as property ([#10998](typescript-eslint/typescript-eslint#10998)) ##### ❤️ Thank You - Ronen Amiel You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
| datasource | package | from | to | | ---------- | -------------------------------- | ------ | ------ | | npm | @typescript-eslint/eslint-plugin | 8.29.0 | 8.30.1 | | npm | @typescript-eslint/parser | 8.29.0 | 8.30.1 | ## [v8.30.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8301-2025-04-14) ##### 🩹 Fixes - **eslint-plugin:** fix mistake with eslintrc config generation ([#11072](typescript-eslint/typescript-eslint#11072)) ##### ❤️ Thank You - Kirk Waiblinger [@kirkwaiblinger](https://github.com/kirkwaiblinger) You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ## [v8.30.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8300-2025-04-14) ##### 🚀 Features - **eslint-plugin:** \[no-explicit-any] suggest to replace keyof any with PropertyKey ([#11032](typescript-eslint/typescript-eslint#11032)) ##### 🩹 Fixes - **eslint-plugin:** \[promise-function-async] use a different error message for functions with promise and non-promise types ([#10950](typescript-eslint/typescript-eslint#10950)) ##### ❤️ Thank You - Dima Barabash [@dbarabashh](https://github.com/dbarabashh) - Ronen Amiel You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website. ## [v8.29.1](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8291-2025-04-07) ##### 🩹 Fixes - **eslint-plugin:** \[no-deprecated] report on deprecated imported variable used as property ([#10998](typescript-eslint/typescript-eslint#10998)) ##### ❤️ Thank You - Ronen Amiel You can read about our [versioning strategy](https://main--typescript-eslint.netlify.app/users/versioning) and [releases](https://main--typescript-eslint.netlify.app/users/releases) on our website.
PR Checklist
Overview
Enhanced the
no-explicit-any
rule to suggest replacingkeyof any
with PropertyKey. added detection, helpful suggestion messages, auto-fix support, and test coverage.