Skip to content

Instantly share code, notes, and snippets.

@turbotobias
Last active March 23, 2023 01:48
Show Gist options
  • Save turbotobias/8fb8c324a63beb4bcfc66e30e42892a4 to your computer and use it in GitHub Desktop.
Save turbotobias/8fb8c324a63beb4bcfc66e30e42892a4 to your computer and use it in GitHub Desktop.
Rust Result in Typescript
/** made by Dan Imhoff, the code below is copy-pasted from https://www.huy.rocks/everyday/02-14-2022-typescript-implement-rust-style-result */
export type Result<T, E = undefined> = { ok: true; value: T } | { ok: false; error: E | undefined }
export const Ok = <T>(data: T): Result<T, never> => {
return { ok: true, value: data }
}
export const Err = <E>(error?: E): Result<never, E> => {
return { ok: false, error }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment