10000 feat(type-utils): isNullableType add Void logic (#9937) · ronami/typescript-eslint@3710c9c · GitHub
[go: up one dir, main page]

Skip to content

Commit 3710c9c

Browse files
feat(type-utils): isNullableType add Void logic (typescript-eslint#9937)
* fix: isNullableType util add Void * fix: lint error
1 parent 042746f commit 3710c9c

File tree

4 files changed

+13
-4
lines changed

4 files changed

+13
-4
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -644,10 +644,9 @@ export default createRule<Options, MessageId>({
644644
? !isCallExpressionNullableOriginFromCallee(node)
645645
: true;
646646

647-
const possiblyVoid = isTypeFlagSet(type, ts.TypeFlags.Void);
648647
return (
649648
isTypeFlagSet(type, ts.TypeFlags.Any | ts.TypeFlags.Unknown) ||
650-
(isOwnNullable && (isNullableType(type) || possiblyVoid))
649+
(isOwnNullable && isNullableType(type))
651650
);
652651
}
653652

packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ export default createRule<Options, MessageIds>({
243243

244244
const type = getConstrainedTypeAtLocation(services, node.expression);
245245

246-
if (!isNullableType(type) && !isTypeFlagSet(type, ts.TypeFlags.Void)) {
246+
if (!isNullableType(type)) {
247247
if (
248248
node.expression.type === AST_NODE_TYPES.Identifier &&
249249
isPossiblyUsedBeforeAssigned(node.expression)

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,15 @@ foo?.()?.toExponential();
867867
type Foo = { [key: string]: () => number | undefined } | null;
868868
declare const foo: Foo;
869869
foo?.['bar']()?.toExponential();
870+
`,
871+
`
872+
declare function foo(): void | { key: string };
873+
const bar = foo()?.key;
874+
`,
875+
`
876+
type fn = () => void;
877+
declare function foo(): void | fn;
878+
const bar = foo()?.();
870879
`,
871880
{
872881
languageOptions: { parserOptions: optionsWithExactOptionalPropertyTypes },

packages/type-utils/src/predicates.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ export function isNullableType(type: ts.Type): boolean {
1515
ts.TypeFlags.Any |
1616
ts.TypeFlags.Unknown |
1717
ts.TypeFlags.Null |
18-
ts.TypeFlags.Undefined,
18+
ts.TypeFlags.Undefined |
19+
ts.TypeFlags.Void,
1920
);
2021
}
2122

0 commit comments

Comments
 (0)
0