diff --git a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts index 42f12748af90..596dcf798467 100644 --- a/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts +++ b/packages/eslint-plugin/src/rules/no-unnecessary-condition.ts @@ -574,10 +574,11 @@ export default createRule({ node.type === AST_NODE_TYPES.MemberExpression ? !isNullableOriginFromPrev(node) : true; + const possiblyVoid = isTypeFlagSet(type, ts.TypeFlags.Void); return ( - isTypeAnyType(type) || - isTypeUnknownType(type) || - (isNullableType(type, { allowUndefined: true }) && isOwnNullable) + isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown) || + (isOwnNullable && + (isNullableType(type, { allowUndefined: true }) || possiblyVoid)) ); } diff --git a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts index af8b189f8994..6a5f803605f0 100644 --- a/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unnecessary-condition.test.ts @@ -346,6 +346,10 @@ do {} while (true); options: [{ allowConstantLoopConditions: true }], }, ` +let variable = 'abc' as string | void; +variable?.[0]; + `, + ` let foo: undefined | { bar: true }; foo?.bar; `, pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy