Files
it-know-how/typescript/bits/17-guard-clauses-and-range-validation.md
T
2026-03-27 12:58:01 +01:00

606 B
Executable File

Guard Clauses and Range Validation

A guard clause exits early when input is invalid.

Example from your snippet

if (rangeStart !== undefined && rangeEnd !== undefined && rangeStart > rangeEnd) {
  return [];
}

Why this is good

  • Fails fast.
  • Prevents harder-to-debug logic later.
  • Keeps the main flow cleaner.

Operator notes

  • !==: strict "not equal" comparison.
  • &&: logical AND (all conditions must be true).
  • >: greater-than comparison.

Python comparison

if range_start is not None and range_end is not None and range_start > range_end:
    return []