Skip to content

[no-unnecessary-condition] optional chaining clarification #1977

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
TomPridham opened this issue May 5, 2020 · 3 comments · Fixed by #2138
Closed

[no-unnecessary-condition] optional chaining clarification #1977

TomPridham opened this issue May 5, 2020 · 3 comments · Fixed by #2138
Labels
bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@TomPridham
Copy link

i just want some clarification on this bullet point in https://github.com/typescript-eslint/typescript-eslint/blob/master/packages/eslint-plugin/docs/rules/no-unnecessary-condition.md .

Base values of optional chain expressions

does that mean it will only check the first value in a chain?
e.g. foo?.bar
or will it continue to check down the chain but stop at qux?
e.g. foo?.bar?.baz?.qux

@TomPridham TomPridham added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels May 5, 2020
@TomPridham TomPridham changed the title [no-unnecessary-expression] clarification [no-unnecessary-condition] clarification May 5, 2020
@TomPridham TomPridham changed the title [no-unnecessary-condition] clarification [no-unnecessary-condition] optional chaining clarification May 5, 2020
@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for team members to take a look labels May 5, 2020
@bradzacher
Copy link
Member

bradzacher commented May 5, 2020

I just looked into this.
Interestingly, the rule incorrectly false negatives on

type T = { a: { b: { c: string } } }
declare const x: T | null;
x?.a?.b?.c;

why? Because it inspects the type of the expression being optional chained.
The type of x?.a is T['a'] | undefined.
So as far as the current logic is concerned, that's a valid optional chain target.

It should be possible to work around this though.

@bradzacher bradzacher added the good first issue Good for newcomers label May 5, 2020
@aggmoulik
Copy link
Contributor

Hy @bradzacher I want to confirm about the issue that we have to make these conditions valid optional chaining.

type T = { a: { b: { c: string } } }
declare const x: T | null;
x?.a?.b?.c;

@bradzacher
Copy link
Member

correct.

declare const x: { a: { b: { c: string } } } | null;
    x?.a?.b?.c;
//   ^^ valid
//      ^^ ^^ both error

declare const y: { a: { b?: { c: string } } } | null;
    y?.a?.b?.c;
//   ^^    ^^ valid
//      ^^ error

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working good first issue Good for newcomers package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants