Skip to content

instance.constructor type narrowing not working in switch #41721

Closed
@pushkine

Description

@pushkine

TypeScript Version: 4.2.0-dev.20201127

Search Terms: switch, narrow, constructor

Code

function foo(o: any) {
    switch (o.constructor) {
        case Array: {
            o // o: any
        }
    }
}
function bar(o: any) {
    if (o.constructor === Array) {
        o // o: any[]
    }
}

Expected behavior: Both functions narrow the value to any[]

Actual behavior: Switch fails to narrow value

Playground Link: playground

Related Issues: #2214 (comment), #32774

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions