Either

The Either algebraic data type is the union of the Just and Nothing types:

    type Either<T1, T2> = Just<T1> | Nothing<T2>;

The Just type is a Functor used to represent a non-nullable value:

class Nothing<T> { 
    public static of<TVal>(val?: TVal) { 
        return new Nothing(val); 
    } 
    private _value: T|undefined; 
    public constructor(val?: T) { 
        this._value = val; 
    } 
    public map<TMap>(fn: (val: T) => TMap) { 
        if (this._value !== undefined) { 
            return new Nothing<TMap>(fn(this._value)); 
        } else { 
            return new Nothing<TMap>(this._value as any); 
        } 
    } 
} 

The Nothing type represents the lack of a value:

class Just<T> { public static of<TVal>(val: TVal) { return new Just(val); } private _value: T; public constructor(val: T) { this._value = val; } public map<TMap>(fn: ...

Get Learning TypeScript 2.x - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.