ts-results
Advanced tools
Changelog
v3.1.0
Big thank you to @petehunt for all his work adding the Option
type.
Added new Option<T>
, Some<T>
, and None
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
and Result.isResult
helper functions.
@ts-ignore
Changelog
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.
Ok<T>
and Err<T>
are now callable without new
!Result.all(...)
- Same as Results
from previous releases. Collects all Ok
values, or returns the first Err
value.Results.any(...)
- Returns the first Ok
value, or all of the Err
values.Result.wrap<T, E>(() => ...)
- Wraps an operation that may throw an error, uses try / catch to return
a Result<T, E>
Result.wrapAsync<T, E>(() => ...)
- Same as the above, but asyncelse
in favor of unwrapOr
to prefer api parity with RustChangelog
v2.0.1
reaonly static EMPTY: Ok<void>;
to Ok
class.reaonly static EMPTY: Err<void>;
to Err
class.Changelog
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:
Ok<T>
and Err<E>
.Result<T, E>
type that is a simple or type between the two classes.Results
function that allows combining multiple results.filterResultOk
and filterResultErr
operatorsresultMapErrTo
operatorErr
and Ok
now require new
:
let result = Ok(value); let error = Err(message);
let result = new Ok(value); let error = new Err(message);
map
function broken into two functions: map
and mapErr
result.map(value => "new value", error => "new error")
result.map(value => "newValue").mapError(error => "newError")
resultMap
operator broken into two operators: resultMap
and resultMapErr
obs.pipe(resultMap(value => "new value", error => "new error"))
result.pipe(resultMap(value => "newValue"), resultMapError(error => "newError"))