Skip to content

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ronami
Copy link
Member

@ronami ronami commented Jan 7, 2025

PR Checklist

Overview

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

const wouldSameTypeBeInferred = 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 a const declaration. If it isn't a literal type, it would be reported as long as the assertion isn't as 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 (quoting Widening 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 a const 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 the TypeScript Handbook, Widening and Narrowing in Typescript and the 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:

declare const a: 'foo';

// `b` has the non-widened type of 'foo'
let b = a;

// `c` has the widened type of `string`
let c = 'foo';

See this 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 on https://opencollective.com/typescript-eslint.

Copy link

netlify bot commented Jan 7, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 8d17d09
🔍 Latest deploy log https://app.netlify.com/sites/typescript-eslint/deploys/678004a986308600091f091e
😎 Deploy Preview https://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 your Netlify site configuration.

Copy link

nx-cloud bot commented Jan 7, 2025

View your CI Pipeline Execution ↗ for commit 8d17d09.

Command Status Duration Result
nx run-many --target=build --exclude website --... ✅ Succeeded 2s View ↗
nx run-many --target=clean ✅ Succeeded 10s View ↗

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

Copy link

codecov bot commented Jan 7, 2025

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              
Flag Coverage Δ
unittest 86.97% <100.00%> (+0.02%) ⬆️

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.38% <100.00%> (+0.42%) ⬆️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant