export class Fallible { private value?: R; private error?: E; constructor(value?: R, error?: E) { this.value = value; this.error = error; } public getValue(): R { if (!this.value) { throw this.error ? this.error : new Error("No value and no error present"); } return this.value; } public getError(): E { if (!this.error) { throw new Error("No error present"); } return this.error; } public isOk(): boolean { return !this.error && this.value !== undefined; } public static ok(value: R): Fallible { return new Fallible(value); } public static error(error: E): Fallible { return new Fallible(undefined, error); } }