ProgramGuru

Control Flow Analysis

Understand Control Flow Analysis

Control flow analysis tracks possible types through assignments, conditions, returns, and reachable code.

What you will learn: the purpose of control flow analysis, the rules TypeScript checks, and what its JavaScript behavior looks like in a runnable example.

What you’ll learn

  • Explain how control flow analysis works and where its type rules apply.
  • Predict the exact output of the control flow analysis example.
  • Diagnose a common issue: Ignore neither reachability nor mutation.

Build the mental model

Early returns simplify the remaining path: once a branch returns for number, the rest of a string | number function can treat the value as string. Narrowing is flow-sensitive, so reassignment can change what TypeScript knows later.

Key distinction: TypeScript checks the declared types during development; emitted JavaScript carries out the value-level operation at runtime. A type declaration alone never validates untrusted runtime input.

See the relationship

Read this illustration from the first labeled part to the last. Compare it with the example and identify which parts are type checks and which parts run in JavaScript.

Control Flow Analysis: explained visuallystring | number → Return for number → Rest is stringstring | numberReturn for numberRest is stringFollow the values through the operation
Control Flow Analysis: string | number → Return for number → Rest is string.

Run the complete TypeScript example

This self-contained example uses fixed values, so it runs without reading input or depending on an external service. Paste it into the TypeScript Playground or save it as main.ts.

TypeScript program

function present(value: string | number): string {
  if (typeof value === "number") return `Number ${value.toFixed(0)}`;
  const clean = value.trim();
  return `Text ${clean}`;
}
console.log(present(12));
console.log(present("  yes "));

Verified output

Number 12
Text yes

Run locally: npx tsc --strict --target es2022 --module commonjs main.ts, then node main.js. For projects with a tsconfig.json, build using that configuration instead.

Trace every important step

The first call returns from the number branch, so code below that branch never runs for a number. The remaining path knows value is string and trims it to yes.

Check each step: identify the input values, follow the JavaScript operation in execution order, and match the final expression with the exact output shown. An annotation can reject invalid source before execution, but does not change the value printed.

Avoid these common mistakes

Ignore neither reachability nor mutation. A check performed before reassignment may no longer describe the updated value.

How to debug: reproduce the issue in a minimal example. For compiler errors, inspect the inferred and declared types; for a runtime surprise, log the actual values and follow their references or branches.

Practice and experiment

Rewrite the example using a nested if/else and compare readability and inferred types.

Practice routine: predict the result on paper, edit one part of the code, compile under --strict, run the JavaScript, and explain whether TypeScript caught an error or JavaScript simply behaved differently.

Check your understanding

Why is trim allowed after the early number return?
Choose one answer. You can try again.

Answer and explanation

Only the string alternative can reach that line.

Control flow analysis tracks possible types through assignments, conditions, returns, and reachable code.

Review the key idea

Control Flow Analysis: Control flow analysis tracks possible types through assignments, conditions, returns, and reachable code.

The first call returns from the number branch, so code below that branch never runs for a number. The remaining path knows value is string and trims it to yes.

Before continuing, explain one valid use case, one common pitfall, and the separation between compile-time checking and runtime behavior for this feature.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: User-Defined Type Guards →