Skip to content

Property goes undetermined on discriminated union #9977

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
saschanaz opened this issue Jul 27, 2016 · 0 comments · Fixed by #10028
Closed

Property goes undetermined on discriminated union #9977

saschanaz opened this issue Jul 27, 2016 · 0 comments · Fixed by #10028
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@saschanaz
Copy link
Contributor

saschanaz commented Jul 27, 2016

TypeScript Version: 2.0 beta

Code

"use strict";

type IDLMemberTypes = OperationMemberType | ConstantMemberType;

interface IDLTypeDescription {
    origin: string;
}

interface InterfaceType {
    members: IDLMemberTypes[];
}

interface OperationMemberType {
    type: "operation";
    idlType: IDLTypeDescription | null;
}

interface ConstantMemberType {
    type: "const";
    idlType: string;
}
// case 1
function insertInterface(callbackType: InterfaceType) {
    for (const memberType of callbackType.members) {
        if (memberType.type === "const") {
            memberType.idlType
            // type of idlType: string, no error
        }
        else if (memberType.type === "operation") {
            memberType.idlType.origin;
            // type of idlType: IDLTypeDescription at least on mouse hover
            /*
            Mutually incompatible messages appear simultaneously:

            1. (property) IDLTypeDescription.origin: string
            2. Property 'origin' does not exist on type 'string'
            */
            (memberType.idlType as IDLTypeDescription);
            // Error: Type 'string' cannot be converted to type 'IDLTypeDescription'
        }
    }
}
// case 2
function insertInterface2(callbackType: InterfaceType) {
    for (const memberType of callbackType.members) {
        if (memberType.type === "operation") {
            memberType.idlType.origin
            // type of idlType: IDLTypeDescription
            // type of origin: string, no error
        }
    }
}
// case 3
function foo(memberType: IDLMemberTypes) {
    if (memberType.type === "const") {
        memberType.idlType
        // type of idlType: string, no error
    }
    else if (memberType.type === "operation") {
        memberType.idlType.origin
        // type of idlType: IDLTypeDescription
        // type of origin: string, no error
    }
}

Expected behavior:

memberType.idlType.origin should be string in both case thanks to the type guard

Actual behavior:

Type guard confuses the type of idlType

@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Jul 28, 2016
@ahejlsberg ahejlsberg self-assigned this Jul 28, 2016
@ahejlsberg ahejlsberg added this to the TypeScript 2.0.1 milestone Jul 28, 2016
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Aug 1, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants