Skip to content

Latest commit

 

History

History
326 lines (204 loc) · 6.53 KB

File metadata and controls

326 lines (204 loc) · 6.53 KB

@mojotech/json-type-validation > "result"

External module: "result"

Index

Interfaces

Type aliases

Functions


Type aliases

Result

ΤResult: * Ok<V> | Err<E> *

The result of a computation that may fail. The decoding function Decoder.run returns a Result. The value of a Result is either Ok if the computation succeeded, or Err if there was some failure in the process.


Functions

<Const> andThen

andThenA,B,E(f: function, r: Result<A, E>): Result<B, E>

Chain together a sequence of computations that may fail, similar to a Promise. If the first computation fails then the error will propagate through. If it succeeds, then f will be applied to the value, returning a new Result.

Type parameters:

A

B

E

Parameters:

Param Type
f function
r Result<A, E>

Returns: Result<B, E>


<Const> asPromise

asPromiseV(r: Result<V, any>): Promise<V>

Create a Promise that either resolves with the result of Ok or rejects with the error of Err.

Type parameters:

V

Parameters:

Param Type
r Result<V, any>

Returns: Promise<V>


<Const> err

errE(error: E): Err<E>

Wraps errors in an Err type.

Example: err('on fire') // => {ok: false, error: 'on fire'}

Type parameters:

E

Parameters:

Param Type
error E

Returns: Err<E>


<Const> isErr

isErrE(r: Result<any, E>): boolean

Typeguard for Err.

Type parameters:

E

Parameters:

Param Type
r Result<any, E>

Returns: boolean


<Const> isOk

isOkV(r: Result<V, any>): boolean

Typeguard for Ok.

Type parameters:

V

Parameters:

Param Type
r Result<V, any>

Returns: boolean


<Const> map

mapA,B,E(f: function, r: Result<A, E>): Result<B, E>

Apply f to the result of an Ok, or pass the error through.

Type parameters:

A

B

E

Parameters:

Param Type
f function
r Result<A, E>

Returns: Result<B, E>


<Const> map2

map2A,B,C,E(f: function, ar: Result<A, E>, br: Result<B, E>): Result<C, E>

Apply f to the result of two Oks, or pass an error through. If both Results are errors then the first one is returned.

Type parameters:

A

B

C

E

Parameters:

Param Type
f function
ar Result<A, E>
br Result<B, E>

Returns: Result<C, E>


<Const> mapError

mapErrorV,A,B(f: function, r: Result<V, A>): Result<V, B>

Apply f to the error of an Err, or pass the success through.

Type parameters:

V

A

B

Parameters:

Param Type
f function
r Result<V, A>

Returns: Result<V, B>


<Const> ok

okV(result: V): Ok<V>

Wraps values in an Ok type.

Example: ok(5) // => {ok: true, result: 5}

Type parameters:

V

Parameters:

Param Type
result V

Returns: Ok<V>


<Const> successes

successesA(results: Result<A, any>[]): A[]

Given an array of Results, return the successful values.

Type parameters:

A

Parameters:

Param Type
results Result<A, any>[]

Returns: A[]


<Const> withDefault

withDefaultV(defaultValue: V, r: Result<V, any>): V

Unwraps a Result and returns either the result of an Ok, or defaultValue.

Example:

Result.withDefault(5, number().run(json))

It would be nice if Decoder had an instance method that mirrored this function. Such a method would look something like this:

class Decoder<A> {
  runWithDefault = (defaultValue: A, json: any): A =>
    Result.withDefault(defaultValue, this.run(json));
}

number().runWithDefault(5, json)

Unfortunately, the type of defaultValue: A on the method causes issues with type inference on the object decoder in some situations. While these inference issues can be solved by providing the optional type argument for objects, the extra trouble and confusion doesn't seem worth it.

Type parameters:

V

Parameters:

Param Type
defaultValue V
r Result<V, any>

Returns: V


<Const> withException

withExceptionV(r: Result<V, any>): V

Return the successful result, or throw an error.

Type parameters:

V

Parameters:

Param Type
r Result<V, any>

Returns: V