Skip to content

Discriminated union types lose their narrowed type inside nested function scopes #9258

Closed
@ryanatkn

Description

@ryanatkn

TypeScript Version:

nightly (1.9.0-dev.20160619-1.0)

Code

interface Rectangle {
    kind: "rectangle";
    width: number;
    height: number;
}

interface Circle {
    kind: "circle";
    radius: number;
}

type Shape = Rectangle | Circle;

function test1(s: Shape) {
    if (s.kind === "rectangle") {
        s;  // Rectangle
        setTimeout(() => {
          s; // Rectangle | Circle
        });
    }
}

Expected behavior: both s usages to narrow to type Rectangle

Actual behavior: the s in the nested function scope has type Rectangle | Circle

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions