Skip to content

True Myth / maybe / Nothing

Interface: Nothing<T> ​

A Nothing instance is the absent variant instance of the Maybe type, representing the presence of a value which may be absent. For a full discussion, see the module docs.

Extends ​

  • Omit<MaybeImpl<T>, "value">

Type Parameters ​

T ​

T extends object

The type which would be wrapped in a Just variant of the Maybe.

Properties ​

isJust ​

isJust: false

Is the Maybe a Just?

Overrides ​

Omit.isJust


isNothing ​

isNothing: true

Is the Maybe a Nothing?

Overrides ​

Omit.isNothing


variant ​

readonly variant: "Nothing"

Distinguish between the Just and Nothing variants.

Overrides ​

Omit.variant

Methods ​

and() ​

and<U>(mAnd): Maybe<U>

Method variant for and

Type Parameters ​

U ​

U extends object

Parameters ​

mAnd ​

Maybe<U>

Returns ​

Maybe<U>

Inherited from ​

Omit.and


andThen() ​

Call Signature ​

andThen<U>(andThenFn): Maybe<U>

Method variant for andThen

Type Parameters ​
U ​

U extends object

Parameters ​
andThenFn ​

(t) => Maybe<U>

Returns ​

Maybe<U>

Inherited from ​

Omit.andThen

Call Signature ​

andThen<R>(andThenFn): Maybe<ValueFor<R>>

Method variant for andThen

Type Parameters ​
R ​

R extends AnyMaybe

Parameters ​
andThenFn ​

(t) => R

Returns ​

Maybe<ValueFor<R>>

Inherited from ​

Omit.andThen


ap() ​

ap<A, B>(this, val): Maybe<B>

Method variant for ap

Type Parameters ​

A ​

A extends object

B ​

B extends object

Parameters ​

this ​

Maybe<(val) => B>

val ​

Maybe<A>

Returns ​

Maybe<B>

Inherited from ​

Omit.ap


equals() ​

equals(comparison): boolean

Method variant for equals

Parameters ​

comparison ​

Maybe<T>

Returns ​

boolean

Inherited from ​

Omit.equals


flatten() ​

flatten<A>(this): Maybe<A>

Given a nested Maybe, remove one layer of nesting.

For example, given a Maybe<Maybe<string>>, the resulting type after using this method will be Maybe<string>.

Note ​

This method only works when the value wrapped in Maybe is another Maybe. If you have a Maybe<string> or Maybe<number>, this method won't work. If you have a Maybe<Maybe<string>>, then you can call .flatten() to get back a Maybe<string>.

Examples ​

ts
import * as maybe from 'true-myth/maybe';

const nested = maybe.just(maybe.just('hello'));
const flattened = nested.flatten(); // Maybe<string>
console.log(flattened); // Just('hello')

const nestedNothing = maybe.just(maybe.nothing<string>());
const flattenedNothing = nestedNothing.flatten(); // Maybe<string>
console.log(flattenedNothing); // Nothing

const nothingNested = maybe.nothing<Maybe<string>>();
const flattenedOuter = nothingNested.flatten(); // Maybe<string>
console.log(flattenedOuter); // Nothing

Type Parameters ​

A ​

A extends object

Parameters ​

this ​

Maybe<Maybe<A>>

Returns ​

Maybe<A>

Inherited from ​

Omit.flatten


get() ​

get<K>(key): Maybe<NonNullable<T[K]>>

Method variant for get

If you have a Maybe of an object type, you can do thatMaybe.get('a key') to look up the next layer down in the object.

ts
type DeepOptionalType = {
  something?: {
    with?: {
      deeperKeys?: string;
    }
  }
};

const fullySet: DeepType = {
  something: {
    with: {
      deeperKeys: 'like this'
    }
  }
};

const deepJust = Maybe.of(fullySet)
  .get('something')
  .get('with')
  .get('deeperKeys');

console.log(deepJust); // Just('like this');

const partiallyUnset: DeepType = { something: { } };

const deepEmpty = Maybe.of(partiallyUnset)
  .get('something')
  .get('with')
  .get('deeperKeys');

console.log(deepEmpty); // Nothing

Type Parameters ​

K ​

K extends string | number | symbol

Parameters ​

key ​

K

Returns ​

Maybe<NonNullable<T[K]>>

Inherited from ​

Omit.get


inspect() ​

inspect(fn): Maybe<T>

Run a side effect with the wrapped value without modifying the Maybe.

This is useful for performing actions like logging, debugging, or other “side effects” external to the wrapped value. (Note: You should never mutate the value in the callback. Doing so will be extremely surprising to callers.) The function is only called if the Maybe is a Just, and the original Maybe is returned unchanged for further operations.

ts
import * as maybe from 'true-myth/maybe';

const log = (value: unknown) => console.log(value);
const double = (value: number) => value * 2;

// Logs `42` then `84`, and returns `Just(84)`.
maybe.just(42).inspect(log).map(double).inspect(log);

// Does not log anything, and returns `Nothing`.
maybe.nothing<number>().inspect(log).map(double).inspect(log);

Parameters ​

fn ​

(value) => void

The function to call with the wrapped value, only called for Just.

Returns ​

Maybe<T>

The original Maybe, unchanged.

Inherited from ​

Omit.inspect


map() ​

map<U>(mapFn): Maybe<U>

Method variant for map

Type Parameters ​

U ​

U extends object

Parameters ​

mapFn ​

(t) => U

Returns ​

Maybe<U>

Inherited from ​

Omit.map


mapOr() ​

mapOr<U>(orU, mapFn): U

Method variant for `mapOr`

Type Parameters ​

U ​

U

Parameters ​

orU ​

U

mapFn ​

(t) => U

Returns ​

U

Inherited from ​

Omit.mapOr


mapOrElse() ​

mapOrElse<U>(orElseFn, mapFn): U

Method variant for mapOrElse

Type Parameters ​

U ​

U

Parameters ​

orElseFn ​

() => U

mapFn ​

(t) => U

Returns ​

U

Inherited from ​

Omit.mapOrElse


match() ​

match<U>(matcher): U

Method variant for match

Type Parameters ​

U ​

U

Parameters ​

matcher ​

Matcher<T, U>

Returns ​

U

Inherited from ​

Omit.match


or() ​

or(mOr): Maybe<T>

Method variant for or

Parameters ​

mOr ​

Maybe<T>

Returns ​

Maybe<T>

Inherited from ​

Omit.or


orElse() ​

Call Signature ​

orElse(orElseFn): Maybe<T>

Method variant for orElse

Parameters ​
orElseFn ​

() => Maybe<T>

Returns ​

Maybe<T>

Inherited from ​

Omit.orElse

Call Signature ​

orElse<R>(orElseFn): Maybe<ValueFor<R>>

Method variant for orElse

Type Parameters ​
R ​

R extends AnyMaybe

Parameters ​
orElseFn ​

() => R

Returns ​

Maybe<ValueFor<R>>

Inherited from ​

Omit.orElse


toJSON() ​

toJSON(): Serialized<T>

Method variant for toJSON

Returns ​

Serialized<T>

Inherited from ​

Omit.toJSON


toString() ​

toString(): string

Method variant for toString

Returns ​

string

Inherited from ​

Omit.toString


unwrapOr() ​

unwrapOr<U>(defaultValue): T | U

Method variant for unwrapOr

Type Parameters ​

U ​

U

Parameters ​

defaultValue ​

U

Returns ​

T | U

Inherited from ​

Omit.unwrapOr


unwrapOrElse() ​

unwrapOrElse<U>(elseFn): T | U

Method variant for unwrapOrElse

Type Parameters ​

U ​

U

Parameters ​

elseFn ​

() => U

Returns ​

T | U

Inherited from ​

Omit.unwrapOrElse