I was looking for a way to exhaustively type-check switch statements when writing Typescript. I went through few iterations with a colleague at work before we arrived at something that worked.
The goal is to always cover all possible cases using case, similarly how language such as Rust forces you to return value as Option<...>.
Note: All code examples were tested on Typescript v5.1.6
Imagine the following example code:
type Id = "A" | "B" | "C";
function getId(id: Id) {
switch (id) {
case
… Read more