Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
Closed
Description
The following cases can all be converted tofoo ?? 'a string'.
constfoo:any='bar';foo!==undefined&&foo!==null ?foo :'a string';foo===undefined||foo===null ?'a string' :foo;constfoo:?string='bar';foo!==undefined ?foo :'a string';foo===undefined ?'a string' :foo;constfoo: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 inda48527
| function myFunc(foo:string|null) { | |
| returnfoo??'a string'; | |
| } | |
| // is equivalent to | |
| function myFunc(foo:string|null) { | |
| returnfoo!==null&&foo!==undefined?foo:'a string'; | |
| } |
Supportingfoo ? foo : 'a string' would create conflicts withno-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.