type MaybeType = "None" | "Just"; export class Maybe { private value?: T; private type: MaybeType; private constructor(value?: T) { this.value = value; this.type = value == null ? "None" : "Just"; } static just(value: T) { if (!value && typeof value !== "boolean") { throw new Error("You must provide a value"); } return new Maybe(value); } static isJust(t: Maybe) { return t.type === "Just"; } static none() { return new Maybe(); } static isNone(t: Maybe) { return t.type === "None"; } map(f: (a: T) => R): Maybe { if (this.value == null) { return Maybe.none(); } return Maybe.just(f(this.value)); } getValue() { return this.value; } }