> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript Switch Statement

A `switch` statement is a control flow construct that evaluates a single expression and executes specific blocks of code based on strict equality (`===`) matching against predefined `case` clauses. In TypeScript, `switch` statements integrate directly with the compiler's control flow analysis to provide automatic type narrowing and compile-time exhaustiveness checking.

```text theme={"dark"}
switch (expression) {
  case value1:
    // Statements execute if expression === value1
    break;
  case value2:
    // Statements execute if expression === value2
    break;
  default:
    // Statements execute if no cases match
}
```

## Execution Mechanics

* **Strict Equality:** TypeScript evaluates the `expression` once and compares it to each `case` value using strict equality (`===`). The TypeScript compiler will throw a type error if the type of a `case` value is not *comparable* to the type of the `switch` expression (meaning the types must have some overlap; they do not strictly need to be assignable).
* **Fallthrough:** If a `case` block does not terminate with a `break`, `return`, or `throw` statement, execution automatically "falls through" to the subsequent `case` block, bypassing its evaluation. TypeScript provides the `noFallthroughCasesInSwitch` compiler flag to emit an error when a non-empty `case` block lacks a termination statement.
* **Lexical Scoping:** By default, the entire `switch` statement shares a single lexical scope. Declaring a block-scoped variable (`let` or `const`) in one `case` makes it visible (though potentially uninitialized) to all other cases, preventing redeclaration. To isolate scope, wrap the `case` execution statements in a block `{}`.

```typescript theme={"dark"}
const numericValue = 1;

switch (numericValue) {
  case 1: {
    const scopedVariable = true; // Isolated to this block
    break;
  }
  case 2: {
    const scopedVariable = false; // Valid redeclaration due to block scoping
    break;
  }
}
```

## TypeScript Control Flow Analysis

TypeScript applies specific compile-time behaviors to `switch` statements when evaluating union types, literal types, or enums.

### Type Narrowing

Within a matched `case` block, TypeScript narrows the type of the evaluated expression to the specific literal type or enum member matched by that case.

```typescript theme={"dark"}
function processInput(input: string | number) {
  switch (typeof input) {
    case "string":
      // 'input' is narrowed to type 'string'
      input.toUpperCase(); 
      break;
    case "number":
      // 'input' is narrowed to type 'number'
      input.toFixed(2);
      break;
  }
}
```

### Exhaustiveness Checking

When switching on a finite union type, TypeScript can enforce that all possible variants are explicitly handled. This is achieved by utilizing the `never` type in the `default` clause. If a union member is added later and not handled by a `case`, the compiler will fail to assign the unhandled type to `never`, resulting in a build error.

```typescript theme={"dark"}
type State = "idle" | "loading" | "success";

function handleState(state: State) {
  switch (state) {
    case "idle":
      break;
    case "loading":
      break;
    case "success":
      break;
    default:
      // If a new State is added (e.g., "error"), 'state' will not be 'never' here,
      // triggering a compile-time error on the assignment below.
      const _exhaustiveCheck: never = state;
      return _exhaustiveCheck;
  }
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
