Skip to content

False positive for "Unreachable code detected" for code following switch/case with allowUnreachableCode: false #59876

@dgollahon

Description

@dgollahon

🔎 Search Terms

"false positive allowUnreachableCode"
"allowUnreachableCode case statement"
"Unreachable code detected case statement"

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about switch/case

⏯ Playground Link

https://www.typescriptlang.org/play/?#code/C4TwDgpgBAYg9nKBeKAiARgQwE6qgHzSwC9UAoMgYzgDsBnYKAMxuSgAoA3TAGwFcIALlgIAlMJp8AtugjZkAPigBvMlHVQ6AdwCWwSgAsuvAaJVqNlypjrQMOVIIuWXUbBGB9srAIwBuZxdrWyJMUidXV3dPbygAJgDLAF8KS2ivVhtbbGAAOQhOOXZUGjhIAEJUUQCUqloGZho4tmN+IRE4cShJGTlFc0ttPUNW0wHI4LssXAjItI8MqH9AqxspsMcVyPTYhK31ABMIJkw+HmBZufUdzLpsvIKikrKISurAlNrqekYWAGYWtw2sJ4J0JNJZPIkEpVJYdExRtAkMjQrgzLDtgtYstklAIDwQvCOECBMgUfZSOj9m4saw9slAoEblAsnIHoVsMVShUqjUKEw+DRKMAdLQWXc2fkOewpBA7pgAOYQAD8wgY2B0NAVXRoj3kGOABmwcC03QgpoAothjZzZfKlVBlcq0AAVAw6OhQahHKBaGx4gAekGFEAOUGAiFkUEF7kwhkw6B4EAAdLyyLUWMUEGA6Hh-aDRGQWHEs2Vc+KOoX-qWc3nPQWyEA

💻 Code

type Foo = "bar" | "baz"

const fn = (value: Foo): number => {
    switch(value) {
        case "bar":
            return 1;
        case "baz":
            return 2;
    }

    return assertNever("nope!");
}

🙁 Actual behavior

return assertNever("nope!");

is highlighted as "Unreachable code detected". The docs, however, state:

This does not affect errors on the basis of code which appears to be unreachable due to type analysis.

🙂 Expected behavior

No error. This should be the same as the other case form, the if form, and various similar constructs where even though it's allegedly exhaustive, that's just based off of type information (I would like to have a runtime assertion in this case).

Additional information about the issue

Possibly related to: #18882

Metadata

Metadata

Assignees

No one assigned

    Labels

    Awaiting More FeedbackThis means we'd like to hear from more people who would be helped by this featureSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions