Created
April 14, 2022 10:50
-
-
Save samolabams/46796d51f55a514c4a01c3a7600c94f6 to your computer and use it in GitHub Desktop.
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
export type Response<T, E> = Ok<T, E> | Err<T, E>; | |
class Ok<T, E> { | |
readonly value: T; | |
constructor(value: T) { | |
this.value = value; | |
} | |
isOk(): this is Ok<T, E> { | |
return true; | |
} | |
isErr(): this is Err<T, E> { | |
return false; | |
} | |
} | |
class Err<T, E> { | |
readonly value: E; | |
constructor(value: E) { | |
this.value = value; | |
} | |
isOk(): this is Ok<T, E> { | |
return false; | |
} | |
isErr(): this is Err<T, E> { | |
return true; | |
} | |
} | |
export const Result = { | |
ok: <T, E>(value: T): Ok<T, E> => new Ok(value), | |
err: <T, E>(error: E): Err<T, E> => new Err(error), | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment