README.md
パッケージの詳細
ts-results-es
A TypeScript implementation of Rust's Result and Option objects.
readme
更新履歴
6.0.0
Backwards incompatible:
Fixed
Result'sandThenandorElsesignatures to fix situations where they couldn't be called (This expression is not callable). That comes at the cost of:- Reduced type narrowing when the input type is known to be
OkorErr. - Reduced type narrowing when the mapper function always returns
Okor always returnsErr. Possible workaround: use more appropriate methods likemapandmapErr. - Type inference failure when the mapper function is generic. The failure is visible
the
Oktype being inferred asunknown. Workaround: instead ofandThen(someGenericFunction)useandThen((v) => someGenericFunction(v)).
- Reduced type narrowing when the input type is known to be
Added:
- Made
AsyncOptionandAsyncResultawaitable - you can now useawait asyncResultinstead ofawait asyncResult.promise.
5.0.1
Fixed:
- Fixed the regression introduced in 5.0.0 as part of the
AsyncResult.andThenfix. The fix is completely reverted for now.
5.0.0
Backwards incompatible:
Changed
OptionandResultiterator behavior such that iteratingSomeandOkwill 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
NoneandErris not affected and continues to produce no results.- Removed the parameter spread variants of
Result.allandResult.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.orandResult.orElsemethod types to actually be callable and return reasonable types when called. - Attempted to fix
AsyncResult.andThento 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.partitionsignature.
Added:
Option.unwrapOrElseResult.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.partitionconvenience method
4.1.0
- A whole bunch of documentation changes
- Introduced
AsyncResultto allow composing results with asynchronous code - Introduced
AsyncOptionas well - Fixed
Option.anybehavior - Fixed an edge case in using
ts-results-esin 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.valueResult.val->Ok.valueandErr.errorOption.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 bothOptionandResult
3.5.0
- Added
andThen()documentation, thanks to Drew De Ponte - Added the
expectErr()method toResult, thanks to TheDudeFromCI - Added
mapOr()andmapOrElse()to bothOptionandResult
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
stackproperty to allErrobjects. Can be used to pull a stack trace - Added
toOptionandtoResultmethods for converting betweenOptionandResultobjects
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>, andNonetypes!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.isOptionandResult.isResulthelper 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 asResultsfrom previous releases. Collects allOkvalues, or returns the firstErrvalue.Results.any(...)- Returns the firstOkvalue, or all of theErrvalues.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
elsein favor ofunwrapOrto prefer api parity with Rust
v2.0.1
New Features
- core: Added
reaonly static EMPTY: Ok<void>;toOkclass. - core: Added
reaonly static EMPTY: Err<void>;toErrclass.
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
Resultsfunction that allows combining multiple results.
New Features
- core: much simpler Typescript types
- rxjs: added new
filterResultOkandfilterResultErroperators - rxjs: added new
resultMapErrTooperator
Breaking Changes
- core:
ErrandOknow requirenew:- Before:
let result = Ok(value); let error = Err(message); - After:
let result = new Ok(value); let error = new Err(message);
- Before:
- core:
mapfunction broken into two functions:mapandmapErr- before:
result.map(value => "new value", error => "new error") - after:
result.map(value => "newValue").mapError(error => "newError")
- before:
- rxjs:
resultMapoperator broken into two operators:resultMapandresultMapErr- before:
obs.pipe(resultMap(value => "new value", error => "new error")) - after:
result.pipe(resultMap(value => "newValue"), resultMapError(error => "newError"))
- before: