Skip to content

Commit 2ecc5e7

Browse files
auvreddanvk
authored andcommitted
fix(eslint-plugin): [prefer-nullish-coalescing] treat any/unknown as non-nullable (typescript-eslint#8262)
* fix(eslint-plugin): [prefer-nullish-coalescing] treat any/unknown as non-nullable * chore: rm unrelated changes * test: add declarations of 'y' variable
1 parent 6dde5d0 commit 2ecc5e7

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
getTypeFlags,
1111
isLogicalOrOperator,
1212
isNodeEqual,
13-
isNullableType,
1413
isNullLiteral,
14+
isTypeFlagSet,
1515
isUndefinedIdentifier,
1616
nullThrows,
1717
NullThrowsReasons,
@@ -309,7 +309,7 @@ export default createRule<Options, MessageIds>({
309309
): void {
310310
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
311311
const type = checker.getTypeAtLocation(tsNode.left);
312-
if (!isNullableType(type)) {
312+
if (!isTypeFlagSet(type, ts.TypeFlags.Null | ts.TypeFlags.Undefined)) {
313313
return;
314314
}
315315

packages/eslint-plugin/tests/rules/prefer-nullish-coalescing.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ x || y;
222222
`,
223223
options: [{ ignorePrimitives: true }],
224224
})),
225+
`
226+
declare const x: any;
227+
declare const y: number;
228+
x || y;
229+
`,
230+
`
231+
declare const x: unknown;
232+
declare const y: number;
233+
x || y;
234+
`,
235+
`
236+
declare const x: never;
237+
declare const y: number;
238+
x || y;
239+
`,
225240
],
226241
invalid: [
227242
...nullishTypeInvalidTest((nullish, type) => ({

0 commit comments

Comments
 (0)
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