Closed
Description
Bug Report
🔎 Search Terms
interface optional properties props nullable nonnullable non-nullable control flow analysis type narrow
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed all the FAQ entries
⏯ Playground Link
Playground link with relevant code
💻 Code
interface NullableProps {
nullableProp?: string
}
type RequiredProps = Required<NullableProps>
declare function withRequiredProps(requiredProps: RequiredProps): any;
function withNullableProps(props: NullableProps) {
if (!props.nullableProp) return;
/** this is correct */
type ProperlyTypeNarrowedToBeNonNullable = typeof props.nullableProp
/** but this is incorrect */
type IncorrectlyStillNullableProps = typeof props;
/**
* Argument of type 'NullableProps' is not assignable to parameter of type 'Required<NullableProps>'.
* Types of property 'someNullableProp' are incompatible.
* Type 'string | undefined' is not assignable to type 'string'.
* Type 'undefined' is not assignable to type 'string'.(2345)
*/
withRequiredProps(props)
}
🙁 Actual behavior
the line
if (!props.nullableProp) return;
does not narrow props
to be { nullableProp: NonNullable }
🙂 Expected behavior
the line
if (!props.nullableProp) return;
should narrow props
to be { nullableProp: NonNullable }
Metadata
Metadata
Assignees
Labels
No labels