Skip to content

Switch exhaustiveness not working for typeof any and typeof unknown #39343

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

Closed
cherryblossom000 opened this issue Jun 30, 2020 · 0 comments · Fixed by #39389
Closed

Switch exhaustiveness not working for typeof any and typeof unknown #39343

cherryblossom000 opened this issue Jun 30, 2020 · 0 comments · Fixed by #39389
Labels
Bug A bug in TypeScript Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Milestone

Comments

@cherryblossom000
Copy link
Contributor

TypeScript Version: 3.9.5, 4.0.0-dev.20200630

Search Terms: switch narrowing typeof any unknown

Code
Making x unknown also gives the same results.

const works = (x: any): number => {
  const type = typeof x
  switch (type) {
    case 'string': return 0
    case 'number': return 0
    case 'bigint': return 0
    case 'boolean': return 0
    case 'symbol': return 0
    case 'undefined': return 0
    case 'object': return 0
    case 'function': return 0
  }
}

const alsoWorks = (x: any): number => {
  switch (typeof x as 'string' | 'number' | 'bigint' | 'boolean' | 'symbol' | 'undefined' | 'object' | 'function') {
    case 'string': return 0
    case 'number': return 0
    case 'bigint': return 0
    case 'boolean': return 0
    case 'symbol': return 0
    case 'undefined': return 0
    case 'object': return 0
    case 'function': return 0
  }
}

const doesntWork = (x: any): number => {
  //                   ~~~
  // TS 2366: Function lacks ending return statement and return type does not include 'undefined'.
  switch (typeof x) {
    case 'string': return 0
    case 'number': return 0
    case 'bigint': return 0
    case 'boolean': return 0
    case 'symbol': return 0
    case 'undefined': return 0
    case 'object': return 0
    case 'function': return 0
  }
}

Expected behavior: No error in doesntWork as the switch is exhaustive. This should be the same as works (using a variable) and alsoWorks (using a type assertion).

Actual behavior: Error TS 2366: Function lacks ending return statement and return type does not include 'undefined'. with strictNullChecks.

Playground Link

Related Issues: #34661, #38136, #27180

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jun 30, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jun 30, 2020
@DanielRosenwasser DanielRosenwasser added Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this labels Jul 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Effort: Moderate Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual". Help Wanted You can do this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants