Skip to main content
🟦 TypeScript Projects

What you're building

Two foundational utilities from functional programming β€” Result and Option β€” that make error handling explicit and impossible to ignore at the type level.

Requirements

Result<T, E>

type Result<T, E = string> = { ok: true; value: T } | { ok: false; error: E }

Write these helpers:

  • ok<T>(value: T): Result<T, never> β€” wraps a success value.
  • err<E>(error: E): Result<never, E> β€” wraps an error.
  • mapResult<T, U, E>(result: Result<T, E>, fn: (v: T) => U): Result<U, E> β€” apply fn to the value if ok, pass the error through unchanged.

Option<T>

type Option<T> = { some: true; value: T } | { some: false }

Write these helpers:

  • some<T>(value: T): Option<T> β€” wraps a present value.
  • none(): Option<never> β€” the absent value.
  • getOrElse<T>(option: Option<T>, fallback: T): T β€” return the value if present, else the fallback.

Stretch goals

  • flatMapResult β€” chain Results without nesting.
  • optionFromNullable β€” wrap a T | null | undefined into an Option.

What this proves

You understand discriminated unions, generics, and the "railway oriented" pattern used in Rust, Haskell, and modern TypeScript codebases to avoid throw/try.

⚑ 40 XP
solution.tsTranspiled & run in your browser
Loading editor…
Tests5 to pass
ok wraps a value
err wraps an error
mapResult transforms ok value
getOrElse returns fallback for none
getOrElse returns value for some

Run your code to check these.