Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

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

Closed
jguddas opened this issue May 4, 2022 · 0 comments · Fixed by #4965
Closed

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

jguddas opened this issue May 4, 2022 · 0 comments · Fixed by #4965
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@jguddas
Copy link
Contributor

jguddas commented May 4, 2022

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.

@jguddas jguddas added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels May 4, 2022
@bradzacher bradzacher added enhancement: plugin rule option New rule option for an existing eslint-plugin rule accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for team members to take a look labels May 4, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepting prs Go ahead, send a pull request that resolves this issue enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants