Skip to content

Commit

Permalink
fix(eslint-plugin): [prefer-nullish-coalescing] treat any/unknown as …
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
auvred authored and danvk committed Feb 4, 2024
1 parent 6dde5d0 commit 2ecc5e7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
getTypeFlags,
isLogicalOrOperator,
isNodeEqual,
isNullableType,
isNullLiteral,
isTypeFlagSet,
isUndefinedIdentifier,
nullThrows,
NullThrowsReasons,
Expand Down Expand Up @@ -309,7 +309,7 @@ export default createRule<Options, MessageIds>({
): void {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const type = checker.getTypeAtLocation(tsNode.left);
if (!isNullableType(type)) {
if (!isTypeFlagSet(type, ts.TypeFlags.Null | ts.TypeFlags.Undefined)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,21 @@ x || y;
`,
options: [{ ignorePrimitives: true }],
})),
`
declare const x: any;
declare const y: number;
x || y;
`,
`
declare const x: unknown;
declare const y: number;
x || y;
`,
`
declare const x: never;
declare const y: number;
x || y;
`,
],
invalid: [
...nullishTypeInvalidTest((nullish, type) => ({
Expand Down

0 comments on commit 2ecc5e7

Please sign in to comment.