Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

fix(eslint-plugin): [prefer-nullish-coalescing] treat any/unknown as non-nullable #8262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions 4 packages/eslint-plugin/src/rules/prefer-nullish-coalescing.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Josh-Cena threading #8262 (comment):

I'm good with this solution 👍 I wonder though, shouldn't we recommend switching to nullish coalescing for any/unknown too, with an option to turn that off, just like primitives? Because any/unknown are also nullable, it doesn't make no sense to use nullish coalescing.

Hmm, good question. I think if the type is explicitly not known then it'd be risky to suggest making any concrete changes to its handling. I'd be in support of this as an opt-in in for v6 and maybe an opt-out for v7.

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;
`,
auvred marked this conversation as resolved.
Show resolved Hide resolved
],
invalid: [
...nullishTypeInvalidTest((nullish, type) => ({
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.