Skip to content

Instantly share code, notes, and snippets.

@LironHazan
Created May 22, 2021 05:37
Show Gist options
  • Save LironHazan/d619844d94106d8020cfc73c9d025cb0 to your computer and use it in GitHub Desktop.
Save LironHazan/d619844d94106d8020cfc73c9d025cb0 to your computer and use it in GitHub Desktop.
snippet for a blog post
export class ResultT<E, T> {
private result: Result<ErrorClassification, T>;
constructor(result: Result<ErrorClassification, T>) {
this.result = result;
}
ok(onSuccessFn: (lifted: T) => ResultT<E, T>): ResultT<E, T> {
return isError(this.result) ? this : onSuccessFn(this.result as T);
}
err(onFailureFn: (error: ErrorClassification) => ResultT<E, T>): ResultT<E, T> {
return isError(this.result) ? onFailureFn(this.result as ErrorClassification) : this;
}
map<A>(transformFn: (a: T) => A): ResultT<never, T> {
this.result = transformFn(this.result as T) as any;
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment