Skip to content

Switch not performing exhaustive check when a default with unreachable assert is specified #45415

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
fcole90 opened this issue Aug 11, 2021 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@fcole90
Copy link

fcole90 commented Aug 11, 2021

Bug Report

🔎 Search Terms

is:issue is:open exhaustive switch

🕗 Version & Regression Information

  • This is the behaviour in every version I tried, and I reviewed the FAQ for entries about switch exhaustiveness and I didn't find anything about this.

⏯ Playground Link

Playground link with relevant code

💻 Code

const assertUnreachable = (x: never): never => {
    throw new Error("Unreachable code")
}

// This is OK for the compiler
const exhaustiveFunctionReturnOK = (value: "a" | "b"): number => {
    switch(value) {
        case "a":
            return 0
        case "b":
            return 1
    }
}

// This is an ERROR for the compiler:
// > Function lacks ending return statement and return type does not include 'undefined'.(2366)
const exhaustiveFunctionMissesReturn = (value: "a" | "b"): number => {
    switch(value) {
        case "a":
            return 0
        case "b":
            return 1
        default:
            assertUnreachable(value)
    }
}

// I think both should be OK instead

🙁 Actual behavior

I have a function with a parameter which is a string union (e.g. value: "a" | "b") and a return of some type (e.g. number). I want to perform an exhaustive switchto decide whatnumberto return based on the value, if it's"a"or"b"`. The compiler correctly identifies that the branches match all the possible values (exhaustive), so it's happy and compiles.

However, if I add a default branch, with an unreachable assert of type never, then the compiler complains that I'm missing a return of type number. This seems weird to me, given that the reason for the assert itself is exactly to ensure that such a branch is actually unreachable, and hence every option of the switch is exhausted.

🙂 Expected behavior

Both behaviours to work and the compiler to understand that a default branch with an unreachable-code assertion is not going to happen and behave like in the case without such a branch, inferring that the only possible return type is number (or whatever type the function is returning).

@fcole90
Copy link
Author

fcole90 commented Aug 11, 2021

Possibly related issue: #36884

@fcole90
Copy link
Author

fcole90 commented Aug 11, 2021

Other possibly related issue: #12825

As for this comment a workaround is to return the assertUnreachable(x: never), like this:

// Now the compiler doesn't complain anymore
const exhaustiveFunctionMissesReturn = (value: "a" | "b"): number => {
    switch(value) {
        case "a":
            return 0
        case "b":
            return 1
        default:
            return assertUnreachable(value)
            //  ^ Note this "return" was added
    }
}

@MartinJohns
Copy link
Contributor

Duplicate of #12825.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 12, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants