Skip to content

Instantly share code, notes, and snippets.

@samolabams
Created April 14, 2022 10:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samolabams/46796d51f55a514c4a01c3a7600c94f6 to your computer and use it in GitHub Desktop.
Save samolabams/46796d51f55a514c4a01c3a7600c94f6 to your computer and use it in GitHub Desktop.
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