Last active
July 5, 2022 19:29
-
-
Save thebearingedge/68052390e79a8a0978df3a2a97c71c02 to your computer and use it in GitHub Desktop.
Result type for Typescript, sorta like rust.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Result<Ok, Err extends Error = Error> = | |
| { kind: 'ok', value: Ok } | |
| { kind: 'err', err: Err } | |
const Ok = <T>(value: T): Result<T> => { | |
return { | |
kind: 'ok', | |
value | |
} | |
} | |
const Err = <E extends Error = Error>(err: E): Result<never, E> => { | |
return { | |
kind: 'err', | |
err | |
} | |
} | |
type AnyFunction = (...args: any[]) => any | |
type Safely<F extends AnyFunction> = | |
(...args: Parameters<F>) => Promise<Result<Awaited<ReturnType<F>>>> | |
function safely<F extends AnyFunction>(fn: F): Safely<F> { | |
return async (...args) => { | |
try { | |
return Ok(await fn(...args)) | |
} catch (err) { | |
return Err(err instanceof Error ? err : new Error(`uncaught ${err}`)) | |
} | |
} | |
} | |
async function mayFail() {} | |
const safelyMayFail = safely(mayFail) | |
;(async () => { | |
const result = await safelyMayFail() | |
if (result.kind === 'ok') { | |
// result.value is mayFail's return value | |
} else { | |
// result.err is the Error object | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment