Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unnecessary-condition] false positives with b…
Browse files Browse the repository at this point in the history
…randed types (typescript-eslint#7293)
  • Loading branch information
MBuchalik committed Aug 13, 2023
1 parent 479f9f6 commit 7545695
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/eslint-plugin/src/rules/no-unnecessary-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const isPossiblyFalsy = (type: ts.Type): boolean =>
// PossiblyFalsy flag includes literal values, so exclude ones that
// are definitely truthy
.filter(t => !isTruthyLiteral(t))
// Intersections like `string & {}` can also be possibly falsy,
// requiring us to look into the intersection.
.flatMap(type => tsutils.intersectionTypeParts(type))
.some(type => isTypeFlagSet(type, ts.TypeFlags.PossiblyFalsy));

const isPossiblyTruthy = (type: ts.Type): boolean =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ const result2 = foo() == null;
necessaryConditionTest('null | object'),
necessaryConditionTest('undefined | true'),
necessaryConditionTest('void | true'),
// "branded" type
necessaryConditionTest('string & {}'),
necessaryConditionTest('string & { __brand: string }'),
necessaryConditionTest('number & {}'),
necessaryConditionTest('boolean & {}'),

necessaryConditionTest('any'), // any
necessaryConditionTest('unknown'), // unknown
Expand Down

0 comments on commit 7545695

Please sign in to comment.