export type Result = | { ok: true; value: Ok } | { ok: false; error: Err }; export const Ok = (value: Ok) => ({ ok: true, value } as const); export const Err = (error: Err) => ({ ok: false, error } as const); export const val = (r: Result) => r.ok ? r.value : r.error; export const EMPTY_ERR = Err(undefined); /** * Wrap an async operation that may throw an Error (`try-catch` style) into checked exception style * @param op The operation function */ export async function wrapAsync(op: () => Promise): Promise> { try { return op() .then(Ok) .catch(Err); } catch (e) { return Promise.resolve(Err(e as E)); } }