Type Inference Improvements on `andThen` + `orElse`, and more flexible `unwrapOr` method
Many thanks to @sidhu663 for these great improvements to neverthrow.
Significantly Improved Type Inference On andThen and orElse Methods
With this release, you should not need to explicitly annotate the return type on the callbacks called into andThen and orElse.
Example:
Before
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>
After
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: #349
Additional notes
- This same improvement applies to
.orElse - These improvements are for both
ResultandResultAsync
More Flexible unwrapOr Method
The 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 #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.