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

Switch on typeof for keyof type loses information #27181

Closed
jack-williams opened this issue Sep 18, 2018 · 2 comments · Fixed by #27157
Closed

Switch on typeof for keyof type loses information #27181

jack-williams opened this issue Sep 18, 2018 · 2 comments · Fixed by #27157
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@jack-williams
Copy link
Collaborator

TypeScript Version: master 83fe1ea

Search Terms: switch typeof keyof intersection

Code

function keyofNarrowing<S extends { [K in keyof S]: string }>(k: keyof S) {
    switch (typeof k) {
        case 'number': k; return; // k has type number, should be keyof S & number
        case 'symbol': k; return; // k has type number, should be keyof S & symbol
        case 'string': k; return; // k has type number, should be keyof S & string
    }
}

Expected behavior:
The value k should narrow to an intersection type to retain keyof type.

Actual behavior:
The value k narrows to a primitive.

Playground Link: N/A (Playground not on this version).

@ahejlsberg
Copy link
Member

This one is fixed by #27157.

@ahejlsberg ahejlsberg self-assigned this Sep 18, 2018
@ahejlsberg ahejlsberg added Fixed A PR has been merged for this issue Bug A bug in TypeScript labels Sep 18, 2018
@ahejlsberg ahejlsberg added this to the TypeScript 3.1 milestone Sep 18, 2018
@jack-williams
Copy link
Collaborator Author

jack-williams commented Sep 18, 2018

Great, I'll remove the fix I put in for #27182 (but will keep the tests). This will mean #27182 is blocked on #27157 but that seems ok I think.

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

Successfully merging a pull request may close this issue.

2 participants