Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
azizhk opened this issue Feb 2, 2019 · 2 comments
Closed

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

azizhk opened this issue Feb 2, 2019 · 2 comments
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed

Comments

@azizhk
Copy link
Contributor

azizhk commented Feb 2, 2019

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.

@RyanCavanaugh RyanCavanaugh added the Design Limitation Constraints of the existing architecture prevent this from being fixed label Feb 5, 2019
@RyanCavanaugh
Copy link
Member

Basically a duplicate of #28131

@azizhk
Copy link
Contributor Author

azizhk commented Feb 5, 2019

Closing then in favor of #28131

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Design Limitation Constraints of the existing architecture prevent this from being fixed
Projects
None yet
Development

No branches or pull requests

2 participants