Skip to content

TypeScript doesn't assume the value type in a ternary operator when it has been assigned before if the value is an object keyΒ #50356

Closed
@albertothedev

Description

@albertothedev

Bug Report

πŸ”Ž Search Terms

ternary operator, props object, object is possibly null

πŸ•— Version & Regression Information

  • This happens in version 4.7.4 and 4.8.0-beta

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Props = {
  value: string | Array<string> | [] | null;
};

function example1(props: Props) {
  const isValueAnArray = Array.isArray(props.value);
  const isValueEmpty = !(isValueAnArray ? !!props.value.length : !!props.value);
                                                       ^ (property) value: string | string[] | [] | null
                                                          Object is possibly 'null'.
}

function example2(props: Props) {
  const isValueAnArray = Array.isArray(props.value);
  const isValueEmpty = !(Array.isArray(props.value) ? !!props.value.length : !!props.value); // No errors
}

function example3(value: string | Array<string> | [] | null) {
  const isValueAnArray = Array.isArray(value);
  const isValueEmpty = !(Array.isArray(value) ? !!value.length : !!value); // No errors
}

πŸ™ Actual behavior

example1 throws an error because TS thinks props.value might be null when checking its length.

πŸ™‚ Expected behavior

There should be no error, like in the other two functions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions