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

fix(eslint-plugin): [no-unnecessary-type-assertion] false positive on non-null assertion after an implicitly-any variable gets initialised inside conditional block#11082

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

Open
mdm317 wants to merge1 commit intotypescript-eslint:main
base:main
Choose a base branch
Loading
frommdm317:11054-no-unnecessary-type-assertion
Open
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -118,9 +118,11 @@ export default createRule<Options, MessageIds>({
if (
// is it `const x!: number`
declaration.initializer == null &&
declaration.exclamationToken == null &&
declaration.type != null
declaration.exclamationToken == null
) {
if (declaration.type == null) {
return true;

Choose a reason for hiding this comment

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

We're definitely in the right ballpark, but I notice that this introduces a regression with the following case:

functionfoo(){letxif(Math.random()>0.5){x=3;}else{x=4;}// should be flagged as unnecessaryx!;}

I wonder if it's possible to get that case correct still? (it may or may not be feasible to prevent some false positives or negatives)

mdm317 reacted with eyes emoji

Choose a reason for hiding this comment

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

actually - I think you can ignore this regression... This is most likely a symptom of#10334 /microsoft/TypeScript#60514

Copy link
ContributorAuthor

@mdm317mdm317Apr 18, 2025
edited
Loading

Choose a reason for hiding this comment

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

@kirkwaiblinger
Thanks for review! 🙏

The regression also occurs in this simplified case:

function foo() {  let x;  x =4;  // should be flagged as unnecessary  x!;}

This is most likely a symptom of#10334 /microsoft/TypeScript#60514

As you mentioned, it's not possible to fix that case for now.

Should I close this PR until that issue is resolved?

kirkwaiblinger reacted with eyes emoji

Choose a reason for hiding this comment

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

Hmmm. I'm not sure which choice is the lesser of two evils here... fixing the bug at the cost of that rather undesirable-looking regression, or marking the bug as blocked. I'm gently leaning towards making this work as blocked unless we can find away to proceed while mitigating this regression 🤔 But then again if it was that important of a case, you'd think we might have a test case that caught it?

Looking for thoughts from @typescript-eslint/triage-team too.

mdm317 reacted with thumbs up emoji

Choose a reason for hiding this comment

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

Yeah I don't really want to wade into these murky waters 🙃. +1 on marking as blocked.

kirkwaiblinger and mdm317 reacted with thumbs up emoji
}
// check if the defined variable type has changed since assignment
const declarationType = checker.getTypeFromTypeNode(declaration.type);
const type = getConstrainedTypeAtLocation(services, node);
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -429,6 +429,13 @@ declare function foo<T extends unknown>(bar: T): T;
const baz: unknown = {};
foo(baz!);
`,
`
let x;
if (Math.random() > -1) {
x = 2;
}
x!;
`,
],

invalid: [
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp