True Myth

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

README

True Myth


A library for safe, idiomatic null and error handling in TypeScript, with Maybe and Result types, supporting both a functional style and a more traditional method-call style.


READMEAPI docsSourceIntro blog post


## Overview

NOTE: this documentation is for version 6.x onwards, which requires using Node 16 module resolution. If you cannot use that yet, please use version 5.x.

True Myth provides standard, type-safe wrappers and helper functions to help help you with two _extremely_ common cases in programming:

- not having a value
- having a _result_ where you need to deal with either success or failure

You could implement all of these yourself – it's not hard! – but it's much easier to just have one extremely well-tested library you can use everywhere to solve this problem once and for all.

### Contents

  - [Result with a functional style](#result-with-a-functional-style)
  - [Maybe with the method style](#maybe-with-the-method-style)
  - [Constructing Maybe](#constructing-maybe)
  - [1. Nothingness: null and undefined](#1-nothingness-null-and-undefined)
- [Solutions: Maybe and Result](#solutions-maybe-and-result)
  - [How it works: Maybe](#how-it-works-maybe)
  - [How it works: Result](#how-it-works-result)
    - [Maybe](#maybe)
    - [Result](#result)
  - Folktale?

Requirements


- Node 14+
- TS 4.7+
- tsconfig.json:
  - moduleResolution: "Node16"
- package.json
  - type: "module" (or else use import() to import True Myth into a commonJS build)

For details on using a pure ES modules package in TypeScript, see the TypeScript handbook's guide.

Setup


Add True Myth to your dependencies:

- with Yarn:

  sh
  yarn add true-myth
  

- with npm:

  sh
  npm install true-myth
  

This package ships ES6-modules so you can import the modules directly, or import the re-exports from the root module:

  1. ```typescript
  2. // this works:
  3. import Maybe from 'true-myth/maybe';
  4. import Result from 'true-myth/result';

  5. // this also works:
  6. import { Maybe, Result } from 'true-myth';
  7. ```

Basic bundle size info


Size without tree-shaking:

filesizeterser[^terser]terser
-----------------------------------------------------------------------------
index.js0.1370.0880.069
maybe.js6.13.10.820
result.js5.72.80.715
toolbelt.js1.30.7750.250
unit.js0.0910.0560.055
utils.js0.5840.3120.161
**total[^total]**13.9127.1312.07

[^terser]: Using terser 5.10.0 with--compress --mangle --mangle-props.

[^brotli]: Generated by running gzip -kq11 on the result of the terser invocation.

[^total]: This is just the sum of the previous lines. Real-world bundle size is a function of what you actually use, how your bundler handles tree-shaking, and how the results of bundling compresses. Notice that sufficiently small files can end up _larger_ after compression; this stops being an issue once part of a bundle.

Compatibility


This project follows the current draft of [the Semantic Versioning for TypeScript Types][semver] specification.

- Currently supported TypeScript versions: 4.7
- Compiler support policy: [simple majors][sm]
- Public API: all published types not in a -private module are public

[semver]: https://www.semver-ts.org
[sm]: https://www.semver-ts.org/#simple-majors

Just the API, please


_If you're unsure of why you would want to use the library, you might jump down to [Why do I need this?](#why-do-i-need-this)._

These examples don't cover every corner of the API; it's just here to show you what a few of the functions are like. [Full API documentation is available!][docs] You can also [view the source][source] if you prefer.

[docs]: https://true-myth.js.org
[source]: https://github.com/chriskrycho/true-myth

Result with a functional style


  1. ```typescript
  2. import Result, { err, map, ok, toString } from 'true-myth/result';

  3. function fallibleCheck(isValid: boolean): Result<string, { reason: string }> {
  4.   return isValid ? ok('all fine here') : err({ reason: 'was not valid' });
  5. }

  6. const describe = (s) => 'The outcome was: ' + s;

  7. const wentFine = fallibleCheck(true);
  8. const mappedFine = map(describe, wentFine);
  9. console.log(toString(mappedFine)); // "Ok(The outcome was: all fine here)"

  10. const notGreat = fallibleCheck(false);
  11. const mappedBad = map(describe, notGreat);
  12. console.log(toString(mappedBad)); // "Err({ reason: 'was not valid' })"
  13. ```

Maybe with the method style


  1. ```typescript
  2. import Maybe, { just, nothing } from 'true-myth/maybe';

  3. function safeLength(mightBeAString: Maybe<string>): Maybe<number> {
  4.   return mightBeAString.map((s) => s.length);
  5. }

  6. const justAString = just('a string');
  7. const nothingHere = nothing<string>();
  8. console.log(safeLength(justAString).toString()); // Just(8)
  9. console.log(safeLength(nothingHere).toString()); // Nothing
  10. ```

Constructing Maybe


You can use Maybe.of to construct a Maybe from any value. It will return a Nothing if the passed type is null or undefined, or a Just otherwise.

  1. ```typescript
  2. import Maybe from 'true-myth/maybe';

  3. function acceptsANullOhNo(value: number | null): Maybe<string> {
  4.   const maybeNumber = Maybe.of(value);
  5.   return mapOr('0', (n) => n.toString(), maybeNumber);
  6. }
  7. ```

Safely getting at values


The library provides smart type narrowing tools to allow you to get at the values wrapped in the type:

  1. ```typescript
  2. import { ok } from 'true-myth/result';

  3. const theAnswer = ok(42);
  4. const theAnswerValue = theAnswer.isOk ? theAnswer.value : 0;
  5. ```

However, ternaries like this can be annoying at times, and don't necessarily fit into functional composition pipelines when the expressions become more complicated. For situations like those, you can use one of the safe unwrap methods:

  1. ```typescript
  2. import { ok, unwrapOr } from 'true-myth/result';

  3. const theAnswer = ok(42);
  4. const theAnswerValue = unwrapOr(0, theAnswer);
  5. ```

You can also use TypeScript's "type narrowing" capabilities: if you _check_ which variant you are accessing, TypeScript will "narrow" the type to that variant and allow you to access the value directly if it is available.

  1. ```typescript
  2. import Maybe from 'true-myth/maybe';

  3. // Maybe
  4. const couldBeSomething = Maybe.of('Hello!');

  5. // type error, because `value` does not exist on `Nothing`:
  6. // couldBeSomething.value;

  7. if (couldBeSomething.isJust) {
  8.   // valid, because `couldBeSomething` is "narrowed" to `Just` here:
  9.   console.log(couldBeSomething.value);
  10. }
  11. ```

This can also be convenient in functional style pipelines:

  1. ```typescript
  2. import { filter, map, pipe, prop } from 'ramda';
  3. import Result from 'true-myth/result';

  4. function getErrorMessages(results: Array<Result<string, Error>>) {
  5.   return results
  6.     .filter(Result.isErr)
  7.     .map(Err.unwrapErr) // would not type-checkout with previous line
  8.     .map((error) => error.message);
  9. }
  10. ```

Curried variants


All static functions which take two or more parameters are automatically partially applied/curried so you can supply only _some_ of the arguments as makes sense. For example, if you were using [lodash], you might have something like this:

  1. ```ts
  2. import * as _ from 'lodash/fp';
  3. import { just, nothing, map } from 'true-myth/maybe';

  4. const length = (s: string) => s.length;
  5. const even = (n: number) => n % 2 === 0;
  6. const timesThree = (n: number) => n * 3;

  7. const transform = _.flow(
  8.   // transform strings to their length: Just(3), Nothing, etc.
  9.   _.map(map(length)),
  10.   // drop `Nothing` instances
  11.   _.filter(_.prop('isJust')),
  12.   // get value now that it's safe to do so (TS will not allow it earlier)
  13.   _.map(_.prop('value')),
  14.   // only keep the even numbers ('fish' => 4)
  15.   _.filter(even),
  16.   // multiply by three
  17.   _.map(timesThree),
  18.   // add them up!
  19.   _.sum
  20. );

  21. const result = transform([
  22.   just('yay'),
  23.   nothing(),
  24.   nothing(),
  25.   just('waffles'),
  26.   just('fish'),
  27.   just('oh'),
  28. ]);

  29. console.log(result); // 18
  30. ```

This makes for a much nicer API than needing to include the parameters for every function. If we _didn't_ have the curried functions, we'd have a much, _much_ noisier input:

  1. ```ts
  2. import * as _ from 'lodash';
  3. import { map } from 'true-myth/maybe';

  4. const length = (s: string) => s.length;
  5. const even = (n: number) => n % 2 === 0;
  6. const timesThree = (n: number) => n * 3;

  7. const result = transform([
  8.   Maybe.of('yay'),
  9.   Maybe.nothing(),
  10.   Maybe.nothing(),
  11.   Maybe.of('waffles'),
  12.   Maybe.of('fish'),
  13.   Maybe.of('oh'),
  14. ]);

  15. const transform = _.flow(
  16.   // transform strings to their length: Just(3), Nothing, etc.
  17.   (maybeStrings) => _.map(maybeStrings, (maybeString) => map(length, maybeString)),
  18.   // drop `Nothing` instances
  19.   (maybeLengths) => _.filter(maybeLengths, (maybe) => maybe.isJust),
  20.   // get value now that it's safe to do so (TS will not allow it earlier)
  21.   (justLengths) => _.map(justLengths, (maybe) => maybe.value),
  22.   // only keep the even numbers ('fish' => 4)
  23.   (lengths) => _.filter(lengths, even),
  24.   // multiply by three
  25.   (evenLengths) => _.map(evenLengths, timesThree),
  26.   // add them up!
  27.   _.sum
  28. );

  29. console.log(result); // 18
  30. ```

This "point-free" style isn't always better, but it's available for the times when it _is_ better. ([Use it judiciously!][pfod])

[pfod]: https://www.youtube.com/watch?v=seVSlKazsNk

Why do I need this?


There are two motivating problems for True Myth (and other libraries like it): dealing with _nothingness_ and dealing with _operations which can fail_.

1. Nothingness: null and undefined


How do you represent the concept of _not having anything_, programmatically? As a language, JavaScript uses null to represent this concept; if you have a variable myNumber to store numbers, you might assign the value null when you don't have any number at all. If you have a variable myString, you might set myString = null; when you don't have a string.

Some JavaScript programmers use undefined in place of null or in addition to null, so rather than setting a value to null they might just set let myString; or even let myString = undefined;.

Every language needs a way to express the concept of nothing, but null and undefined are a curse. Their presence in JavaScript (and in many other languages) introduce a host of problems, because they are not a particularly _safe_ way to represent the concept. Say, for a moment, that you have a function that takes an integer as a parameter:

  1. ```js
  2. let myNumber = undefined;

  3. function myFuncThatTakesAnInteger(anInteger) {
  4.   return anInteger.toString();
  5. }

  6. myFuncThatTakesAnInteger(myNumber); // TypeError: anInteger is undefined
  7. ```

this is fine

When the function tries to convert the integer to a string, the function blows up because it was written with the assumption that the parameter being passed in (a) is defined and (b) has a toString method. Neither of these assumptions are true when anInteger is null or undefined. This leads JavaScript programmers to program defensively, with if (!anInteger) return; style guard blocks at the top of their functions. This leads to harder-to-read code, and what's more, _it doesn't actually solve the root problem._

You could imagine this situation playing itself out in a million different ways: arguments to functions go missing. Values on objects turn out not to exist. Arrays are absent instead of merely empty. The result is a steady stream not merely of programming frustrations, but of _errors_. The program does not function as the programmer intends. That means stuff doesn't work correctly for the user of the software.

You can program around null and undefined. But defensive programming is gross. You write a lot of things like this:

  1. ```js
  2. function isNil(thingToCheck) {
  3.   return thingToCheck === undefined || thingToCheck === null;
  4. }

  5. function doAThing(withAString) {
  6.   if (isNil(withAString)) {
  7.     withAString = 'some default value';
  8.   }

  9.   console.log(withAString.length);
  10. }
  11. ```

If you forget that check, or simply assume, "Look, I'll _never_ call this without including the argument," eventually you or someone else will get it wrong. Usually somewhere far away from the actual invocation of doAThing, so that it's not obvious why that value ended up being null there.

TypeScript takes us a big step in that direction, so long as our type annotations are good enough. (Use of any will leave us sad, though.) We can specify that type _may_ be present, using the [optional] annotation. This at least helps keep us honest. But we still end up writing a ton of repeated boilerplate to deal with this problem. Rather than just handling it once and being done with it, we play a never-ending game of whack-a-mole. We must be constantly vigilant and proactive so that our users don't get into broken error states.

[optional]: http://www.typescriptlang.org/docs/handbook/interfaces.html#optional-properties

2. Failure handling: callbacks and exceptions


Similarly, you often have functions whose return value represents an operation which might fail in some way. We also often have functions which have to deal with the result of operations which might fail.

Many patterns exist to work around the fact that you can't very easily return two things together in JavaScript. Node has a callback pattern with an error as the first argument to every callback, set to null if there was no error. Client-side JavaScript usually just doesn't have a single pattern for handling this.

In both cases, you might use exceptions – but often an exception feels like the wrong thing because the possibility of failure is built into the kind of thing you're doing – querying an API, or checking the validity of some date, and so on.

In Node.js, the callback pattern encourages a style where literally every function starts with the exact same code:

  1. ```js
  2. const doSomething = (err, data) => {
  3.   if (err) {
  4.     return handleErr(err);
  5.   }

  6.   // do whatever the *actual* point of the function is
  7. };
  8. ```

There are two major problems with this:

1.  It's incredibly repetitive – the very opposite of "Don't Repeat Yourself". We wouldn't do this with _anything_ else in our codebase!

2.  It puts the error-handling right up front and _not in a good way._ While we want to have a failure case in mind when designing the behavior of our functions, it's not usually the _point_ of most functions – things like handleErr in the above example being the exception and not the rule. The actual meat of the function is always after the error handling.

Meanwhile, in client-side code, if we're not using some similar kind of callback pattern, we usually resort to exceptions. But exceptions are unpredictable: you can't know whether a given function invocation is going to throw an exception until runtime as someone calling the function. No big deal if it's a small application and one person wrote all the code, but with even a few thousand lines of code or two developers, it's very easy to miss that. And then this happens:

  1. ```js
  2. // in one part of the codebase
  3. function getMeAValue(url) {
  4.   if (isMalformed(url)) {
  5.     throw new Error(`The url `${url}` is malformed!`);
  6.   }

  7.   // do something else to load data from the URL
  8. }

  9. // somewhere else in the codebase
  10. const value = getMeAValue('http:/www.google.com');  // missing slash
  11. ```

Notice: there's no way for the caller to know that the function will throw. Perhaps you're very disciplined and write good docstrings for every function – _and_ moreover, perhaps everyone's editor shows it to them _and_ they pay attention to that briefly-available popover. More likely, though, this exception throws at runtime and probably as a result of user-entered data – and then you're chasing down the problem through error logs.

More, if you _do_ want to account for the reality that any function anywhere in JavaScript might actually throw, you're going to write something like this:

  1. ```js
  2. try {
  3.   getMeAValue('http:/www.google.com'); // missing slash
  4. } catch (e) {
  5.   handleErr(e);
  6. }
  7. ```

This is like the Node example _but even worse_ for repetition!

Nor can TypeScript help you here! It doesn't have type signatures to say "This throws an exception!" (TypeScript's never might come to mind, but it might mean lots of things, not just exception-throwing.)

Neither callbacks nor exceptions are good solutions here.

Solutions: Maybe and Result


Maybe and Result are our escape hatch from all this madness.

We reach for libraries precisely so we can solve real business problems while letting lower-level concerns live in the "solved problems" category. True Myth, borrowing ideas from many other languages and libraries, aims to put _code written to defend against null/undefined problems_ in that "solved problems" category.

Maybe and Result solve this problem _once_, and _in a principled way_, instead of in an _ad-hoc_ way throughout your codebase, by putting the value into a _container_ which is guaranteed to be safe to act upon, regardless of whether there's something inside it or not.

These containers let us write functions with _actually safe_ assumptions about parameter values by extracting the question, "Does this variable contain a valid value?" to API boundaries, rather than needing to ask that question at the head of every. single. function.

_What is this sorcery?_

How it works: Maybe


It turns out you probably already have a good idea of how this works, if you've spent much time writing JavaScript, because this is exactly how arrays work.

Imagine, for a moment, that you have a variable myArray and you want to map over it and print out every value to the console. You instantiate it as an empty array and then forget to load it up with values before mapping over it:

  1. ```js
  2. let myArray = [];
  3. // oops, I meant to load up the variable with an array, but I forgot!
  4. myArray.forEach((n) => console.log(n)); //
  5. ```

Even though this doesn't print anything to the screen, it doesn't unexpectedly blow up, either. In other words, it represents the concept of having nothing "inside the box" in a safe manner. By contrast, an integer has no such safe box around it. What if you could multiply an integer by two, and if your variable was "empty" for one reason or another, it wouldn't blow up?

  1. ```js
  2. let myInteger = undefined;

  3. myInteger * 3; // 😢
  4. ```

Let's try that again, but this time let's put the actual value in a container and give ourselves safe access methods:

  1. ```js
  2. import Maybe from 'true-myth/maybe';

  3. const myInteger = Maybe.of(undefined);
  4. myInteger.map((x) => x * 3); // Nothing
  5. ```

mind blown

We received Nothing back as our value, which isn't particularly useful, but it also didn't halt our program in its tracks!

Best of all, when you use these with libraries like TypeScript, you can lean on their type systems to check aggressively for null and undefined, and actually _eliminate_ those from your codebase by replacing anywhere you would have used them with Maybe.

How it works: Result


Result is similar to Maybe, except it packages up the result of an operation (like a network request) whether it's a success (an Ok) or a failure (an Err) and lets us unwrap the package at our leisure. Whether you get back a 200 or a 401 for your HTTP request, you can pass the box around the same either way; the methods and properties the container has are not dependent upon whether there is shiny new data or a big red error inside.

  1. ```typescript
  2. import { ok, err } from 'true-myth/result';

  3. const myNumber = ok<number, string>(12);
  4. const myNumberErr = err<number, string>('oh no');

  5. console.log(myNumber.map((n) => n * 2)); // Ok(24)
  6. console.log(myNumberErr.map((n) => n * 2)); // Err(oh no)
  7. ```

Thus, you can replace functions which take polymorphic arguments or have polymorphic return values to try to handle scenarios where something may be a success or an error with functions using Result.

Any place you try to treat either a Maybe or a Result as just the underlying value rather than the container, the type systems will complain, of course. And you'll also get help from smart editors with suggestions about what kinds of values (including functions) you need to interact with any given helper or method, since the type definitions are supplied.

By leaning on TypeScript to handle the checking, we also get all these benefits with _no_ runtime overhead other than the cost of constructing the actual container objects (which is to say: _very_ low!).

Design philosophy


The design aims for True Myth are:

- to be as idiomatic as possible in JavaScript
- to support a natural functional programming style
- to have zero runtime cost beyond simple object construction and function invocation
- to lean heavily on TypeScript to enable all of the above

In practice, that means:

- You can construct the variant types in the traditional JavaScript way or with a pure function:

  typescript
  import Maybe, { just, nothing } from 'true-myth/maybe';

  const classicalJust = new Maybe('value');
const classicalNothing = new Maybe();

  const functionalJust = just('value');
  const functionalNothing = nothing();
  

- Similarly, you can use methods or pure functions:

  typescript
  import { ok, map } from 'true-myth/result';

  const numberResult = ok(42);
  const ok84 = numberResult.map((x) => x * 2);
  const ok21 = map((x) => x / 2, numberResult);
  

  As this second example suggests, the aim has been to support the most idiomatic approach for each style. This means that yes, you might find it a bit confusing if you're actively switching between the two of them. (Why would you do that?!?)

- Using the library with TypeScript will _just work_ and will provide you with considerable safety out of the box. Using it with JavaScript will work just fine, but there is no runtime checking, and you're responsible to make sure you don't unwrap() a Maybe without checking that it's safe to do so.

- Since this is a TypeScript-first library, we intentionally leave out any runtime type checking. As such, you _should_ make use of the type systems if you want the benefits of the system. Many of the functions simply assume that the types are checked, and _will_ error if you pass in items of the wrong type.

  For example, if you pass a non-Maybe instance to many functions, they will simply fail – even the basic helpers like isJust and isNothing. These assumptions have been made precisely _because_ this is a TypeScript-first library. (See the discussion below comparing True Myth to Folktale and Sanctuary if you aren't using TypeScript and need runtime checking.)

The overarching themes are flexibility and approachability.

The hope is that a team just picking up these ideas for the first time can use them without adapting their whole style to a "traditional" functional programming approach, but a team comfortable with functional idioms will find themselves at home with the style of data-last pure functions. (For a brief discussion of why you want the data last in a functional style, see [this blog post].)

[this blog post]: http://www.chriskrycho.com/2017/collection-last-auto-curried-functions.html
[ramda]: http://ramdajs.com
[lodash]: https://lodash.com

A note on reference types: no deep copies here!


One important note: True Myth does _not_ attempt to deeply-clone the wrapped values when performing operations on them. Instead, the library assumes that you will _not_ mutate those objects in place. (Doing more than this would require taking on a dependency on e.g. [lodash]). If you violate that constraint, you can and will see surprising outcomes. Accordingly, you should take care not to mutate reference types, or to use deep cloning yourself when e.g. mapping over reference types.

  1. ```typescript
  2. import { just, map } from 'true-myth/maybe';

  3. const anObjectToWrap = {
  4.   desc: ['this', ' ', 'is a string'],
  5.   val: 42,
  6. };

  7. const wrapped = just(anObjectToWrap);
  8. const updated = map((obj) => ({ ...obj, val: 92 }), wrapped);

  9. console.log((anObjectToWrap as Just<number>).val); // 42
  10. console.log((updated as Just<number>).val); // 92
  11. console.log((anObjectToWrap as Just<string[]>).desc); // ["this", " ", "is a string"]
  12. console.log((updated as Just<string[]>).desc); // ["this", " ", "is a string"]

  13. // Now mutate the original
  14. anObjectToWrap.desc.push('.');

  15. // And… 😱 we've mutated the new one, too:
  16. console.log((anObjectToWrap as Just<string[]>).desc); // ["this", " ", "is a string", "."]
  17. console.log((updated as Just<string[]>).desc); // ["this", " ", "is a string", "."]
  18. ```

In other words: you _must_ use other tools along with True Myth if you're going to mutate objects you're wrapping in Maybe or Result.

True Myth will work quite nicely with [lodash], [Ramda], [Immutable-JS], etc., so you can use whatever tools you like to handle this problem.

[immutable-js]: http://facebook.github.io/immutable-js/

The type names


Maybe


The existing options in this space include Option, Optional, and Maybe. You could also point to "nullable," but that actually means the _opposite_ of what we're doing here – these represent types which can _not_ be nullable!

Option implies a choice between several different _options_; in this case that's not really what's going on. It's also not really a great word for the type in the sense that it's weird to read aloud: "an Option string" doesn't make any sense in English.

Optional is much better than Option. The semantics are much more accurate, in that it captures that the thing is allowed to be absent. It's also the nicest grammatically: "an Optional string". On the other hand, it's also the _longest_.

Maybe seems to be the best type name semantically: we're modeling something which _may_ be there – or may _not_ be there! Grammatically, it's comparable to "optional": "a Maybe string" isn't great – but "maybe a string" is the most natural _accurate_ way to answer the question, "What's in this field?" It's also the shortest!

Optional or Maybe are both good names; Maybe just seemed slightly better.

The Maybe variants: Just and Nothing

Similar consideration was given to the names of the type variants. Options for the "present" type in other libraries are Some and Just. Options for the "absent" type are None or Nothing.

Why Just?

Both Just and Some are reasonable choices for this, and both have things to recommend them semantically:

- When talking about the _type_ of given item, "some" makes a lot of sense: "What's in this field? Some number." You can get the same idea across with "just" but it's a bit less clear: "What's in this field? Just a number."
- On the other hand, when talking about or constructing a given _value_, "just" makes more sense: "What is this? It's just 12." When you try to use "some" there, it reads oddly: "What is this? It's some 12."

Given that "just a number" _works_ (even if it's strictly a little less nice than "some number") and that "just 12" works but "some 12" doesn't, Just seems to be a slightly better option.

Why Nothing?

Given the choice between None and Nothing, the consideration just came down to the most natural _language_ choice. "What's here? Nothing!" makes sense, while "What's here? None" does not. None also implies that there might be more than one of the items. It's entirely unnatural to say "There is none of a number here"; you'd normally say "there is no number here" or "there is nothing here" instead. So Nothing it is!

Result


In some languages and libraries, a more general type named Either is used instead of the more specific Result name. The two are equivalent in functionality – both provide two variants, each of which wraps a value. In the Either implementations, those are usually named Left and Right. In the Result implementations (both here and in other libraries and languages), they are named Ok and Err.

The main difference between Either and Result is precisely that question of generality. Either can meaningfully capture _any_ scenario where there are two possible values resulting from a given function application, or applicable as arguments to a function. Result _only_ captures the idea of something succeeding or failing. In that sense, Either might seem to be better: it can capture what Result captures (traditionally with Left being the error case and Right being the success, or _right_, case), and many more besides.

However, in practice, the idea of a result is far and away the most common case for using an Either, and it's also the easiest to explain. (An Either implementation would also be valuable, though, and it might be a later addition to the library.)

The Result variants: Ok and Err

Given a "result" type, we need to be able to express the idea of "success" and "failure." The most obvious names here would be Success and Failure. Those are actually really good names with a single problem: they're _long_. Needing to write success(12) or failure({ oh: 'no' }) is a _lot_ to write over and over again. Especially when there some options which _also_ work well: Ok and Err.

Both Ok and Err could be written out long-form: Okay and Error. But in this case, the longer names don't add any particular clarity; they require more typing; and the Error case also overloads the existing name of the base exception type in JavaScript. So: Ok and Err it is.

Inspiration


The design of True Myth draws heavily on prior art; essentially nothing of this is original – _perhaps_ excepting the choice to make Maybe.of handle null and undefined in constructing the types. In particular, however, True Myth draws particular inspiration from:

- Rust's [Option][rs-option] and [Result][rs-result] types and their associated methods
- Folktale's [Maybe][ft-maybe] and [Result][ft-result] implementations
- Elm's [Maybe][elm-maybe] and [Result][elm-result] types and their
  associated functions

[rs-option]: https://doc.rust-lang.org/stable/std/option/
[rs-result]: https://doc.rust-lang.org/stable/std/result/
[ft-maybe]: http://folktale.origamitower.com/api/v2.0.0/en/folktale.maybe.html
[ft-result]: http://folktale.origamitower.com/api/v2.0.0/en/folktale.result.html
[elm-maybe]: http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Maybe
[elm-result]: http://package.elm-lang.org/packages/elm-lang/core/5.1.1/Result

Why not...


There are other great functional programming libraries out there... so why not just use one of them?

Note that much of the content between these sections is the same; it's presented as is so you can simply read the section appropriate to the library you're comparing it with.

Folktale?


[Folktale] has an API a lot like this one, as you'll see when perusing the docs. However, there are two main reasons you might prefer True Myth to Folktale:

[folktale]: http://folktale.origamitower.com

1.  True Myth is TypeScript-first, which means that it assumes you are using TypeScript if you're aiming for rigorous type safety.

    By contrast, Folktale is a JavaScript-first library, with runtime checking built in for its types. Folktale's TypeScript support is in-progress, but will remain secondary until a TypeScript rewrite of the whole Folktale library lands... eventually.

    There's value in both of these approaches, so True Myth aims to take advantage of the compilers and play in a no-runtime-cost space.

    If you want a JS-focused (rather than TS-focused) library which will help you be safer without a compiler, you should definitely pick Folktale over True Myth. If you've already using TS, True Myth is a bit nicer of an experience.

2.  True Myth aims to keep functional programming jargon to a minimum and to use TypeScript type notation throughout its docs as well as in its implementation.

    Folktale is aimed squarely at people who are already pretty comfortable with the world of strongly-typed functional programming languages. This is particularly evident in the way its type signatures are written out (using the same basic notation you might see in e.g. Haskell), but it's also there in its heavy use of functional programming terminology throughout its docs.

    Haskell-style types are quite nice, and functional programming jargon is very useful. However, they're also another hump to get over. Again: a tradeoff.

    By opting for type notation that TS developers are already familiar with, and by focusing on what various functions _do_ rather than the usual FP names for them, True Myth aims at people just coming up to speed on these ideas.

    The big win for Folktale over True Myth is [Fantasy Land] compatibility.

3.  True Myth's API aims to be more idiomatic as JavaScript/TypeScript, with a couple differences in particular worth calling out:

    - function naming convention: True Myth uses PascalCase for types and camelCase for functions – so, new Just(5) and just(5), whereas FolkTale uses the capitals as function names for type constructors, i.e. Just(5), and does not support new.

    - ease of construction from nullable types: True Myth allows you to construct Maybe types from nullable types with Maybe.of, because JS is _full_ of null and undefined, and allowing Maybe.of to handle them makes it easier to be sure you're always doing the right thing.

      Folktale's Maybe.of only allows the use of non-nullable types, and requires you to use Maybe.fromNullable instead. This isn't unreasonable, but it dramatically decreases the convenience of integration with existing JS codebases or interfacing with untyped JS libraries.

4.  Folktale also aims to provide a larger suite of types and functions to use – though much smaller than [lodash] – including a number of [general functions][folktale-core], [concurrency][folktale-concurrency], [general union types][folktale-adt], and more. True Myth intentionally punts on those concerns, assuming that most consumers are already using a library like Lodash or Ramda, and are comfortable with or prefer using e.g. Promises for concurrency, and aiming to be easy to integrate with those instead.

[fantasy land]: https://github.com/fantasyland/fantasy-land
[folktale-core]: http://folktale.origamitower.com/api/v2.0.0/en/folktale.core.html
[folktale-concurrency]: http://folktale.origamitower.com/api/v2.0.0/en/folktale.concurrency.html
[folktale-adt]: http://folktale.origamitower.com/api/v2.0.0/en/folktale.adt.html

Sanctuary?


[Sanctuary] has many of the same goals as True Myth, but is much more focused on the expectations and patterns you'd see in Haskell or PureScript or similar languages. Its API and True Myth's are much _less_ similar than Folktale and True Myth's are, as a result – the underlying details are often similar, but the names are nearly all different. A few of the major contrasts:

[sanctuary]: https://sanctuary.js.org

1.  True Myth is TypeScript-first, which means that it assumes you are using TypeScript if you're aiming for rigorous type safety.

    By contrast, Sanctuary is a JavaScript-first library, with runtime checking built in for its types. Sanctuary's TypeScript support is [in progress][s-ts], but will for the foreseeable future remain add-on rather than first-class. (Sanctuary _does_ allow you to create a version of the module without the runtime checking, but it requires you to do this yourself.)

    There's value in both of these approaches, so True Myth aims to take advantage of the compilers and play in a no-runtime-cost space.

    If you want a JS-focused (rather than TS-focused) library which will help you be safer without a compiler, you should definitely pick Sanctuary over True Myth. If you've already using TS, True Myth is a bit nicer of an experience.

2.  True Myth aims to keep functional programming jargon to a minimum and to use TypeScript type notation throughout its docs as well as in its implementation.

    Sanctuary is aimed squarely at people who are already extremely comfortable the world of strongly-typed, pure functional programming languages. This is particularly evident in the way its type signatures are written out (using the same notation you would see in Haskell or PureScript), but it's also present in Sanctuary's heavy use of functional programming terminology throughout its docs.

    Haskell- and Purescript-style types are quite nice, and the functional programming jargon is very useful. However, they're also another hump to get over. Again: a tradeoff.

    By opting for type notation that TS developers are already familiar with, and by focusing on what various functions _do_ rather than the usual FP names for them True Myth aims at people just coming up to speed on these ideas.

    The big win for Sanctuary over True Myth is [Fantasy Land] compatibility, or familiarity if coming from a language like Haskell or PureScript.

3.  True Myth's API aims to be more idiomatic as JavaScript/TypeScript, with a one difference in particular worth calling out: the function naming convention. True Myth uses PascalCase for types and camelCase for functions – so, new Just(5) and just(5), whereas Sanctuary uses the capitals as function names for type constructors, i.e. S.Just(5), and does not support new.

4.  Sanctuary also aims to provide a much larger suite of functions, more like [Ramda], but with Haskell- or PureScript-inspired type safety and sophistication. True Myth intentionally punts on those concerns, assuming that most consumers are already using a library like Lodash or Ramda and aiming to be easy to integrate with those instead.

[s-ts]: https://github.com/sanctuary-js/sanctuary/pull/431

What's with the name?


For slightly quirky [historical reasons], libraries which borrow ideas from typed functional programming in JavaScript often use names related to the phrase "fantasy land" – especially [Fantasy Land] itself and [Folktale].

[historical reasons]: https://github.com/promises-aplus/promises-spec/issues/94#issuecomment-16176966

"True Myth" leans on that history (and serves, hopefully, as a respectful nod to Folktale in particular, as both Folktale and Sanctuary are huge inspirations for this library), and borrows an idea from J.R.R. Tolkien and C.S. Lewis: what if all myths appeal to us because they point ultimately at something true – and what if some story with the structure of a myth _were_ true in history? It's a beautiful idea, and the name of this library was picked as an homage to it.