Skip to content

Commit e1d9c67

Browse files
authored
fix(eslint-plugin): [no-unnecessary-condition] account for optional chaining on potentially void values (#6432)
* Update error message per #5255 discussion * Add void check to no-unnecessary-condition and test case * Cleanup, update type checks
1 parent d6126b8 commit e1d9c67

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

packages/eslint-plugin/src/rules/no-unnecessary-condition.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -574,10 +574,11 @@ export default createRule<Options, MessageId>({
574574
node.type === AST_NODE_TYPES.MemberExpression
575575
? !isNullableOriginFromPrev(node)
576576
: true;
577+
const possiblyVoid = isTypeFlagSet(type, ts.TypeFlags.Void);
577578
return (
578-
isTypeAnyType(type) ||
579-
isTypeUnknownType(type) ||
580-
(isNullableType(type, { allowUndefined: true }) && isOwnNullable)
579+
isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown) ||
580+
(isOwnNullable &&
581+
(isNullableType(type, { allowUndefined: true }) || possiblyVoid))
581582
);
582583
}
583584

packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,10 @@ do {} while (true);
346346
options: [{ allowConstantLoopConditions: true }],
347347
},
348348
`
349+
let variable = 'abc' as string | void;
350+
variable?.[0];
351+
`,
352+
`
349353
let foo: undefined | { bar: true };
350354
foo?.bar;
351355
`,

0 commit comments

Comments
 (0)