fix(eslint-plugin): [no-unnecessary-type-assertion] fix false negative for const variable declarations#8558
Conversation
|
Thanks for the PR, @abrahamguo! 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. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #8558 +/- ##
==========================================
- Coverage 88.10% 88.09% -0.02%
==========================================
Files 402 402
Lines 14029 14015 -14
Branches 4117 4112 -5
==========================================
- Hits 12360 12346 -14
Misses 1370 1370
Partials 299 299
Flags with carried forward coverage won't be shown. Click here to find out more.
|
There was a problem hiding this comment.
See the end of this comment #4485 (comment) for details of the changes we would accept to the rule.
Furthermore, the TSLint PR above also introduced some special handling for tuple types, which appears to be unnecessary, so it has been removed.
playground is this not the case that this assertion is required?
Or are you saying that the logic isn't required because it's never hit?
@bradzacher this assertion is indeed required; however, no special handling is required for it (or, for any tuple types, I believe) in this lint rule because |
|
Alrighty — I still need to clean up the tests a little bit, but I've reviewed (and added tests for):
and I am confident that my logic is correct for detecting useful vs useless literal type assertions:
Things to investigate for the future:
|
auvred
left a comment
There was a problem hiding this comment.
Overall looks good to me! Thanks for working on this! 🚀
Just requesting splitting the test cases
| function isConstVariableDeclaration(node: TSESTree.Node): boolean { | ||
| return ( | ||
| node.type === AST_NODE_TYPES.VariableDeclaration && | ||
| node.kind === 'const' | ||
| ); | ||
| } | ||
|
|
There was a problem hiding this comment.
[non-actionable] Hmmm, looks like these cases are not reported (playground)
let a: unknown
const b = (a = 1 as 1)
const c = (console.log(1), 1 as 1)However, I'm not sure we can easily detect all such cases. This function might become too complex...
There was a problem hiding this comment.
Yeah I think we can leave these for now. If someone files an issue showing them being buggy in isolation, then that'd indicate there's enough user appetite to start thinking about whether the complexity is worth it.
no-unnecessary-type-assertion
auvred
left a comment
There was a problem hiding this comment.
Getting closer! 🙌
I think the main blocker in my mind is updating correct example in the docs
…o-unnecessary-type-assertion
Co-authored-by: auvred <61150013+auvred@users.noreply.github.com>
…/typescript-eslint into no-unnecessary-type-assertion
…o-unnecessary-type-assertion


PR Checklist
Overview
The
no-unnecessary-type-assertionsrule needs a bit more detail about when type assertions are truly useful or useless on literals.