3 ms·
function ok_or<T>(x: T | null, msg: string): T { if (x === null) throw new Error(msg); return x; }
by moonchild 6y ago
function ok_or<T>(x: T | null, msg: string): T {
if (x === null) throw new Error(msg);
return x;
}
- frenchy 6y agoThis 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.