There was an error while loading. Please reload this page.
ternary operator, props object, object is possibly null
Playground link with relevant 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 }
example1 throws an error because TS thinks props.value might be null when checking its length.
example1
props.value
There should be no error, like in the other two functions.