Skip to content

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

Description

TypeScript Version: 83fe1ea

Search Terms: switch typeof narrowing narrows

Code

// @strictNullChecks: truefunctionnarrowingNarrows(x: {}|undefined){switch(typeofx){case'string': x;return;// x is {}, should be stringcase'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

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