Return a Result Instead of Throwing

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Export a generic type Result<T>: either { ok: true; value: T } or { ok: false; error: string }. Then write parseAge(input): a Result<number> with the age for a whole number from 0 to 150 (surrounding spaces allowed), otherwise ok: false with a short error message. Callers should only reach value after checking ok.

Your code must pass

  • Parses a valid age
  • Rejects text, fractions and out-of-range numbers
  • Has no type errors
  • value is a number once ok is true
  • error is a string once ok is false
  • value can't be read before checking ok