Last active
March 23, 2023 01:48
-
-
Save turbotobias/8fb8c324a63beb4bcfc66e30e42892a4 to your computer and use it in GitHub Desktop.
Rust Result in Typescript
This file contains hidden or 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
| /** 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