镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Nested Switch infers additional undefined on return type. #23281

Closed

Description

TypeScript Version:** 2.8.1 and 2.9.0-dev.20180409

nested switch undefined, strictnullcheck

Code
(with strictnullchecks activated)

const foo = (a: 1 | 2 | 3): 'a' | 'b' | 'c' => { // <- Errors here with: Function lacks ending return statement and return type does not include 'undefined'.
  switch (a) {
    case 1:
      return 'a'
    default:
      switch (a) {
        case 2:
          return 'b'
        case 3:
          return 'c'
      }
  }
}

Also note that this typechecks:

const foo = (a: 1 | 2 | 3): 'a' | 'b' | 'c' => { // No error
  switch (a) {
    case 1:
      return 'a'
    default:
          return (() => {
              switch (a) {
                  case 2:
                      return 'b'
                  case 3:
                      return 'c'
              }
          })()
  }
}

Expected behavior:
Should compile fine

Actual behavior:
Errors with "Function lacks ending return statement and return type does not include 'undefined'."

Playground Link:
http://www.typescriptlang.org/play/#src=const%20foo%20%3D%20(a%3A%201%20%7C%202%20%7C%203)%3A%20'a'%20%7C%20'b'%20%7C%20'c'%20%3D%3E%20%7B%0D%0A%20%20switch%20(a)%20%7B%0D%0A%20%20%20%20case%201%3A%0D%0A%20%20%20%20%20%20return%20'a'%0D%0A%20%20%20%20default%3A%0D%0A%20%20%20%20%20%20switch%20(a)%20%7B%0D%0A%20%20%20%20%20%20%20%20case%202%3A%0D%0A%20%20%20%20%20%20%20%20%20%20return%20'b'%0D%0A%20%20%20%20%20%20%20%20case%203%3A%0D%0A%20%20%20%20%20%20%20%20%20%20return%20'c'%0D%0A%20%20%20%20%20%20%7D%0D%0A%20%20%7D%0D%0A%7D%0D%0A

Related Issues:

Activity

  1. sledorze commented on Apr 10, 2018

    @sledorze
    Author

    Ok duplicate of #11572

  2. locked and limited conversation to collaborators on Jul 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions