Skip to content
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

instance.constructor type narrowing not working in switch #41721

Closed
pushkine opened this issue Nov 29, 2020 · 4 comments
Closed

instance.constructor type narrowing not working in switch #41721

pushkine opened this issue Nov 29, 2020 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@pushkine
Copy link
Contributor

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

@MartinJohns
Copy link
Contributor

Related / duplicate of #28936, #3841. Search terms used: constructor narrow

@pushkine
Copy link
Contributor Author

The issue is that IfBlocks have a special <var>.constructor === <class> handling that isn't mirrored in switch blocks
The issues mentioned by @MartinJohns are not related to switch blocks

@MartinJohns
Copy link
Contributor

You're right. It's actually a duplicate of #16035.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 1, 2020
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants