A library for safer and smarter error- and "nothing"-handling in TypeScript.
.value
(@chriskrycho)null
and undefined
(@chriskrycho)Result.ok()
(@chriskrycho)Result.ok()
(@chriskrycho)main
and next
with Dependabot (@chriskrycho)true-myth/toolbelt
module for better tree shaking (@chriskrycho)package.json
to exports
(@chriskrycho)package.json
to exports
by @chriskrycho in https://github.com/true-myth/true-myth/pull/243
Full Changelog: https://github.com/true-myth/true-myth/compare/v5.0.0...v5.0.1
The top-level namespace-style export has been removed. If you were relying on the static members to be present when doing import Maybe from 'true-myth/maybe'
or import Result from 'true-myth/result';
, you can replace them with import * as Maybe from 'true-myth/maybe';
or import * as Result from 'true-myth/result';
. This should make for much better tree-shaking with bundlers like Rollup, which can see “through” a namespace import in a way they cannot with a manually-created namespace object. Where you want to maintain the type and namespace imports, you can do this:
import * as Maybe from 'true-myth/maybe';
type Maybe<T> = Maybe.Maybe<T>; // convenience alias
In general, though, you should prefer to simply import the named functions instead:
import Maybe, { transposeArray } from 'true-myth/maybe';
There are no longer separate classes for Just
, Nothing
, Ok
, and Err
. This substantially reduces the size of the library, and should hopefully improve browsers' ability to optimize code using these, since there is now only one class in each case: Maybe
and Result
. The public API for the classes is unchanged, but if you relied on instanceof
checks against those classes anywhere, those checks will no longer work.
The exported Variant
types are now frozen, constant objects, not TypeScript enum
s. This should not break anyone, since the only difference in observable behavior between an enum
and a const
is the ability to do a “reverse lookup” on an enum
—but since the field names and their values are identical, this just means shipping less, and faster, code.
The isJust
, isNothing
, isOk
, and isErr
methods have been converted to getters. This makes them muchmore immediately useful in contexts where invoking a function is annoying, for any reason—for example, in Ember templates.
We no longer publish CommonJS modules, only ES Modules.
Dropped support for Node versions earlier than Node 12 LTS.
Support for versions of TypeScript before 4.0 have been removed, to enable the type-safe re-implementation of Maybe.all
.
The MaybeShape
and ResultShape
interfaces are no longer exported. These were never intended for public reimplementation, and there is accordingly no value in their continuing to be public.
A number of aliases (originally designed to make migration from e.g. Folktale easier) have been removed:
cata
: use match
chain
and flatMap
: use andThen
maybeify
and transmogrify
: use wrapReturn
unsafelyGet
, unsafeGet
, and unsafelyUnwrap
: use .isJust
/.isOk
then .value
unsafelyGetErr
and unsafelyUnwrapErr
: use .isErr
then .error
getOr
: use unwrapOr
getOrElse
: use unwrapOrElse
fromNullable
and maybe
:
Maybe
and use its static constructor Maybe.of
Maybe.of
of
and alias it as you likeWe introduced a new Maybe.transposeArray
, which is a type-safe, renamed, merged version of Maybe.tuple
and Maybe.all
which can correctly handle both array and tuple types. To support this change, it now accepts arrays or tuples directly, and the variadic/spread arguments to all
have been replaced with taking an array or tuple directly. While tuple
and all
are unchanged, they are also deprecated (see below).
Before:
import Maybe, { all, just, nothing, tuple } from 'true-myth/maybe';
// arrays
type ArrayResult = Maybe<Array<string | number>>;
let mixedArray = [just("hello"), nothing<number>()];
let mixedArrayResult: ArrayResult = all(...arrayOfMaybes);
let allJustArray = [just("hello"), just(12)];
let allJustArrayResult: ArrayResult = all(...allJustArray);
type Tuple = [Maybe<number>, Maybe<string>];
type TupleResult = Maybe<[number, string]>;
let mixedTuple: Tuple = [just(12), just("hi")];
let mixedTupleResult: TupleResult = tuple(mixedTuple);
After:
import Maybe, { arrayTranspose, just, nothing } from 'true-myth/maybe';
// arrays
type ArrayResult = Maybe<Array<string | number>>;
let mixedArray = [just("hello"), nothing<number>()];
let mixedArrayResult: ArrayResult = arrayTranspose(arrayOfMaybes);
let allJustArray = [just("hello"), just(12)];
let allJustArrayResult: ArrayResult = arrayTranspose(allJustArray);
// Tuples now work with `arrayTranspose` as well.
type Tuple = [Maybe<number>, Maybe<string>];
type TupleResult = Maybe<[number, string]>;
let mixedTuple: Tuple = [just(12), just("hi")];
let mixedTupleResult: TupleResult = arrayTranspose(mixedTuple);
Maybe.transpose
and Result.transpose
: for when you have a Maybe<Result<T, E>>
or a Result<Maybe<T>, E>
and need to invert them.
import Maybe, { just, nothing } from 'true-myth/maybe';
import Result, { ok, err } from 'true-myth/result';
let anOkJust: Result<Maybe<number>, string> = ok(just(12));
let maybe: Maybe<number>, string> = Maybe.transposeResult(anOkJust);
console.log(maybe); // Just(Ok(12))
let aJustOk: Maybe<Result<number, string>> = just(ok(12));
let result: Maybe<Result<number, string>> = Result.transposeMaybe(aJustOk);
console.log(result); // Ok(Just(12))
See the docs for further details!
Note: these are standalone functions, not methods, because TypeScript does not support conditionally supplying a method only for one specific type parameterization.
Maybe.tuple
and Maybe.all
are deprecated in favor of Maybe.arrayTranspose
now correctly handles both arrays and tuples. They will be removed not earlier than 6.0.0 (timeline not decided, but not sooner than when Node 12 LTS reaches end of life on April 30, 2022).Maybe.all
implementation by @chriskrycho in https://github.com/true-myth/true-myth/pull/88
*Shape
interface exports by @chriskrycho in https://github.com/true-myth/true-myth/pull/93
any
from the codebase by @chriskrycho in https://github.com/true-myth/true-myth/pull/94
engines
, fix authors/contributors by @chriskrycho in https://github.com/true-myth/true-myth/pull/121
ap
docs about currying by @chriskrycho in https://github.com/true-myth/true-myth/pull/216
transpose
handling by @chriskrycho in https://github.com/true-myth/true-myth/pull/219
Full Changelog: https://github.com/true-myth/true-myth/compare/v4.1.1...v5.0.0