Skip to content

[@typescript-eslint/no-unnecessary-condition] Nullable object can still fail the linter #1897

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
astorije opened this issue Apr 13, 2020 · 4 comments
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period

Comments

@astorije
Copy link
Contributor

astorije commented Apr 13, 2020

Repro

rules:
  '@typescript-eslint/no-unnecessary-condition':
    - error
    - ignoreRhs: true
      checkArrayPredicates: true
import React, { FunctionComponent } from 'react';

export const Foo: FunctionComponent<{ opt?: string; req: string }> = props => (
  <>{props.children || props.req || props.opt}</>
);

export const Bar: FunctionComponent<{ opt?: string; req: string }> = props => (
  <>{props.req || props.opt}</>
);

export const Baz: FunctionComponent<{ req: string }> = props => (
  <>{props.children || props.req}</>
);

Expected Result

Per the @types/react types, children is optional:

type PropsWithChildren<P> = P & { children?: ReactNode };

Because of this, I would expect all examples above to pass the rule.

Actual Result

For a reason I do not understand, the first example (Foo) fails the linter:

     Unnecessary conditional, value is always truthy

     src/Foobar.tsx:4:6
     2 |
     3 | export const Foo: FunctionComponent<{ opt?: string; req: string }> = props => (
   > 4 |   <>{props.children || props.req || props.opt}</>
       |      ^
     5 | );

But we know from the types that props.children is not always truthy.

Additionally:

  • If the issue is indeed that props.children is always truthy, Baz should fail
  • I was wondering if the issue was because props.req is non-nullable (but '' is still falsy) but Bar doesn't fail either

I can't seem to find another combination that makes the linter scream, unless I'm missing something.

Additional Info

Apologies if this has already been reported. I went through existing tickets, opened and closed, and didn't find something similar.

Versions

package version
@typescript-eslint/eslint-plugin 2.27.0
@typescript-eslint/parser 2.27.0
TypeScript 3.8.3
ESLint 6.8.0
node 12.14.0
yarn 1.22.4
@astorije astorije added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Apr 13, 2020
@bradzacher
Copy link
Member

I'm unable to repro this against master.

Do you have strictNullChecks turned on?

@bradzacher bradzacher added awaiting response Issues waiting for a reply from the OP or another party and removed triage Waiting for team members to take a look labels Apr 13, 2020
@astorije
Copy link
Contributor Author

I do have it turned on yeah.

I'm not testing against master though, but 2.27. I could give it a shot against 2.28, but haven't looked at the changelog to see if there was a relevant change yet.

@bradzacher
Copy link
Member

would you be able to chuck it into a repro repo?
there might be some specific combination of @types/react / typescript that triggers the bug (eg #1868 requires a specific version of the types package)

@bradzacher bradzacher added the stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period label May 7, 2020
@astorije
Copy link
Contributor Author

astorije commented Jun 1, 2020

Hey @bradzacher, sorry for the lack of response! It seems like the issue disappeared while upgrading to v3, so it's probably something that got fixed after v2.27.0. I'll re-open if it reappears.
Thanks again for your amazing work!!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 2, 2020
@bradzacher bradzacher removed the awaiting response Issues waiting for a reply from the OP or another party label Aug 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin stale PRs or Issues that are at risk of being or have been closed due to inactivity for a prolonged period
Projects
None yet
Development

No branches or pull requests

2 participants