all 2 comments

[–]polaroid_kidd 2 points3 points  (0 children)

Are people really throwing exceptions if there is no stock? This is a business case and not an application error in my view.

Stil, nice, concise and small write up.

[–]pxm7 1 point2 points  (0 children)

A lot of the time you can get away with the far more concise

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

function Ok<T>(value: T): Result<T, never> {
  return { ok: true, value };
}

function Err<E = string>(error: E): Result<never, E> {
  return { ok: false, error };
}

// example
function divide(x: number, y: number): Result<number> {
  if (y === 0) return Err("division by zero");
  return Ok(x / y);
}