Hacker News new | ask | show | jobs
by moonchild 2014 days ago

  function ok_or<T>(x: T | null, msg: string): T {
          if (x === null) throw new Error(msg);
          return x;
  }
1 comments

This is actually more similar to what is going on above:

  function ok_or<T>(x: T | null, msg: string): T | Error {
    if (x === null) return new Error(msg);
    return x;
  }
Thrown values (or exceptions if you like that term) are very much not type-safe in TypeScript.
Does TS have syntax sugar for returning errors, as rust has? If not, I don't see what's interesting about your form.
Sorry for the late reply. It does not. My point was mostly that the TypeScript analogs are either more of a pain or less type safe.