Skip to content

Order matters while Resolving a Union Type and its conditional keys  #29703

@azizhk

Description

@azizhk

TypeScript Version: 3.4.0-dev.20190202

Search Terms: Conditional Resolution, Optionals

Code

interface RangeProps {
    range?: false;
    onApply?: (value: Date) => void;
}
interface SingleProps {
    range: true;
    onApply?: (value: [Date, Date]) => void;
}
type CalendarProps = RangeProps | SingleProps;

function apply (props: CalendarProps) {
    if (props.onApply) {
        if (props.range) {
            // Should work as already inside props.onApply check.
            props.onApply([new Date(), new Date()]);
        } else {
            // Should work as already inside props.onApply check.
            props.onApply(new Date());
        }
    }
}

Expected behavior:
Above code should not throw error.

Actual behavior:
Error at lines after comment: Cannot invoke an object which is possibly 'undefined'.

Playground Link:

Related Issues:
Tried my best, sorry but could not find any
Please also change/suggest the title of issue if it does not reflect on the error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions