Skip to content

Switch on typeof for keyof type loses information #27181

Closed
@jack-williams

Description

@jack-williams

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).

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions