Skip to content

feat(eslint-plugin): [no-unnecessary-type-assertion] add option to ignore string const assertions #10979

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

Merged
merged 5 commits into from
Apr 21, 2025

Conversation

KuSh
Copy link
Contributor

@KuSh KuSh commented Mar 21, 2025

PR Checklist

Overview

Add new ignoreStringConstAssertion that ignores const assertions on string literals to as it is a common use case to force typescript to not widen type to string

@typescript-eslint
Copy link
Contributor

Thanks for the PR, @KuSh!

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.

Copy link

netlify bot commented Mar 21, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit fa008e4
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/6802586f4aeeb50008be1762
😎 Deploy Preview https://deploy-preview-10979--typescript-eslint.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
Lighthouse
Lighthouse
1 paths audited
Performance: 94 (🔴 down 2 from production)
Accessibility: 100 (no change from production)
Best Practices: 100 (no change from production)
SEO: 98 (no change from production)
PWA: 80 (no change from production)
View the detailed breakdown and full score reports

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

nx-cloud bot commented Mar 21, 2025

View your CI Pipeline Execution ↗ for commit fa008e4.

Command Status Duration Result
nx test eslint-plugin ✅ Succeeded 6m 4s View ↗
nx run eslint-plugin:test -- --coverage ✅ Succeeded 6m 30s View ↗
nx test type-utils ✅ Succeeded 1s View ↗
nx test parser ✅ Succeeded 1s View ↗
nx run types:build ✅ Succeeded 1s View ↗
nx test eslint-plugin --coverage=false ✅ Succeeded 5m View ↗
nx typecheck ast-spec ✅ Succeeded 2s View ↗
nx run-many --target=build --exclude website --... ✅ Succeeded 19s View ↗
Additional runs (26) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-04-18 14:05:14 UTC

Copy link

codecov bot commented Mar 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.82%. Comparing base (f30a20e) to head (fa008e4).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #10979      +/-   ##
==========================================
- Coverage   90.82%   90.82%   -0.01%     
==========================================
  Files         497      497              
  Lines       50204    50222      +18     
  Branches     8274     8278       +4     
==========================================
+ Hits        45600    45615      +15     
- Misses       4589     4592       +3     
  Partials       15       15              
Flag Coverage Δ
unittest 90.82% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...-plugin/src/rules/no-unnecessary-type-assertion.ts 98.08% <100.00%> (-0.90%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JoshuaKGoldberg JoshuaKGoldberg changed the title feat(eslint-plugin): add option to ignore string const assertiions in no-unnecessary-type-assertion rule feat(eslint-plugin): [no-unnecessary-type-assertion] add option to ignore string const assertions Mar 24, 2025
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A great start, thank you 🎉! Seems like a very reasonable implementation direction. I left a few comments to expand it a bit & flip the logic.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Mar 24, 2025
@KuSh KuSh requested a review from JoshuaKGoldberg March 24, 2025 17:12
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Mar 24, 2025
@KuSh
Copy link
Contributor Author

KuSh commented Apr 1, 2025

@JoshuaKGoldberg I've taken your comments and suggestions into account.

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good! Just a few small things requested, no actual logic changes.

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Apr 7, 2025
@KuSh
Copy link
Contributor Author

KuSh commented Apr 9, 2025

Looking good! Just a few small things requested, no actual logic changes.

I've taken your suggestions into account, thanks

Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like tests are failing on some unknown option names - missed a few spots? 🙂

@KuSh
Copy link
Contributor Author

KuSh commented Apr 14, 2025

Looks like tests are failing on some unknown option names - missed a few spots? 🙂

Yeah sorry about that. It should be OK now!

@KuSh KuSh requested a review from JoshuaKGoldberg April 15, 2025 06:16
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Apr 15, 2025
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, and a nice reorganization of tests. Thanks! 🎉

@JoshuaKGoldberg JoshuaKGoldberg added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Apr 15, 2025
Copy link
Member

@ronami ronami left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀🚀 Looking great! I've added a couple of comments.

@ronami ronami added awaiting response Issues waiting for a reply from the OP or another party and removed 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge labels Apr 15, 2025
@KuSh KuSh force-pushed the main branch 2 times, most recently from 0251979 to 7a3d342 Compare April 18, 2025 09:09
@KuSh KuSh requested a review from ronami April 18, 2025 13:50
@github-actions github-actions bot removed the awaiting response Issues waiting for a reply from the OP or another party label Apr 18, 2025
Copy link
Member

@ronami ronami left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 🚀🚀

@ronami ronami added the 1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge label Apr 20, 2025
Copy link
Member

@JoshuaKGoldberg JoshuaKGoldberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

@JoshuaKGoldberg JoshuaKGoldberg merged commit 80bd7a5 into typescript-eslint:main Apr 21, 2025
58 of 59 checks passed
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Apr 23, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.29.0 | 8.31.0 |
| npm        | @typescript-eslint/parser        | 8.29.0 | 8.31.0 |


## [v8.31.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8310-2025-04-21)

##### 🚀 Features

-   **eslint-plugin:** \[no-unnecessary-type-assertion] add option to ignore string const assertions ([#10979](typescript-eslint/typescript-eslint#10979))

##### ❤️ Thank You

-   Nicolas Le Cam

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.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.
renovate bot added a commit to andrei-picus-tink/auto-renovate that referenced this pull request Apr 23, 2025
| datasource | package                          | from   | to     |
| ---------- | -------------------------------- | ------ | ------ |
| npm        | @typescript-eslint/eslint-plugin | 8.29.0 | 8.31.0 |
| npm        | @typescript-eslint/parser        | 8.29.0 | 8.31.0 |


## [v8.31.0](https://github.com/typescript-eslint/typescript-eslint/blob/HEAD/packages/eslint-plugin/CHANGELOG.md#8310-2025-04-21)

##### 🚀 Features

-   **eslint-plugin:** \[no-unnecessary-type-assertion] add option to ignore string const assertions ([#10979](typescript-eslint/typescript-eslint#10979))

##### ❤️ Thank You

-   Nicolas Le Cam

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.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.
phaux pushed a commit to phaux/typescript-eslint that referenced this pull request Apr 24, 2025
…nore string const assertions (typescript-eslint#10979)

* feat(eslint-plugin): add option to ignore string const assertiions in no-unnecessary-type-assertion rule
Fixes typescript-eslint#8721

* review: Enable const assertions on all literals by default

* fix: Handle null, undefined and boolean literal expressions

* Apply suggestions from code review
Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>

* Apply suggestions from code review typescript-eslint#2
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
1 approval >=1 team member has approved this PR; we're now leaving it open for more reviews before we merge
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [@typescript-eslint/no-unnecessary-type-assertion] False positives for "as const" assertions
3 participants