export interface Option < T > { map < U > ( fn ) : Option < U > ; isSome ( ) : this is Some < T > ; isNone ( ) : this is None < T > ; unwrap ( ) : T ; tryUnwrap ( ) : T | undefined ; } export class Some < T > implements Option < T > { value : T ; map < U > ( fn ) { return new Some ( fn ( this . value ) ) ; } constructor ( value ) { this . value = value ; } isSome ( ) { return true ; } isNone ( ) { return false ; } unwrap ( ) { return this . value ; } tryUnwrap ( ) { return this . unwrap ( ) ; } toString ( ) { return `Some(${ this . value })` ; } } export function some < T > ( value ) { return new Some < T > ( value ) ; } export function none < T > ( ) { return new None < T > ( ) ; } export class None < T > implements Option < T > { map < U > ( fn ) { return new None < U > ( ) ; } isSome ( ) { return false ; } isNone ( ) { return true ; } unwrap ( ) { throw new Error ( 'None has no value' ) ; } tryUnwrap ( ) { return undefined ; } public toString ( ) { return 'None' ; } }