Closed
Description
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
Labels
No labels