Type-Safe Errors for JS & TypeScript
Many thanks to @sidhu663 for these great improvements to neverthrow
.
andThen
and orElse
MethodsWith this release, you should not need to explicitly annotate the return type on the callbacks called into andThen
and orElse
.
Example:
const slotsResult = ok(123).andThen((value) =>
value === '777'
? ok('Lucky Sevens')
: err('Try Again')
)
Before, slotsResult
would be of type Result<unknown, unknown>
.
The "solution" was to explicitly annotate your callback function as returning Result<string, string>
This exact piece of code above now types slotsResult
as Result<string, string>
!
If you're interested in how this works, check out the PR that implements these improvements here: https://github.com/supermacro/neverthrow/pull/349
.orElse
Result
and ResultAsync
unwrapOr
MethodThe type signature of .unwrapOr
has been "widened" to allow for returning something other than a type T
as defined by the Result
/ ResultAsync
you are calling unwrapOr
on.
This change was implemented in https://github.com/supermacro/neverthrow/pull/350.
Example:
const result = err<number, string>('boom').unwrapOr(false)
The above did not work prior to v4.3.0
because boolean
(the false
value) is not of type number
.
But now you are not constrained by this! As of v4.3.0
you now have the ability to specify a value of any type within unwrapOr
.
In the above example, result is now of type number | boolean
.
Fixes #300
This PR updates the type definition of Result.fromThrowable
in order to allow accepting functions that take 1 - n arguments.
Thanks to @universalhandle for the contribution 👏
This release adds in-line docs for the Result
variants. Now, when using a LSP-supported editor, you'll have docs as you hover a Result
method.
Thanks to @Liam-Tait for the contribution!
neverthrow
now exposes a combineWithAllErrors
function that is the "lazy" (for lack of a better word) equivalent of combine
.
This means that rather than short-circuiting upon finding the first Err
value, combineWithAllErrors
will continue consuming the Result
/ ResultAsync
list until the end and return a list containing all the Err
values that it found.
Thanks to @tobloef for the contribution 🚀
This release patches a bug where calling combine
on a Result<T, E>[]
or ResultAsync<T, E>[]
and the T
values were lists, then those lists would get concatenated into a single output list:
Example:
import { combine, ok } from 'neverthrow'
combine([
ok([ 1, 2, 3 ]),
ok([ 'hello', 'world ])
])
// before v4.1.1, the above would incorrectly return [ 1, 2, 3, 'hello', 'world' ]
//
// actual output should be [ [ 1,2,3 ], [ 'hello', 'world' ] ]
The fix of the bug can be found here:
https://github.com/supermacro/neverthrow/commit/a5e66a88f8257f3afb74787e65bc04a1bf1a3812
I decided to continue using Array.prototype.concat
in spite of this subtlety because it does not mutate references (unlike Array.prototype.push
).
This release:
Releases https://github.com/supermacro/neverthrow/releases/tag/v4.1.0-beta.0 out of beta
@default
npm tag, meaning you can just run npm install neverthrow
to get this functionalityRe-exporting fromThrowable
, fromPromise
and fromSafePromise
(PR here)
I finally caved and have gone ahead and "loosened" the restriction that all andThen
variations (Result.andThen
, Result.asyncAndThen
, and ResultAsync.andThen
) had before. The restriction prevented the callback inside all andThen
variations from returning a distinct / different error type.
See https://github.com/supermacro/neverthrow/issues/30 for more context on the "problem" that this addresses.
This release is downloadable behind a beta
tag on npm.
> npm install neverthrow@beta
Docs have been updated to reflect this change.
Here's an example diff.
- andThen<U>(f: (t: T) => Result<U, E>): Result<U, E> {
+ andThen<U, F>(f: (t: T) => Result<U, F>): Result<U, E | F> {
And here is the full diff:
https://github.com/supermacro/neverthrow/commit/8f87ef050279cf17e81b4f747abace84540472f5
The combine
overload for ResultAsync
lists was incorrect. Previously the overload stated that the return type of a combine
invocation with a list of ResultAsyncs
was a Result
.
This patch fixes this error and now combine
's type on ResultAsync
s correctly specifies that the return value will be a ResultAsync
.
Breaking Change: This release changes the API of the original ResultAsync.fromPromise
method to require a error handler callback.
In addition, there is now a new static class method called ResultAsync.fromSafePromise
that does not take a error handler callback under the assumption that you are calling this method with a promise that does not ever throw.
The combine
utility function now lets you combine lists containing different kinds of results! The recommended way that you use combine when calling it with a heterogeneous list is to either declare a tuple type that describes each element in the list, or declare the list as const
.
orElse
methodThe orElse
method (implemented on both Result
and ResultAsync
) allows for error-recovery where you want to potentially convert an E
into T
.