[**@cprussin/option-result v2.0.0**](../README.md)

***

# Class: Result\<T, E\>

Defined in: [result.ts:16](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L16)

A type which represents values that may encode either a successful result or
an error result.

## Type Parameters

### T

`T` *extends* `NonNullable`\<`unknown`\>

the type of successful results

### E

`E` *extends* `NonNullable`\<`unknown`\> = `Error`

the type of error results

## Boolean operators

### and()

> **and**\<`U`\>(`this`, `other`): `Result`\<`U`, `E`\>

Defined in: [result.ts:285](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L285)

Takes another Result and returns it if `this` is `Ok`, otherwise
returns `this`.

#### Type Parameters

##### U

`U` *extends* `object`

the type contained in the Result returned by `fn`

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### other

`Result`\<`U`, `E`\>

a Result to return if `this` is `Ok`

#### Returns

`Result`\<`U`, `E`\>

`other` if `this` is `Ok`, `this` otherwise

#### See

 - [andThen](#andthen)
 - [andThenAsync](#andthenasync)
 - [or](#or)
 - [orElse](#orelse)
 - [orElseAsync](#orelseasync)

***

### andThen()

> **andThen**\<`U`\>(`this`, `fn`): `Result`\<`U`, `E`\>

Defined in: [result.ts:261](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L261)

Takes a function which takes the value contained in `this` and returns a
new Result; calls it and returns the result if `this` is `Ok`,
otherwise returns `this`.  Also sometimes known in other languages or
libraries as `flatmap` or `bind`.

#### Type Parameters

##### U

`U` *extends* `object`

the type contained in the Result returned by `fn`

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### fn

(`value`) => `Result`\<`U`, `E`\>

a function that will be called with the value in `this` if
`this` is `Ok`

#### Returns

`Result`\<`U`, `E`\>

the result of calling `fn` if `this` is `Ok`, `this` otherwise

#### See

 - [and](#and)
 - [andThenAsync](#andthenasync)
 - [or](#or)
 - [orElse](#orelse)
 - [orElseAsync](#orelseasync)

***

### andThenAsync()

> **andThenAsync**\<`U`\>(`this`, `fn`): `Promise`\<`Result`\<`U`, `E`\>\>

Defined in: [result.ts:309](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L309)

Takes a function which takes the value contained in `this` and returns a
promise which resolves to a new Result; calls it and returns the
result if `this` is `Ok`, otherwise returns a promise which resolves to
`this`.  This is an async version of [andThen](#andthen).

#### Type Parameters

##### U

`U` *extends* `object`

the type contained in the Result returned by `fn`

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### fn

(`value`) => `Promise`\<`Result`\<`U`, `E`\>\>

a function that will be called with the value in `this` if
`this` is `Ok`

#### Returns

`Promise`\<`Result`\<`U`, `E`\>\>

the result of calling `fn` if `this` is `Ok`, `this` otherwise

#### See

 - [and](#and)
 - [andThen](#andthen)
 - [or](#or)
 - [orElse](#orelse)
 - [orElseAsync](#orelseasync)

***

### or()

> **or**\<`F`\>(`this`, `other`): `Result`\<`T`, `F`\>

Defined in: [result.ts:357](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L357)

Takes another Result and returns it if `this` is `Err`, otherwise
returns `this`.

#### Type Parameters

##### F

`F` *extends* `object` = `Error`

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### other

`Result`\<`T`, `F`\>

a Result to return if `this` is `Err`

#### Returns

`Result`\<`T`, `F`\>

`other` if `this` is `Err`, `this` otherwise

#### See

 - [and](#and)
 - [andThen](#andthen)
 - [andThenAsync](#andthenasync)
 - [orElse](#orelse)
 - [orElseAsync](#orelseasync)

***

### orElse()

> **orElse**\<`F`\>(`this`, `fn`): `Result`\<`T`, `F`\>

Defined in: [result.ts:334](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L334)

Takes a function which takes an error and returns a Result; calls
it on the contained error and returns the result if `this` is `Err`,
otherwise returns `this`.

#### Type Parameters

##### F

`F` *extends* `object` = `Error`

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### fn

(`error`) => `Result`\<`T`, `F`\>

a function that will be called with the error value if `this`
is `Err`

#### Returns

`Result`\<`T`, `F`\>

the result of calling `fn` if `this` is `Err`, `this` otherwise

#### See

 - [and](#and)
 - [andThen](#andthen)
 - [andThenAsync](#andthenasync)
 - [or](#or)
 - [orElseAsync](#orelseasync)

***

### orElseAsync()

> **orElseAsync**\<`F`\>(`this`, `fn`): `Promise`\<`Result`\<`T`, `F`\>\>

Defined in: [result.ts:379](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L379)

Takes a function which takes an error and returns a promise which resolves
to a new Result; calls it with the contained error and returns the
result if `this` is `Err`, otherwise returns a promise which resolves to
`this`.

#### Type Parameters

##### F

`F` *extends* `object` = `Error`

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### fn

(`error`) => `Promise`\<`Result`\<`T`, `F`\>\>

a function that will be called if `this` is `Err`

#### Returns

`Promise`\<`Result`\<`T`, `F`\>\>

the result of calling `fn` if `this` is `Err`, `this` otherwise

#### See

 - [and](#and)
 - [andThen](#andthen)
 - [andThenAsync](#andthenasync)
 - [or](#or)
 - [orElse](#orelse)

## Constructing

### Err()

> `static` **Err**\<`T`, `E`\>(`error`): `Result`\<`T`, `E`\>

Defined in: [result.ts:59](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L59)

Construct a `Result` containing an error value.

#### Type Parameters

##### T

`T` *extends* `object`

the type of success values

##### E

`E` *extends* `object` = `Error`

the type of the error value

#### Parameters

##### error

`E`

the error contained by the `Result`

#### Returns

`Result`\<`T`, `E`\>

a `Result` containing an error `error`

#### See

 - [Ok](../functions/Ok.md)
 - [wrap](#wrap)
 - [wrapAsync](#wrapasync)

***

### Ok()

> `static` **Ok**\<`T`, `E`\>(`value`): `Result`\<`T`, `E`\>

Defined in: [result.ts:40](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L40)

Construct a `Result` containing a success value.

#### Type Parameters

##### T

`T` *extends* `object`

the type of the value

##### E

`E` *extends* `object` = `Error`

the type of error results

#### Parameters

##### value

`T`

the value contained by the `Result`

#### Returns

`Result`\<`T`, `E`\>

a `Result` containing `value`

#### See

 - [Err](../functions/Err.md)
 - [wrap](#wrap)
 - [wrapAsync](#wrapasync)

***

### wrap()

> `static` **wrap**\<`T`\>(`fn`): `Result`\<`T`, [`Option`](Option.md)\<\{ \}\>\>

Defined in: [result.ts:116](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L116)

Takes a function that could be throw and converts it into an Result.

#### Type Parameters

##### T

`T` *extends* `object`

the type contained by the Result

#### Parameters

##### fn

() => `T`

the function which could throw

#### Returns

`Result`\<`T`, [`Option`](Option.md)\<\{ \}\>\>

`Ok` with the return value of `fn` if `fn` doesn't throw,
otherwise `Err` containing an [Option](Option.md) which is `None` if the
exception is `null` or `undefined`, and is `Some` with the exception
otherwise

#### See

 - [Ok](../functions/Ok.md)
 - [Err](../functions/Err.md)
 - [wrapAsync](#wrapasync)

***

### wrapAsync()

> `static` **wrapAsync**\<`T`\>(`promise`): `Promise`\<`Result`\<`T`, [`Option`](Option.md)\<\{ \}\>\>\>

Defined in: [result.ts:141](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L141)

Takes a promise that could reject and converts it into an Result.

#### Type Parameters

##### T

`T` *extends* `object`

the type contained by the Result

#### Parameters

##### promise

`Promise`\<`T`\>

the promise to convert

#### Returns

`Promise`\<`Result`\<`T`, [`Option`](Option.md)\<\{ \}\>\>\>

a Promise containing `Ok` with the value resolved by `promise` if
`promise` doesn't reject, otherwise `Err` containing an [Option](Option.md)
which is `None` if `promise` rejects with a `null` or `undefined`, and is
`Some` with the rejection value otherwise

#### See

 - [Ok](../functions/Ok.md)
 - [Err](../functions/Err.md)
 - [wrap](#wrap)

## Extracting the contained value

### match()

> **match**\<`U`\>(`this`, `matchers`): `U`

Defined in: [result.ts:184](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L184)

Takes two functions, one is called with the contained value if `this` is
`Ok` and the other is called with the error if `this` is `Err`.

#### Type Parameters

##### U

`U`

the type of the return value of the matcher functions

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### matchers

an object containing matcher functions

###### Err

(`error`) => `U`

A function that will be called on the value contained if `this` is
`Err`.

###### Ok

(`value`) => `U`

A function that will be called on the value contained if `this` is
`Ok`.

#### Returns

`U`

the return value of the `Ok` or `Err` matcher function

#### See

 - [unwrapOrElse](#unwraporelse)
 - [unwrapOr](#unwrapor)

***

### unwrapOr()

> **unwrapOr**(`this`, `defaultValue`): `T`

Defined in: [result.ts:240](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L240)

Returns the contained value if `this` is `Ok`, otherwise return the
provided default value, ignoring any error value.

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### defaultValue

`T`

the value to return if `this` is `Err`

#### Returns

`T`

the contained if `this` is `Ok`, otherwise `defaultValue`

#### See

 - [unwrapOrElse](#unwraporelse)
 - [match](#match)

***

### unwrapOrElse()

> **unwrapOrElse**(`this`, `defaultValue`): `T`

Defined in: [result.ts:223](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L223)

Returns the contained value if `this` is `Ok`, otherwise call the provided
function with the error value and return the result.

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### defaultValue

(`error`) => `T`

a function that will be called on the error value if
`this` is `Err`

#### Returns

`T`

the contained if `this` is `Ok`, otherwise the return value from
calling `defaultValue` on the error value

#### See

 - [unwrapOr](#unwrapor)
 - [match](#match)

## Interacting with Option

### err()

> **err**(`this`): [`Option`](Option.md)\<`E`\>

Defined in: [result.ts:538](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L538)

If `this` is `Err`, returns [Option.Some](Option.md#some) containing the value in
`this`.  Otherwise return [Option.None](Option.md#none).

#### Parameters

##### this

`Result`\<`T`, `E`\>

#### Returns

[`Option`](Option.md)\<`E`\>

[Option.Some](Option.md#some) with the value in `this` if `this` is `Err`,
[Option.None](Option.md#none) otherwise

#### See

 - [ok](#ok)
 - [transpose](#transpose)

***

### ok()

> **ok**(`this`): [`Option`](Option.md)\<`T`\>

Defined in: [result.ts:521](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L521)

If `this` is `Ok`, returns [Option.Some](Option.md#some) containing the value in
`this`.  Otherwise return [Option.None](Option.md#none).

#### Parameters

##### this

`Result`\<`T`, `E`\>

#### Returns

[`Option`](Option.md)\<`T`\>

[Option.Some](Option.md#some) with the value in `this` if `this` is `Ok`,
[Option.None](Option.md#none) otherwise

#### See

 - [err](#err)
 - [transpose](#transpose)

***

### transpose()

> **transpose**\<`T`\>(`this`): [`Option`](Option.md)\<`Result`\<`T`, `E`\>\>

Defined in: [result.ts:559](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L559)

Convert a Result containing an [Option](Option.md) into an [Option](Option.md) containing a Result.

#### Type Parameters

##### T

`T` *extends* `object`

the type of the value contained in the [Option](Option.md)
contained in `this`

#### Parameters

##### this

`Result`\<[`Option`](Option.md)\<`T`\>, `E`\>

#### Returns

[`Option`](Option.md)\<`Result`\<`T`, `E`\>\>

if `this` is `Err`, returns [Option.Some](Option.md#some) containing `this`;
if `this` is `Ok` and the [Option](Option.md) in `this` is [Option.None](Option.md#none),
returns [Option.None](Option.md#none); otherwise returns [Option.Some](Option.md#some)
containing `Ok` containing the value in the [Option](Option.md) in `this`

#### See

 - [ok](#ok)
 - [err](#err)

## Querying the variant

### isErr()

> **isErr**(`this`): `boolean`

Defined in: [result.ts:169](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L169)

Check if `this` is `Err`.

#### Parameters

##### this

`Result`\<`T`, `E`\>

#### Returns

`boolean`

true if `this` is `Err`, `false` otherwise

#### See

[isOk](#isok)

***

### isOk()

> **isOk**(`this`): `boolean`

Defined in: [result.ts:158](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L158)

Check if `this` is `Ok`.

#### Parameters

##### this

`Result`\<`T`, `E`\>

#### Returns

`boolean`

true if `this` is `Ok`, `false` otherwise

#### See

[isErr](#iserr)

## Transforming contained values

### map()

> **map**\<`U`\>(`this`, `fn`): `Result`\<`U`, `E`\>

Defined in: [result.ts:404](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L404)

Transforms `Result<T, E>` to `Result<U, E>` by applying the provided
function to the contained value of `Ok` and leaving `Err` values unchanged.

#### Type Parameters

##### U

`U` *extends* `object`

the type of the return value of `fn`

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### fn

(`value`) => `U`

the function to apply to the value contained if `this` is `Ok`

#### Returns

`Result`\<`U`, `E`\>

`this` if `this` is `Err`, otherwise `Ok` containing the result of
applying `fn` to the value in `this`

#### See

 - [mapAsync](#mapasync)
 - [mapOr](#mapor)
 - [mapErr](#maperr)
 - [mapErrAsync](#maperrasync)
 - [collect](#collect)

***

### mapAsync()

> **mapAsync**\<`U`\>(`this`, `fn`): `Promise`\<`Result`\<`U`, `E`\>\>

Defined in: [result.ts:428](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L428)

Transforms `Result<T, E>` to `Promise<Result<U, E>>` by applying the
provided async function to the contained value of `Ok` and resolving `Err`
values unchanged.

#### Type Parameters

##### U

`U` *extends* `object`

the type of the value in the promise returned by `fn`

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### fn

(`value`) => `Promise`\<`U`\>

the function to apply to the value contained if `this` is `Ok`

#### Returns

`Promise`\<`Result`\<`U`, `E`\>\>

a promise resolving to `this` if `this` is `Err`, otherwise a
promise resolving to `Ok` containing the value resolved by the promise
returned from applying `fn` to the value in `this`

#### See

 - [map](#map)
 - [mapOr](#mapor)
 - [mapErr](#maperr)
 - [mapErrAsync](#maperrasync)
 - [collect](#collect)

***

### mapErr()

> **mapErr**\<`F`\>(`this`, `fn`): `Result`\<`T`, `F`\>

Defined in: [result.ts:474](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L474)

Transforms `Result<T, E>` to `Result<T, F>` by applying the provided
function to the contained value of `Err` and leaving `Ok` values unchanged.

#### Type Parameters

##### F

`F` *extends* `object` = `Error`

the type of the return value of `fn`

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### fn

(`error`) => `F`

the function to apply to the value contained if `this` is `Err`

#### Returns

`Result`\<`T`, `F`\>

`this` if `this` is `Ok`, otherwise `Err` containing the result of
applying `fn` to the value in `this`

#### See

 - [map](#map)
 - [mapAsync](#mapasync)
 - [mapOr](#mapor)
 - [mapErrAsync](#maperrasync)
 - [collect](#collect)

***

### mapErrAsync()

> **mapErrAsync**\<`F`\>(`this`, `fn`): `Promise`\<`Result`\<`T`, `F`\>\>

Defined in: [result.ts:501](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L501)

Transforms `Result<T, E>` to `Promise<Result<T, F>>` by applying the
provided async function to the contained value of `Err` and resolving `Ok`
values unchanged.

#### Type Parameters

##### F

`F` *extends* `object` = `Error`

the type of the value in the promise returned by `fn`

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### fn

(`error`) => `Promise`\<`F`\>

the function to apply to the value contained if `this` is `Err`

#### Returns

`Promise`\<`Result`\<`T`, `F`\>\>

a promise resolving to `this` if `this` is `Ok`, otherwise a
promise resolving to `Err` containing the value resolved by the promise
returned from applying `fn` to the value in `this`

#### See

 - [map](#map)
 - [mapAsync](#mapasync)
 - [mapOr](#mapor)
 - [mapErr](#maperr)
 - [collect](#collect)

***

### mapOr()

> **mapOr**\<`U`\>(`this`, `defaultValue`, `fn`): `U`

Defined in: [result.ts:451](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L451)

Applies the provided function to the contained value if `this` is `Ok`,
otherwise returns the provided default value.

#### Type Parameters

##### U

`U` *extends* `object`

the type of `defaultValue` and the value returned by `fn`

#### Parameters

##### this

`Result`\<`T`, `E`\>

##### defaultValue

`U`

the value to return if `this` is `Err`

##### fn

(`value`) => `U`

the function to apply to the value contained if `this` is `Ok`

#### Returns

`U`

the result of applying `fn` to the value in `this` if `this` is
`Ok`, otherwise `defaultValue`

#### See

 - [map](#map)
 - [mapAsync](#mapasync)
 - [mapErr](#maperr)
 - [mapErrAsync](#maperrasync)
 - [collect](#collect)

***

### collect()

> `static` **collect**\<`T`, `E`\>(`results`): `Result`\<`T`[], `E`\>

Defined in: [result.ts:85](https://github.com/cprussin/cprussin-monorepo/blob/c4cae9afc0f1d2361327617312f36819ab993c71/packages/option-result/src/result.ts#L85)

Takes an Array of Result values.  If any of the Result
values in the array is `Err`, returns that `Err`.  Otherwise, returns `Ok`
containing an Array of each value inside each Result in the
original list.

#### Type Parameters

##### T

`T` *extends* `object`

the type contained by the Result values

##### E

`E` *extends* `object` = `Error`

the error type of the Result values

#### Parameters

##### results

`Result`\<`T`, `E`\>[]

the Result values to collect

#### Returns

`Result`\<`T`[], `E`\>

`Ok` containing an Array of values contained by each Result in the original Array if all Result values in the original
list are `Ok`, otherwise returns the first `Err` in the Array

#### See

 - [map](#map)
 - [mapAsync](#mapasync)
 - [mapOr](#mapor)
 - [mapErr](#maperr)
 - [mapErrAsync](#maperrasync)
