Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat(eslint-plugin): [no-unnecessary-type-assertion] improve reporting of literal types (POC)#10631

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
ronami wants to merge6 commits intotypescript-eslint:main
base:main
Choose a base branch
Loading
fromronami:no-unnecessary-type-assertion-fresh-literals

Conversation

ronami
Copy link
Member

@ronamironami commentedJan 7, 2025
edited
Loading

PR Checklist

Overview

Theno-unnecessary-type-assertion rule has custom logic for handling literal types:

constwouldSameTypeBeInferred=castType.isLiteral()
?isImplicitlyNarrowedConstDeclaration(node)
:!isConstAssertion(node.typeAnnotation);

To my understanding, the current logic is as follows: If the cast type is a literal type, it would only be reported if it's the initializer of aconst declaration. If it isn't a literal type, it would be reported as long as the assertion isn'tas const (along with the asserted type and expression having the same type).

Zooming into the logic of handling literals, I think the rule tries to predict when a literal would be widened. To my understanding (quotingWidening and Narrowing in Typescript), a literal would be widened in the following cases:

  • Literal widening only happens to literal types that originate from expressions. These are called fresh literal types.
  • Literal widening happens whenever a fresh literal type reaches a "mutable" location.

To my understanding, the rule somewhat handles (2) as it only reports literals that are assigned to aconst variable declaration but ignores (1). (2) is still somewhat ignored, as "fresh" literals are relevant here too.

In my opinion, this logic may be improved so the rule can catch additional cases that it currently misses (and not fail on some false positives), mostly around "fresh literals".

I'm basing this on theTypeScript Handbook,Widening and Narrowing in Typescript andthe literal widening PR, along with testing this on the playground. See this comment#1056 (comment), as I think they're somewhat related.

This isn't something I have a thorough understanding of, but it seems like the case with the following simplified example:

declareconsta:'foo';// `b` has the non-widened type of 'foo'letb=a;// `c` has the widened type of `string`letc='foo';

Seethis playground link for bugs in the current implementation which I think may be improved.


The goal of sending this draft POC PR is to make it easier to discuss or test this on the deploy preview playground. This PR roughly implements what's described above (along with fixing#10257 and#10257 (comment)).

This is a POC proposal to a topic I'm far from being an expert in, so I'd be happy for opinions on this.

Note: Should some of these assumptions be checked/verified by the TypeScript devs? There might be edge cases that I missed that require APIs that aren't currently public.

Please let me know if this should be an issue instead; I wasn't sure if this should be an issue, a draft PR, or an issue accompanied by a draft PR.


Without taking "fresh literals" into consideration, I think differentiating between these two would be difficult:1,2.

@typescript-eslint
Copy link
Contributor

Thanks for the PR,@ronami!

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 onhttps://opencollective.com/typescript-eslint.

@netlifyNetlify
Copy link

netlifybot commentedJan 7, 2025
edited
Loading

Deploy Preview fortypescript-eslint ready!

NameLink
🔨 Latest commit8d17d09
🔍 Latest deploy loghttps://app.netlify.com/sites/typescript-eslint/deploys/678004a986308600091f091e
😎 Deploy Previewhttps://deploy-preview-10631--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: 92 (🔴 down 1 from production)
Accessibility: 100 (no change from production)
Best Practices: 92 (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 yourNetlify site configuration.

@nx-cloudNx Cloud
Copy link

nx-cloudbot commentedJan 7, 2025
edited
Loading

View yourCI Pipeline Execution ↗ for commit8d17d09.

CommandStatusDurationResult
nx run-many --target=build --exclude website --...✅ Succeeded2sView ↗
nx run-many --target=clean✅ Succeeded10sView ↗

☁️Nx Cloud last updated this comment at2025-01-10 18:12:27 UTC

@codecovCodecov
Copy link

codecovbot commentedJan 7, 2025
edited
Loading

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.97%. Comparing base(f2a3e8c) to head(8805b82).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@##             main   #10631      +/-   ##==========================================+ Coverage   86.94%   86.97%   +0.02%==========================================  Files         446      446                Lines       15508    15534      +26       Branches     4518     4529      +11     ==========================================+ Hits        13484    13510      +26  Misses       1669     1669                Partials      355      355
FlagCoverage Δ
unittest86.97% <100.00%> (+0.02%)⬆️

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

Files with missing linesCoverage Δ
...-plugin/src/rules/no-unnecessary-type-assertion.ts98.38% <100.00%> (+0.42%)⬆️

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@ronami

[8]ページ先頭

©2009-2025 Movatter.jp