True Myth Versions Save

A library for safer and smarter error- and "nothing"-handling in TypeScript.

v5.3.1

2 years ago

:bug: Bug Fix

Committers: 1

v5.3.0

2 years ago

:rocket: Enhancement

:bug: Bug Fix

Committers: 2

v5.2.0

2 years ago

:rocket: Enhancement

Committers: 1

v5.1.3

2 years ago

:bug: Bug Fix

Committers: 1

v5.1.2

2 years ago

5.1.2 (2021-12-27)

:bug: Bug Fix

:memo: Documentation

Committers: 1

v5.1.1

2 years ago

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

Committers: 1

v5.1.0

2 years ago

:rocket: Enhancement

Committers: 1

v5.0.1

2 years ago

:bug: Bug Fix

:memo: Documentation

Committers: 1

Full change list

Full Changelog: https://github.com/true-myth/true-myth/compare/v5.0.0...v5.0.1

v5.0.0

2 years ago

:boom: Changed

  • 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 enums. 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:
      • import Maybe and use its static constructor Maybe.of
      • import the module as namespace and use Maybe.of
      • import of and alias it as you like

:star: Added

  • We 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.

:red_square: Deprecated

  • 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).

Full change list

New Contributors

Full Changelog: https://github.com/true-myth/true-myth/compare/v4.1.1...v5.0.0

v4.1.1

3 years ago

Fixed :wrench:

  • Set stripInternal to false for generated types (#97), so that they type-check.

Upgrading :gear:

With yarn:

yarn upgrade true-myth@latest

With npm:

npm install true-myth@latest

Contributors 🙇

  • @chriskrycho
  • @strmer15