Skip to content

A problem with type checking in if statement. #31059

Closed
@Arfey

Description

@Arfey

TypeScript Version: 3.4.2

Search Terms:
A problem with type checking in if statement.

Code

{({ loading, data, error }) => {
    const canShow =
      !loading &&
      !error &&
      data &&
      data.recommendedPrice &&
      data.recommendedPrice.price;
    if (canShow) {
      return (
        <RecommendedPrice
          price={data.recommendedPrice.price}
          isActive={isActive}
        />
      );
    }

    return null;
}}

Expected behavior:
all is fine

Actual behavior:

object is possibly 'null'

But when i write something like that all is fine

{({ loading, data, error }) => {
    if (
      !loading &&
      !error &&
      data &&
      data.recommendedPrice &&
      data.recommendedPrice.price
    ) {
      return (
        <RecommendedPrice
          price={data.recommendedPrice.price}
          isActive={isActive}
        />
      );
    }

    return null;
}}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions