Skip to content

[prefer-nullish-coalescing] request for ternary support #4905

@jguddas

Description

@jguddas

The following cases can all be converted to foo ?? 'a string'.

const foo: any = 'bar';
foo !== undefined && foo !== null ? foo : 'a string';
foo === undefined || foo === null ? 'a string' : foo;

const foo:? string = 'bar';
foo !== undefined ? foo : 'a string';
foo === undefined ? 'a string' : foo;

const foo: string | null = 'bar';
foo !== null ? foo : 'a string';
foo === null ? 'a string' : foo;

This is even hinted at in the docs, but sadly currently not covered.

function myFunc(foo: string | null) {
return foo ?? 'a string';
}
// is equivalent to
function myFunc(foo: string | null) {
return foo !== null && foo !== undefined ? foo : 'a string';
}

Supporting foo ? foo : 'a string' would create conflicts with no-unneeded-ternary which is why I'm against doing so.

I can have a look at creating a PR if there is interest for this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      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