export interface Ok < T > { tag : 'ok' ; value : T ; } export interface Err < E > { tag : 'err' ; error : E ; } export type Result < T , E > = Ok < T > | Err < E > ; export type Resultable < T , E > = { [ P in keyof T ] : Result < T [ P ] , E > ; } ; export function asOk < T > ( value ) { return { tag : 'ok' , value , } ; } export function asErr < T > ( error ) { return { tag : 'err' , error , } ; } export function isOk < T , E > ( result ) { return result . tag === 'ok' ; } export function isErr < T , E > ( result ) { return ! isOk ( result ) ; } export async function promiseResult < T , E > ( future ) { try { return asOk ( await future ) ; } catch ( e ) { return asErr ( e ) ; } } export function map < T , E , Resolution > ( result , onOk , onErr ) { return isOk ( result ) ? onOk ( result . value ) : onErr ( result . error ) ; } export function resolveErr < T , E > ( result , onErr ) { return isOk ( result ) ? result . value : onErr ( result . error ) ; }