Skip to content

Switch on typeof does not narrow unknown correctly. #27180

Closed
@jack-williams

Description

@jack-williams

TypeScript Version: master 83fe1ea

Search Terms: switch typeof unknown

Code

// @strictNullChecks: true
function unknownNarrowing(x: unknown) {
    switch (typeof x) {
        case 'function': x; return; // x is unknown, should be Function
        case 'object': x; return; // x is unknown, should be object | null
    }
}

Expected behavior:

  • Narrowing unknown in switch at clause "function" should result in type Function.
  • Narrowing unknown in switch at clause "object" should result in type object | null.

Actual behavior:

  • Value of type unknown does not narrow.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions