-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Closed
Labels
accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin ruleNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugin
Description
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.
typescript-eslint/packages/eslint-plugin/docs/rules/prefer-nullish-coalescing.md
Lines 9 to 17 in da48527
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.
denolfedenolfe
Metadata
Metadata
Assignees
Labels
accepting prsGo ahead, send a pull request that resolves this issueGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin ruleNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-pluginIssues related to @typescript-eslint/eslint-plugin