← Back to context

Comment by yen223

2 years ago

I recall having to add that dummy 'never' branch in the past, but I haven't had to do that in newer projects on recent versions of Typescript.

I just tried it in the online playground. I didn't get any sort of error with one of the cases unhandled. https://www.typescriptlang.org/play/?noFallthroughCasesInSwi...

  • Hmm. I think this is because in that situation, all cases (including the unhandled one) are "correctly" returning undefined.

    I can see that if I made the branch return a string, Typescript will correctly show the "Not all code paths return a value" error

    • Yep, or if you declare an explicit return type for the function, TS correctly gives a "Function lacks ending return statement and return type does not include 'undefined'." error. But if you have a function that's purely side-effecting, I think you still have to do something manual.