Skip to content

Control flow

Goal: Use branches to narrow types safely.

Conditions must be boolean — no truthy coercion in --strict lint paths.

Example

function abs(n: number): number {
  if (n < 0) {
    return -n
  }
  return n
}

function label(n: number): string {
  return n > 0 ? "positive" : n < 0 ? "negative" : "zero"
}

Open in playground

Key takeaways

  • Each branch can refine types for locals.
  • Ternary expressions must share a common result type.
  • Prefer === over ==.

Next: Null safety

Documentation