Add classes used for better management of errors

pull/73/head
MillenniumEarl 2021-02-25 19:06:57 +01:00
parent 827564ca05
commit 20e3d863ed
1 changed files with 49 additions and 0 deletions

View File

@ -0,0 +1,49 @@
export type Result<L, A> = Failure<L, A> | Success<L, A>;
export class Failure<L, A> {
readonly value: L;
constructor(value: L) {
this.value = value;
}
isFailure(): this is Failure<L, A> {
return true;
}
isSuccess(): this is Success<L, A> {
return false;
}
applyOnSuccess<B>(_: (a: A) => B): Result<L, B> {
return this as any;
}
}
export class Success<L, A> {
readonly value: A;
constructor(value: A) {
this.value = value;
}
isFailure(): this is Failure<L, A> {
return false;
}
isSuccess(): this is Success<L, A> {
return true;
}
applyOnSuccess<B>(func: (a: A) => B): Result<L, B> {
return new Success(func(this.value));
}
}
export const failure = <L, A>(l: L): Result<L, A> => {
return new Failure(l);
};
export const success = <L, A>(a: A): Result<L, A> => {
return new Success<L, A>(a);
};