neverthrow
Advanced tools
Comparing version 4.0.1 to 4.1.0-beta.0
@@ -25,3 +25,3 @@ interface ErrorConfig { | ||
mapErr<U>(_f: (e: E) => U): Result<T, U>; | ||
andThen<U>(f: (t: T) => Result<U, E>): Result<U, E>; | ||
andThen<U, F>(f: (t: T) => Result<U, F>): Result<U, E | F>; | ||
/** | ||
@@ -31,3 +31,3 @@ * 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>; | ||
asyncAndThen<U>(f: (t: T) => ResultAsync<U, E>): ResultAsync<U, E>; | ||
asyncAndThen<U, F>(f: (t: T) => ResultAsync<U, F>): ResultAsync<U, E | F>; | ||
asyncMap<U>(f: (t: T) => Promise<U>): ResultAsync<U, E>; | ||
@@ -46,3 +46,3 @@ unwrapOr(_v: T): T; | ||
mapErr<U>(f: (e: E) => U): Result<T, U>; | ||
andThen<U>(_f: (t: T) => Result<U, E>): Result<U, E>; | ||
andThen<U, F>(_f: (t: T) => Result<U, F>): Result<U, E | F>; | ||
/** | ||
@@ -52,3 +52,3 @@ * 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>; | ||
asyncAndThen<U>(_f: (t: T) => ResultAsync<U, E>): ResultAsync<U, E>; | ||
asyncAndThen<U, F>(_f: (t: T) => ResultAsync<U, F>): ResultAsync<U, E | F>; | ||
asyncMap<U>(_f: (t: T) => Promise<U>): ResultAsync<U, E>; | ||
@@ -68,3 +68,3 @@ unwrapOr(v: T): T; | ||
mapErr<U>(f: (e: E) => U | Promise<U>): ResultAsync<T, U>; | ||
andThen<U>(f: (t: T) => Result<U, E> | ResultAsync<U, E>): ResultAsync<U, E>; | ||
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F>; | ||
orElse<A>(f: (e: E) => Result<T, A> | ResultAsync<T, A>): ResultAsync<T, A>; | ||
@@ -71,0 +71,0 @@ match<A>(ok: (t: T) => A, _err: (e: E) => A): Promise<A>; |
{ | ||
"name": "neverthrow", | ||
"version": "4.0.1", | ||
"version": "4.1.0-beta.0", | ||
"description": "Stop throwing errors, and instead return Results!", | ||
@@ -9,3 +9,4 @@ "main": "dist/index.cjs.js", | ||
"scripts": { | ||
"test": "jest", | ||
"test": "jest && npm run test-types", | ||
"test-types": "tsc --noEmit ./tests/typecheck-tests.ts", | ||
"lint": "eslint ./src --ext .ts", | ||
@@ -12,0 +13,0 @@ "format": "prettier --write 'src/**/*.ts?(x)' && npm run lint -- --fix", |
@@ -284,7 +284,7 @@ # NeverThrow 🙅 | ||
The returned value will be a `Result`. | ||
The returned value will be a `Result`. As of `v4.1.0-beta`, you are able to return distinct error types (see signature below). Prior to `v4.1.0-beta`, the error type could not be distinct. | ||
This is useful for when you need to do a subsequent computation using the inner `T` value, but that computation might fail. | ||
`andThen` is really useful as a tool to flatten a `Result<Result<A, E2>, E1>` into a `Result<A, E2>` (see example below). | ||
Additionally, `andThen` is really useful as a tool to flatten a `Result<Result<A, E2>, E1>` into a `Result<A, E2>` (see example below). | ||
@@ -294,6 +294,8 @@ **Signature:** | ||
```typescript | ||
type AndThenFunc = <T, U>(t: T) => Result<U, E> | ||
andThen<U>(f: AndThenFunc): Result<U, E> { ... } | ||
class Result<T, E> { | ||
// Note that the latest version lets you return distinct errors as well. | ||
// If the error types (E and F) are the same (like `string | string`) | ||
// then they will be merged into one type (`string`) | ||
andThen<U, F>(fn: (val: T) => Result<U, F>): Result<U, E | F> { ... } | ||
} | ||
``` | ||
@@ -341,3 +343,3 @@ | ||
Same idea as `andThen` above. Except you must return a new `ResultAsync`. | ||
Same idea as [`andThen` above](#resultandthen-method), except you must return a new `ResultAsync`. | ||
@@ -349,6 +351,5 @@ The returned value will be a `ResultAsync`. | ||
```typescript | ||
type AndThenAsyncFunc = (t: T) => ResultAsync<U, E> | ||
asyncAndThen<U>(f: AndThenAsyncFunc): ResultAsync<U, E> { ... } | ||
class Result<T, E> { | ||
asyncAndThen<U, F>(fn: (val: T) => ResultAsync<U, F>): ResultAsync<U, E | F> { ... } | ||
} | ||
``` | ||
@@ -769,2 +770,9 @@ | ||
andThen<U>(f: AndThenFunc): ResultAsync<U, E> { ... } | ||
class ResultAsync<T, E> { | ||
// Note that the latest version (v4.1.0-beta) lets you return distinct errors as well. | ||
// If the error types (E and F) are the same (like `string | string`) | ||
// then they will be merged into one type (`string`) | ||
andThen<U, F>(f: (t: T) => Result<U, F> | ResultAsync<U, F>): ResultAsync<U, E | F> { ... } | ||
} | ||
``` | ||
@@ -771,0 +779,0 @@ |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
60822
954
1