Skip to content

typeof switch filters union types but does not narrow them #27335

Closed
@jack-williams

Description

@jack-williams

TypeScript Version: 83fe1ea

Search Terms: switch typeof narrowing narrows

Code

// @strictNullChecks: true
function narrowingNarrows(x: {} | undefined) {
    switch (typeof x) {
        case 'string': x; return; // x is {}, should be string
        case 'number': x; return; // x is {}, should be number
    } 
}

Expected behavior:

Switching on typeof should narrow union members in addition to filtering.

  • In the first clause, undefined should be filtered and {} should be narrowed to string.
  • In the second clause, undefined should be filtered and {} should be narrowed to number.

Actual behavior:

Switching on typeof only filters unions.

  • In the first clause, undefined is filtered but {} is not narrowed.
  • In the second clause, undefined is filtered but {} is not narrowed.

Playground Link: 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