Switch statements are sometimes considered a code smell, but when they do make sense to use, you’d better make sure you don’t forget a single case. Luckily, TypeScript can help.

In this article, I won’t discuss the reasons why you should avoid switch statements in your applications (e.g., they may easily violate the DRY and Open/Closed principles, may introduce subtle bugs if you forget a break, etc) . It is pretty widely known that switch statements often leads to code that isn’t SOLID.
I also won’t explain the alternatives to writing switch statements (e.g., the strategy pattern, the command pattern, higher-order functions, etc).
Instead, I’ll assume that you know what you’re doing and that the switch statement is a good solution for your use case.
I’ll show you how to leverage TypeScript’s never type to ensure that you did cover all the possible cases, for instance when using a switch against an enumeration.

#typescript #web-development #javascript

Never miss a Switch Case with TypeScript
1.60 GEEK