8000 [prefer-nullish-coalescing] request for ternary support · Issue #4905 · typescript-eslint/typescript-eslint · GitHub
[go: up one dir, main page]

Skip to content
[prefer-nullish-coalescing] request for ternary support #4905
Closed
@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

      0