README.md
Package detail
ts-results-es
A TypeScript implementation of Rust's Result and Option objects.
readme
changelog
5.0.1
Fixed:
- Fixed the regression introduced in 5.0.0 as part of the
AsyncResult.andThen
fix. The fix is completely reverted for now.
5.0.0
Backwards incompatible:
Changed
Option
andResult
iterator behavior such that iteratingSome
andOk
will instead produce only one result – the wrapped value. Previously the iteration depended on the type of the wrapped value (iteratable or not) and produced results obtained by iterating the wrapped values.For example:
const o: Option<number[]> = Some([1, 2, 3]) const rs = Array.from(o) // Previously: rs was [1, 2, 3] // Now: rs equals [[1, 2, 3]]
Iterating
None
andErr
is not affected and continues to produce no results.- Removed the parameter spread variants of
Result.all
andResult.any
. Both of these methods now only take a single array parameter (the array parameter has already been supported for a while).
Fixed:
- Fixed
Result.or
andResult.orElse
method types to actually be callable and return reasonable types when called. - Attempted to fix
AsyncResult.andThen
to return the correct type when the provided callback always returns anOk
. This attempt has been (for now) reverted in 5.0.1 as it created other problems. - Fixed the
Result.partition
signature.
Added:
Option.unwrapOrElse
Result.unwrapOrElse
4.2.0
Added:
- Added a non-spread (you can pass an array as a single parameter) variant of
Result.all
- Added a new
Result.partition
convenience method
4.1.0
- A whole bunch of documentation changes
- Introduced
AsyncResult
to allow composing results with asynchronous code - Introduced
AsyncOption
as well - Fixed
Option.any
behavior - Fixed an edge case in using
ts-results-es
in CommonJS projects
4.0.0
- Improved the documentation
- Fixed the rxjs-operators submodules type declarations for CommonJS code
- Changed
Result.orElse()
andResult.mapOrElse()
error-handling callback to take the error as an argument (consistent with the original Rust methods)
Backwards incompatible:
- A bunch of renames:
Some.val
->Some.value
Result.val
->Ok.value
andErr.error
Option.some
->Option.isSome()
Option.none
->Option.isNone()
Result.ok
->Result.isOk()
Result.err
->Result.isErr()
3.6.1
- Improved the documentation a little bit
- Fixed rxjs-operators module imports, thanks to Jacob Nguyen
3.6.0
- Added
or()
andorElse()
methods to bothOption
andResult
3.5.0
- Added
andThen()
documentation, thanks to Drew De Ponte - Added the
expectErr()
method toResult
, thanks to TheDudeFromCI - Added
mapOr()
andmapOrElse()
to bothOption
andResult
3.4.0
- Fixed some type errors that prevented the package from being built with recent TypeScript versions
- Fixed ESM compatibility so that client code can use named imports without resorting to workarounds (fixes https://github.com/vultix/ts-results/issues/37)
3.3.0
Big thank you to @petehunt for all his work adding stack traces to Err
.
- Added a
stack
property to allErr
objects. Can be used to pull a stack trace - Added
toOption
andtoResult
methods for converting betweenOption
andResult
objects
v3.2.1
- Fix regression found in Issue#24
v3.2.0
- Fixes for Typescript 4.2
v3.1.0
Big thank you to @petehunt for all his work adding the Option
type.
New Features
Added new
Option<T>
,Some<T>
, andNone
types!You should feel at home if you're used to working with Rust:
import { Option, Some, None } from 'ts-results'; const optionalNum: Option<number> = Some(3).map((num) => num * 2); if (optionalNum.some) { console.log(optionalNum.val === 6); // prints `true` } const noneNum: Option<number> = None; if (noneNum.some) { // You'll never get in here }
Added new
Option.isOption
andResult.isResult
helper functions.
Other Improvements
- Got to 100% test coverage on all code!
- Removed uses of
@ts-ignore
v3.0.0
Huge shout out to @Jack-Works for helping get this release out. Most of the work was his, and it would not have happened without him.
New Features
Ok<T>
andErr<T>
are now callable withoutnew
!- No longer breaks when calling from node
- Tree-shakable when using tools like rollup or webpack
- Fully unit tested
- Added these helper functions:
Result.all(...)
- Same asResults
from previous releases. Collects allOk
values, or returns the firstErr
value.Results.any(...)
- Returns the firstOk
value, or all of theErr
values.Result.wrap<T, E>(() => ...)
- Wraps an operation that may throw an error, uses try / catch to return aResult<T, E>
Result.wrapAsync<T, E>(() => ...)
- Same as the above, but async
- Deprecated
else
in favor ofunwrapOr
to prefer api parity with Rust
v2.0.1
New Features
- core: Added
reaonly static EMPTY: Ok<void>;
toOk
class. - core: Added
reaonly static EMPTY: Err<void>;
toErr
class.
v2.0.0
This release features a complete rewrite of most of the library with one focus in mind: simpler types.
The entire library now consists of only the following:
- Two classes:
Ok<T>
andErr<E>
. - A
Result<T, E>
type that is a simple or type between the two classes. - A simple
Results
function that allows combining multiple results.
New Features
- core: much simpler Typescript types
- rxjs: added new
filterResultOk
andfilterResultErr
operators - rxjs: added new
resultMapErrTo
operator
Breaking Changes
- core:
Err
andOk
now requirenew
:- Before:
let result = Ok(value); let error = Err(message);
- After:
let result = new Ok(value); let error = new Err(message);
- Before:
- core:
map
function broken into two functions:map
andmapErr
- before:
result.map(value => "new value", error => "new error")
- after:
result.map(value => "newValue").mapError(error => "newError")
- before:
- rxjs:
resultMap
operator broken into two operators:resultMap
andresultMapErr
- before:
obs.pipe(resultMap(value => "new value", error => "new error"))
- after:
result.pipe(resultMap(value => "newValue"), resultMapError(error => "newError"))
- before: