🌐 AI搜索 & 代理 主页
Skip to content

Conversation

@Tamashoo
Copy link
Contributor

@Tamashoo Tamashoo commented Dec 7, 2025

PR Checklist

Overview

Added special handling for type checking of IIFEs in the no-unnecessary-type-assertion rule.
Introduced an isIIFE type guard and a getUncastType helper function to correctly resolve IIFE return types, converting implicit undefined to void for functions without explicit return type annotations.
This improves the accuracy of unnecessary assertion detection for IIFE expressions.

…rtion handling for IIFE and add related tests
@typescript-eslint
Copy link
Contributor

Thanks for the PR, @Tamashoo!

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.

@netlify
Copy link

netlify bot commented Dec 7, 2025

Deploy Preview for typescript-eslint ready!

Name Link
🔨 Latest commit 2f02909
🔍 Latest deploy log https://app.netlify.com/projects/typescript-eslint/deploys/6935909d70fab9000842a50d
😎 Deploy Preview https://deploy-preview-11826--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: 97 (🟢 up 2 from production)
Accessibility: 97 (no change from production)
Best Practices: 100 (no change from production)
SEO: 92 (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 project configuration.

@nx-cloud
Copy link

nx-cloud bot commented Dec 7, 2025

View your CI Pipeline Execution ↗ for commit 2f02909

Command Status Duration Result
nx test eslint-plugin --coverage=false ✅ Succeeded 4m 57s View ↗
nx run-many -t lint ✅ Succeeded 3m 1s View ↗
nx run-many -t typecheck ✅ Succeeded 1m 56s View ↗
nx test typescript-estree --coverage=false ✅ Succeeded 1s View ↗
nx test eslint-plugin-internal --coverage=false ✅ Succeeded 2s View ↗
nx run integration-tests:test ✅ Succeeded 3s View ↗
nx run types:build ✅ Succeeded 2s View ↗
nx run generate-configs ✅ Succeeded 8s View ↗
Additional runs (29) ✅ Succeeded ... View ↗

☁️ Nx Cloud last updated this comment at 2025-12-07 14:47:01 UTC

@codecov
Copy link

codecov bot commented Dec 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.54%. Comparing base (32b7e89) to head (2f02909).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #11826   +/-   ##
=======================================
  Coverage   90.53%   90.54%           
=======================================
  Files         523      523           
  Lines       53096    53124   +28     
  Branches     8838     8844    +6     
=======================================
+ Hits        48073    48101   +28     
  Misses       5010     5010           
  Partials       13       13           
Flag Coverage Δ
unittest 90.54% <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.24% <100.00%> (+0.15%) ⬆️
🚀 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.

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.

This is looking good, thanks for the PR! I poked at the rule in a bunch of different ways and couldn't figure out amy way to break the new implementation. Nice work 🙂.

Just requesting changes on more thorough testing. This area of TypeScript (inferred types, overloads/signatures) tends to cause bugs in lint rules a lot.

Comment on lines +453 to +457
{
code: `
const UNDEFINED = (() => {})() as undefined;
`,
},
Copy link
Member

Choose a reason for hiding this comment

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

[Testing] There's no need to store these values in variables - other than to test that storing in a variable isn't broken. Let's split this into two, simpler tests:

const value = (() => {})() as undefined;
(() => {})() as undefined;

...and trim out the unnecessary variables from the rest:

const f = () => {};
f() as undefined;

const signatures = functionType.getCallSignatures();

if (signatures.length > 0) {
const returnType = checker.getReturnTypeOfSignature(signatures[0]);
Copy link
Member

Choose a reason for hiding this comment

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

[Testing] This code hardcodes the return type to what's in signatures[0], not the specific resolved signature in case of an overload:

interface Overloaded {
  (): undefined; // or void?
  (value: string): void; // or undefined?
}

(((value) => { }) as Overloaded)("") as undefined;

I couldn't find a way to break this rule with overloads (phew!) - but these cases should be added as tests to prevent regressions. Overload signatures are a mess to deal with and tend to break over time.

// treat it as void (TypeScript infers () => {} as () => undefined, but it should be void)
if (
callee.returnType == null &&
isTypeFlagSet(returnType, ts.TypeFlags.Undefined)
Copy link
Member

Choose a reason for hiding this comment

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

[Testing] Another common soruce of bugs is knowing when to check for a type flag being set vs. something specifically and only being a type. Let's add in at least one test for undefined | void just to be safe:

interface Unioned {
  (): undefined | void;
}

((() => { }) as Unioned)() as undefined;

@JoshuaKGoldberg JoshuaKGoldberg added the awaiting response Issues waiting for a reply from the OP or another party label Dec 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting response Issues waiting for a reply from the OP or another party

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: [no-unnecessary-type-assertion] false positive when casting IIFE with void return type to undefined

2 participants