Skip to content

Narrow contextual type of object literals #18488

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
wants to merge 4 commits into from

Conversation

sandersn
Copy link
Member

Fixes #16457 (first half). This allows the following example to get the correct contextual type of Y for the object literal with type: 'y'.

interface X {
    type: 'x';
    value: 1 | 2 | 3;
    xtra: number;
}

interface Y {
    type: 'y';
    value: 11 | 12 | 13;
    ytra: number;
}

let xy: X | Y = {
    type: 'y',
    value: 11,
    ytra: 12
};

Note that many properties look like discriminants, but aren't particularly good ones. For example, the original example from #16457 has X.value : string and Y.value: 'none' | 'some'. string | 'none' | 'some' is not a good discriminant and is not helped by this PR.

@sandersn
Copy link
Member Author

This is superceded by the "more literal types" PR.

@sandersn sandersn closed this Nov 13, 2017
@mhegazy mhegazy deleted the narrow-contextual-type-of-object-literals branch November 14, 2017 20:02
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants