Hacker News new | ask | show | jobs
by frenchy 2014 days ago
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.
1 comments

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.