export class Optional < T > { private readonly value ? : T ; constructor ( value ? ) { this . value = value ; } public isEmpty ( ) { return this . value === undefined ; } public nonEmpty ( ) { return this . value !== undefined ; } public getOrElse ( defaultValue ) { return this . value || defaultValue ; } public map < U > ( mapper ) { return new Optional ( this . value ? mapper ( this . value ) : undefined ) ; } public flatMap < U > ( mapper ) { return this . value ? mapper ( this . value ) : new Optional ( ) ; } } export function Some < T > ( value ) { return new Optional ( value ) ; } export function None < T > ( ) { return new Optional ( ) ; } export class OptionalMap < K , V > extends Map < K , V > { public getOpt ( key ) { return new Optional ( this . get ( key ) ) ; } }