Skip to content

Switch over discriminated union inside while loop is broken #27493

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
WiseBird opened this issue Oct 2, 2018 · 2 comments
Closed

Switch over discriminated union inside while loop is broken #27493

WiseBird opened this issue Oct 2, 2018 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@WiseBird
Copy link

WiseBird commented Oct 2, 2018

TypeScript Version: 3.2.0-dev.20181002

Search Terms: tagged/discriminated unions unreachable/conditions

Code

enum Types { Str = 1, Num = 2 }
type Instance = StrType | NumType;
interface StrType {
    type: Types.Str;
    value: string;
    length: number;
}
interface NumType {
    type: Types.Num;
    value: number;
}

function func(inst: Instance) {
    if (false) {
        switch (inst.type) {
            case Types.Str: {
                console.log(inst.value.length);
                break;
            }
            case Types.Num: {
                console.log(inst.value.toExponential);
                break;
            }
        }
    }
}

Expected behavior:
No errors

Actual behavior:

Error:(17, 40) TS2339: Property 'length' does not exist on type 'string | number'.
  Property 'length' does not exist on type 'number'.
Error:(21, 40) TS2339: Property 'toExponential' does not exist on type 'string | number'.
  Property 'toExponential' does not exist on type 'string'.

Code

function func2(inst: Instance) {
    while (true) {
        switch (inst.type) {
            case Types.Str: {
                console.log(inst.value.length);
                break;
            }
            case Types.Num: {
                console.log(inst.value.toExponential);
                break;
            }
        }
    }
}

Expected behavior:
No errors

Actual behavior:

Error:(21, 40) TS2339: Property 'toExponential' does not exist on type 'string'.

Playground Link:
Playground

Related Issues:
Not found

@RyanCavanaugh
Copy link
Member

Duplicate #26914 - search for "unreachable" 🙃

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 2, 2018
@WiseBird
Copy link
Author

WiseBird commented Oct 2, 2018

@RyanCavanaugh, what about second case?

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed Duplicate An existing issue was already created labels Oct 2, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.2 milestone Oct 2, 2018
@ahejlsberg ahejlsberg changed the title Tagged union not narrowed with switch when unreachable or inside while condition Switch over discriminated union inside while loop is broken Oct 8, 2018
@ahejlsberg ahejlsberg added the Fixed A PR has been merged for this issue label Oct 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants