neverthrow
Advanced tools
Comparing version 4.2.0 to 4.2.1
@@ -144,11 +144,5 @@ 'use strict'; | ||
}; | ||
// add info on how this is really useful for converting a | ||
// Result<Result<T, E2>, E1> | ||
// into a Result<T, E2> | ||
Ok.prototype.andThen = function (f) { | ||
return f(this.value); | ||
}; | ||
/** | ||
* Applies a function to an `Err` value, leaving `Ok` values untouched. Useful for error recovery. | ||
*/ | ||
Ok.prototype.orElse = function (_f) { | ||
@@ -200,5 +194,2 @@ return ok(this.value); | ||
}; | ||
/** | ||
* Applies a function to an `Err` value, leaving `Ok` values untouched. Useful for error recovery. | ||
*/ | ||
Err.prototype.orElse = function (f) { | ||
@@ -205,0 +196,0 @@ return f(this.error); |
@@ -18,3 +18,113 @@ interface ErrorConfig { | ||
declare const err: <T, E>(err: E) => Err<T, E>; | ||
declare class Ok<T, E> { | ||
interface IResult<T, E> { | ||
/** | ||
* Used to check if a `Result` is an `OK` | ||
* | ||
* @returns `true` if the result is an `OK` variant of Result | ||
*/ | ||
isOk(): this is Ok<T, E>; | ||
/** | ||
* Used to check if a `Result` is an `Err` | ||
* | ||
* @returns `true` if the result is an `Err` variant of Result | ||
*/ | ||
isErr(): this is Err<T, E>; | ||
/** | ||
* Maps a `Result<T, E>` to `Result<U, E>` | ||
* by applying a function to a contained `Ok` value, leaving an `Err` value | ||
* untouched. | ||
* | ||
* @param f The function to apply an `OK` value | ||
* @returns the result of applying `f` or an `Err` untouched | ||
*/ | ||
map<A>(f: (t: T) => A): Result<A, E>; | ||
/** | ||
* Maps a `Result<T, E>` to `Result<T, F>` by applying a function to a | ||
* contained `Err` value, leaving an `Ok` value untouched. | ||
* | ||
* This function can be used to pass through a successful result while | ||
* handling an error. | ||
* | ||
* @param f a function to apply to the error `Err` value | ||
*/ | ||
mapErr<U>(f: (e: E) => U): Result<T, U>; | ||
/** | ||
* Similar to `map` Except you must return a new `Result`. | ||
* | ||
* This is useful for when you need to do a subsequent computation using the | ||
* inner `T` value, but that computation might fail. | ||
* Additionally, `andThen` is really useful as a tool to flatten a | ||
* `Result<Result<A, E2>, E1>` into a `Result<A, E2>` (see example below). | ||
* | ||
* @param f The function to apply to the current value | ||
*/ | ||
andThen<U, F>(f: (t: T) => Result<U, F>): Result<U, E | F>; | ||
/** | ||
* Takes an `Err` value and maps it to a `Result<T, SomeNewType>`. | ||
* | ||
* This is useful for error recovery. | ||
* | ||
* | ||
* @param f A function to apply to an `Err` value, leaving `Ok` values | ||
* untouched. | ||
*/ | ||
orElse<A>(f: (e: E) => Result<T, A>): Result<T, A>; | ||
/** | ||
* Similar to `map` Except you must return a new `Result`. | ||
* | ||
* This is useful for when you need to do a subsequent async computation using | ||
* the inner `T` value, but that computation might fail. Must return a ResultAsync | ||
* | ||
* @param f The function that returns a `ResultAsync` to apply to the current | ||
* value | ||
*/ | ||
asyncAndThen<U, F>(f: (t: T) => ResultAsync<U, F>): ResultAsync<U, E | F>; | ||
/** | ||
* Maps a `Result<T, E>` to `ResultAsync<U, E>` | ||
* by applying an async function to a contained `Ok` value, leaving an `Err` | ||
* value untouched. | ||
* | ||
* @param f An async function to apply an `OK` value | ||
*/ | ||
asyncMap<U>(f: (t: T) => Promise<U>): ResultAsync<U, E>; | ||
/** | ||
* Unwrap the `Ok` value, or return the default if there is an `Err` | ||
* | ||
* @param v the default value to return if there is an `Err` | ||
*/ | ||
unwrapOr(v: T): T; | ||
/** | ||
* | ||
* Given 2 functions (one for the `Ok` variant and one for the `Err` variant) | ||
* execute the function that matches the `Result` variant. | ||
* | ||
* Match callbacks do not necessitate to return a `Result`, however you can | ||
* return a `Result` if you want to. | ||
* | ||
* `match` is like chaining `map` and `mapErr`, with the distinction that | ||
* with `match` both functions must have the same return type. | ||
* | ||
* @param ok | ||
* @param err | ||
*/ | ||
match<A>(ok: (t: T) => A, err: (e: E) => A): A; | ||
/** | ||
* **This method is unsafe, and should only be used in a test environments** | ||
* | ||
* Takes a `Result<T, E>` and returns a `T` when the result is an `Ok`, otherwise it throws a custom object. | ||
* | ||
* @param config | ||
*/ | ||
_unsafeUnwrap(config?: ErrorConfig): T; | ||
/** | ||
* **This method is unsafe, and should only be used in a test environments** | ||
* | ||
* takes a `Result<T, E>` and returns a `E` when the result is an `Err`, | ||
* otherwise it throws a custom object. | ||
* | ||
* @param config | ||
*/ | ||
_unsafeUnwrapErr(config?: ErrorConfig): E; | ||
} | ||
declare class Ok<T, E> implements IResult<T, E> { | ||
readonly value: T; | ||
@@ -27,5 +137,2 @@ constructor(value: T); | ||
andThen<U, F>(f: (t: T) => Result<U, F>): Result<U, E | F>; | ||
/** | ||
* Applies a function to an `Err` value, leaving `Ok` values untouched. Useful for error recovery. | ||
*/ | ||
orElse<A>(_f: (e: E) => Result<T, A>): Result<T, A>; | ||
@@ -39,3 +146,3 @@ asyncAndThen<U, F>(f: (t: T) => ResultAsync<U, F>): ResultAsync<U, E | F>; | ||
} | ||
declare class Err<T, E> { | ||
declare class Err<T, E> implements IResult<T, E> { | ||
readonly error: E; | ||
@@ -48,5 +155,2 @@ constructor(error: E); | ||
andThen<U, F>(_f: (t: T) => Result<U, F>): Result<U, E | F>; | ||
/** | ||
* Applies a function to an `Err` value, leaving `Ok` values untouched. Useful for error recovery. | ||
*/ | ||
orElse<A>(f: (e: E) => Result<T, A>): Result<T, A>; | ||
@@ -53,0 +157,0 @@ asyncAndThen<U, F>(_f: (t: T) => ResultAsync<U, F>): ResultAsync<U, E | F>; |
@@ -142,11 +142,5 @@ /*! ***************************************************************************** | ||
}; | ||
// add info on how this is really useful for converting a | ||
// Result<Result<T, E2>, E1> | ||
// into a Result<T, E2> | ||
Ok.prototype.andThen = function (f) { | ||
return f(this.value); | ||
}; | ||
/** | ||
* Applies a function to an `Err` value, leaving `Ok` values untouched. Useful for error recovery. | ||
*/ | ||
Ok.prototype.orElse = function (_f) { | ||
@@ -198,5 +192,2 @@ return ok(this.value); | ||
}; | ||
/** | ||
* Applies a function to an `Err` value, leaving `Ok` values untouched. Useful for error recovery. | ||
*/ | ||
Err.prototype.orElse = function (f) { | ||
@@ -203,0 +194,0 @@ return f(this.error); |
{ | ||
"name": "neverthrow", | ||
"version": "4.2.0", | ||
"version": "4.2.1", | ||
"description": "Stop throwing errors, and instead return Results!", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.cjs.js", |
71072
930