1 export class Result<T> {
3 private isSuccess: boolean,
4 private error?: string,
11 return !this.isSuccess
15 if (!this.isSuccess) {
16 throw new Error(`Cannot get value of an unsuccessful result: ${this.error}`)
19 return this.value as T
23 if (this.isSuccess || this.error === undefined) {
24 throw new Error('Cannot get an error of a successful result')
30 static ok<U>(value?: U): Result<U> {
31 return new Result<U>(true, undefined, value)
34 static fail<U>(error: string): Result<U> {
36 throw new Error('Attempting to create a failed result without an error')
39 return new Result<U>(false, error)