> ## 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 Else Clause

The `else` clause is a fundamental control flow construct that dictates the execution path when its associated `if` condition evaluates to a falsy value. In TypeScript, the `else` block is integral to control flow analysis; the compiler automatically applies the logical complement of any type guards evaluated in the preceding `if` statement to narrow types within the `else` scope.

## Syntax

The `else` clause must immediately follow an `if` block or an `else if` block.

```typescript theme={"dark"}
if (condition) {
  // Statements executed if condition is truthy
} else {
  // Statements executed if condition is falsy
}
```

## Control Flow Analysis and Type Narrowing

TypeScript's type checker performs static analysis on `if/else` statements. When a union type is evaluated using a type guard (such as `typeof`, `instanceof`, or custom type predicates) in the `if` condition, TypeScript narrows the type in the `if` block and assigns the remaining constituent types to the `else` block.

```typescript theme={"dark"}
function process(value: string | number | boolean) {
  if (typeof value === "string") {
    // Type of 'value' is narrowed to 'string'
  } else {
    // Type of 'value' is narrowed to 'number | boolean'
  }
}
```

## Chaining with `else if`

Multiple conditions can be evaluated sequentially by chaining an `if` statement directly onto an `else` clause. TypeScript continues to narrow the type down the chain by subtracting the types matched in previous conditions.

```typescript theme={"dark"}
function evaluate(value: string | number | boolean) {
  if (typeof value === "string") {
    // 'value' is 'string'
  } else if (typeof value === "number") {
    // 'value' is 'number'
  } else {
    // 'value' is strictly narrowed to 'boolean'
  }
}
```

## Exhaustiveness Checking

In strict TypeScript configurations, the `else` clause is frequently utilized to enforce exhaustiveness checking on union types. By assigning the narrowed value to a variable of type `never` inside the final `else` block, the compiler will throw an error if the union is modified in the future without a corresponding condition being added to the control flow.

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

function handleState(state: State) {
  if (state === "idle") {
    // state is "idle"
  } else if (state === "loading") {
    // state is "loading"
  } else if (state === "success") {
    // state is "success"
  } else {
    // state is narrowed to 'never'
    // This assignment will cause a compile-time error if a new State is added
    const _exhaustiveCheck: never = state; 
  }
}
```

<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>
