Skip to content

Nested Switch infers additional undefined on return type. #23281

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
sledorze opened this issue Apr 9, 2018 · 1 comment
Closed

Nested Switch infers additional undefined on return type. #23281

sledorze opened this issue Apr 9, 2018 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@sledorze
Copy link

sledorze commented Apr 9, 2018

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:

@sledorze
Copy link
Author

Ok duplicate of #11572

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Apr 10, 2018
@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants