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

typeof type guards don't work with switch statements #16068

Closed
Gozala opened this issue May 24, 2017 · 1 comment
Closed

typeof type guards don't work with switch statements #16068

Gozala opened this issue May 24, 2017 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@Gozala
Copy link

Gozala commented May 24, 2017

TypeScript Version: 2.3

Code

declare function parse(input: string): boolean | string | number

const data = parse('whatever')

switch (typeof data) {
    case 'number':
        console.log(data.toFixed())
    case 'string':
        console.log(data.toUpperCase())
    case 'boolean':
        console.log(!data)
}

Expected behavior:

I would expect data type to be refined in each case & for this code to check.

Actual behavior:

data type does not seems to get refined by switch cases, there for errors are reported when data is treated as number and as string:

Property 'toFixed' does not exist on type 'string | number | boolean'.
  Property 'toFixed' does not exist on type 'string'.

Property 'toUpperCase' does not exist on type 'string | number | boolean'.
  Property 'toUpperCase' does not exist on type 'number'.
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label May 24, 2017
@RyanCavanaugh
Copy link
Member

Duplicate #2214.
image

@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants