param T export type Result < T > = Success < T > | Failure < T > ; export enum ResultStatus { Fulfilled = 'fulfilled' , Rejected = 'rejected' } param value returns export function result < T > ( value ) ; export function result < T > ( value ) ; export function result ( ) ; export function result < T > ( value ? ) { return value instanceof Error ? new Failure < T > ( value ) : new Success < T > ( value as T ) ; } param promise returns export async function asyncResult < T > ( promise ) { try { const value = await promise ; return new Success ( value ) ; } catch ( error ) { return error instanceof Error ? new Failure ( error ) : new Failure ( new Error ( String ( error ) ) ) ; } } param resultPromise returns export async function enforceAsyncResult < T > ( resultPromise ) { const result = await resultPromise ; return result . enforceValue ( ) ; } abstract class Outcome < T > { /**
     * Whether this result represents a success or a failure. Successes always have a result value and
     * never have a failure reason, while failures always have a failure reason and never have a
     * result value.
     *
     * This property is provided for convenience instead of checking the `status` property.
     */ abstract readonly ok : boolean ; /**
     * The status of this result: either "fulfilled" or "rejected". This property and its possible
     * values are the same as the `status` field of standard promise results.
     */ abstract readonly status : ResultStatus ; /**
     * The value of this result, if it represents a success. This property is always `undefined` if
     * this result represents a failure.
     */ abstract readonly value : T | undefined ; /**
     * The reason the operation that created this result failed, if this result represents a failure.
     * This property is always `undefined` if this result represents a success.
     */ abstract readonly reason : Error | undefined ; /**
     * Returns the value of this result if it represents a success, or throws the underlying error if
     * this result represents a failure.
     */ enforceValue ( ) { if ( ! this . ok ) { throw this . reason ; } return this . value ! ; } /**
     * Returns the error that caused this failure, or throws a TypeError if this result actually is a
     * success.
     */ enforceError ( ) { if ( this . ok ) { throw new TypeError ( `Expected result to have a failure reason but actually was a success` ) ; } return this . reason ! ; } returns toJSON ( ) { return this . ok ? { status : ResultStatus . Fulfilled , value : this . value } : { status : ResultStatus . Rejected , reason : this . reason } ; } } export class Success < T > extends Outcome < T > { readonly status = ResultStatus . Fulfilled ; readonly ok = true ; readonly value : T ; readonly reason : undefined ; [ Symbol . toStringTag ] = 'Success' ; constructor ( value ) { super ( ) ; this . value = value ; } } export class Failure < T > extends Outcome < T > { readonly status = ResultStatus . Rejected ; readonly ok = false ; readonly value : undefined ; readonly reason : Error ; [ Symbol . toStringTag ] = 'Failure' ; constructor ( reason ) { super ( ) ; this . reason = reason ; } }