myHotTake

Tag: TypeScript exhaustive checks

  • How to Ensure Exhaustive Checks in TypeScript Switch Cases

    If you find this story helpful, feel free to like or share it!


    I’m a detective in a city, solving a mysterious case. The city is divided into different districts, each with its own unique character and set of rules. My mission is to ensure every district is accounted for in the investigation, leaving no stone unturned.

    In this case, the districts represent the different cases in a TypeScript switch statement. Each district (or case) has its own way of being handled, much like how each case in a switch statement has its distinct block of code. As a meticulous detective, I need to ensure that every district is considered, so I won’t miss any crucial clues.

    Now, as I’m working through the districts, I have a trusty sidekick, TypeScript, who helps me enforce exhaustive checks. TypeScript is like my magnifying glass, highlighting any districts that haven’t been accounted for. It ensures that I don’t overlook any part of the city, just like it ensures I don’t miss any possible cases in my switch statement.

    As I wrap up my investigation, I encounter an unexpected twist—a new district that wasn’t on my map. This is where TypeScript’s “never” type comes into play. If I miss a district, TypeScript throws a red flag, much like my sidekick nudging me to pay attention. It tells me, “Hey, detective, there’s a district you haven’t considered!”

    By using this approach, I ensure that my investigation is thorough and complete, much like how TypeScript ensures that all possible cases in a switch statement are covered. With TypeScript’s help, I can confidently solve the mystery, knowing that every district has been accounted for and no crucial details have been missed.

    And just like that, with a complete investigation, I can close the case with confidence, knowing that every part of my city has been explored.


    In the world of TypeScript, each district corresponds to a case in a switch statement. Let’s say I’m investigating different types of cases represented by a union type, like so:

    type CaseType = "burglary" | "fraud" | "vandalism";
    
    function investigateCase(caseType: CaseType) {
        switch (caseType) {
            case "burglary":
                console.log("Investigating burglary...");
                break;
            case "fraud":
                console.log("Investigating fraud...");
                break;
            case "vandalism":
                console.log("Investigating vandalism...");
                break;
            default:
                // This should never happen
                const _exhaustiveCheck: never = caseType;
                throw new Error(`Unhandled case: ${caseType}`);
        }
    }

    In this investigation, CaseType is a union type representing all the possible districts (case types) I need to explore. Each case in the switch corresponds to a district with its own handling logic.

    Here’s where the magic happens: the default case. In my detective work, this is like my trusty sidekick, TypeScript, ensuring I didn’t miss any district. The line const _exhaustiveCheck: never = caseType; is my safety net. If I ever encounter a case type that I forgot to handle, TypeScript will alert me by throwing a compile-time error. This tells me, “Detective, there’s a district you haven’t covered!”

    By enforcing this check, I ensure that any future additions to CaseType—like a new district popping up—won’t slip through unnoticed. TypeScript forces me to handle them, ensuring my investigation remains exhaustive.

    Key Takeaways:

    1. Exhaustiveness: Using TypeScript’s type system, you can enforce exhaustive checks in switch statements to ensure all possible cases are covered.
    2. Safety Net: The never type acts as a guard, alerting you at compile time if there’s an unhandled case, much like a detective’s sidekick ensuring no district is missed.
    3. Future-Proof: This approach makes your code robust against future changes, ensuring new cases are handled as soon as they’re introduced.