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-condition] false positives with branded types#7466

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

Merged
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
12 changes: 11 additions & 1 deletionpackages/eslint-plugin/src/rules/no-unnecessary-condition.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -28,13 +28,23 @@ const isTruthyLiteral = (type: ts.Type): boolean =>
const isPossiblyFalsy = (type: ts.Type): boolean =>
tsutils
.unionTypeParts(type)
// Intersections like `string & {}` can also be possibly falsy,
// requiring us to look into the intersection.
.flatMap(type => tsutils.intersectionTypeParts(type))
Comment on lines +31 to +33
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

This works because:

  • if it is not an intersection,intersectionTypeParts(type) just returns the original type, making the function behave like before
  • if it is something likestring & {},intersectionTypeParts(type) returns an array containing the typesstring and{}.string in this example is possibly falsy, making our function correctly report that the entire thing is possibly falsy
  • if we have something likestring & number, the type checker directly reports this type asnever, i.e. here we shouldn't even see that this was ever defined as an intersection in the first place

Choose a reason for hiding this comment

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

[External] Heh, I wonder ifts-api-utils should export a function liketypeParts that essentially callsintersectionTypeParts(unionTypeParts(type))...? Not a blocker for this PR, just ruminating.

JoshuaKGoldberg/ts-api-utils#258

// PossiblyFalsy flag includes literal values, so exclude ones that
// are definitely truthy
.filter(t => !isTruthyLiteral(t))
.some(type => isTypeFlagSet(type, ts.TypeFlags.PossiblyFalsy));

const isPossiblyTruthy = (type: ts.Type): boolean =>
tsutils.unionTypeParts(type).some(type => !tsutils.isFalsyType(type));
tsutils
.unionTypeParts(type)
.map(type => tsutils.intersectionTypeParts(type))
.some(intersectionParts =>
// It is possible to define intersections that are always falsy,
// like `"" & { __brand: string }`.
intersectionParts.every(type => !tsutils.isFalsyType(type)),
);

// Nullish utilities
const nullishFlag = ts.TypeFlags.Undefined | ts.TypeFlags.Null;
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -83,6 +83,34 @@ const result2 = foo() == null;
necessaryConditionTest('null | object'),
necessaryConditionTest('undefined | true'),
necessaryConditionTest('void | true'),
// "branded" type
necessaryConditionTest('string & {}'),
necessaryConditionTest('string & { __brand: string }'),
necessaryConditionTest('number & { __brand: string }'),
necessaryConditionTest('boolean & { __brand: string }'),
necessaryConditionTest('bigint & { __brand: string }'),
necessaryConditionTest('string & {} & { __brand: string }'),
necessaryConditionTest(
'string & { __brandA: string } & { __brandB: string }',
),
necessaryConditionTest('string & { __brand: string } | number'),
necessaryConditionTest('(string | number) & { __brand: string }'),
necessaryConditionTest('string & ({ __brand: string } | number)'),
necessaryConditionTest('("" | "foo") & { __brand: string }'),
necessaryConditionTest(
'(string & { __brandA: string }) | (number & { __brandB: string })',
),
necessaryConditionTest(
'((string & { __brandA: string }) | (number & { __brandB: string }) & ("" | "foo"))',
),
necessaryConditionTest(
'{ __brandA: string} & (({ __brandB: string } & string) | ({ __brandC: string } & number))',
),
necessaryConditionTest(
'(string | number) & ("foo" | 123 | { __brandA: string })',
),

necessaryConditionTest('string & string'),

necessaryConditionTest('any'), // any
necessaryConditionTest('unknown'), // unknown
Expand DownExpand Up@@ -645,6 +673,7 @@ const t1 = b2 && b1 ? 'yes' : 'no';
unnecessaryConditionTest('null', 'alwaysFalsy'),
unnecessaryConditionTest('void', 'alwaysFalsy'),
unnecessaryConditionTest('never', 'never'),
unnecessaryConditionTest('string & number', 'never'),

// More complex logical expressions
{
Expand DownExpand Up@@ -1821,5 +1850,37 @@ foo &&= null;
},
],
},

// "branded" types
unnecessaryConditionTest('"" & {}', 'alwaysFalsy'),
unnecessaryConditionTest('"" & { __brand: string }', 'alwaysFalsy'),
unnecessaryConditionTest(
'("" | false) & { __brand: string }',
'alwaysFalsy',
),
unnecessaryConditionTest(
'((string & { __brandA: string }) | (number & { __brandB: string })) & ""',
'alwaysFalsy',
),
unnecessaryConditionTest(
'("foo" | "bar") & { __brand: string }',
'alwaysTruthy',
),
unnecessaryConditionTest(
'(123 | true) & { __brand: string }',
'alwaysTruthy',
),
unnecessaryConditionTest(
'(string | number) & ("foo" | 123) & { __brand: string }',
'alwaysTruthy',
),
unnecessaryConditionTest(
'((string & { __brandA: string }) | (number & { __brandB: string })) & "foo"',
'alwaysTruthy',
),
unnecessaryConditionTest(
'((string & { __brandA: string }) | (number & { __brandB: string })) & ("foo" | 123)',
'alwaysTruthy',
),
],
});

[8]ページ先頭

©2009-2025 Movatter.jp