Options
All
  • Public
  • Public/Protected
  • All
Menu

Class 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.

Type parameters

  • T

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

Hierarchy

  • Nothing

Implements

Index

Constructors

constructor

  • new Nothing(_?: undefined | null): Nothing
  • Create an instance of Maybe.Nothing with new.

    note

    While you may create the Nothing type via normal JavaScript class construction, it is not recommended for the functional style for which the library is intended. Instead, use Maybe.of (for the general case) or Maybe.nothing for this specific case.

    // Avoid:
    const aNothing = new Maybe.Err();
    
    // Prefer:
    const aNothing = Maybe.nothing();

    null and undefined are allowed so that you may explicitly construct the Err type with a known null or undefined value. (This maybe helpful primarily when transitioning a codebase to the use of Maybe.)

    throws

    If you pass null or undefined.

    Parameters

    • Optional _: undefined | null

    Returns Nothing

Properties

variant

variant: Nothing = Variant.Nothing

Nothing is always Variant.Nothing.

Methods

and

andThen

  • andThen<U>(this: Maybe<T>, andThenFn: function): Maybe<U>

ap

chain

  • chain<U>(this: Maybe<T>, chainFn: function): Maybe<U>

equals

  • equals(this: Maybe<T>, comparison: Maybe<T>): boolean

flatMap

  • flatMap<U>(this: Maybe<T>, flatMapFn: function): Maybe<U>

get

  • get<K>(this: Maybe<T>, key: K): Maybe<NonNullable<T[K]>>
  • Method variant for Maybe.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.

    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: keyof T

    Parameters

    Returns Maybe<NonNullable<T[K]>>

isJust

  • isJust(this: Maybe<T>): this is Just<T>

isNothing

  • isNothing(this: Maybe<T>): this is Nothing<T>

map

  • map<U>(this: Maybe<T>, mapFn: function): Maybe<U>
  • Method variant for Maybe.map

    Type parameters

    • U

    Parameters

    • this: Maybe<T>
    • mapFn: function
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns Maybe<U>

mapOr

  • mapOr<U>(this: Maybe<T>, orU: U, mapFn: function): U
  • Method variant for Maybe.mapOr

    Type parameters

    • U

    Parameters

    • this: Maybe<T>
    • orU: U
    • mapFn: function
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns U

mapOrElse

  • mapOrElse<U>(this: Maybe<T>, orElseFn: function, mapFn: function): U
  • Method variant for Maybe.mapOrElse

    Type parameters

    • U

    Parameters

    • this: Maybe<T>
    • orElseFn: function
        • (): U
        • Returns U

    • mapFn: function
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns U

match

or

orElse

  • orElse(this: Maybe<T>, orElseFn: function): Maybe<T>

toJSON

toOkOrElseErr

  • toOkOrElseErr<E>(this: Maybe<T>, elseFn: function): Result<T, E>

toOkOrErr

toString

  • toString(this: Maybe<T>): string

unsafelyUnwrap

  • unsafelyUnwrap(): never

unwrapOr

  • unwrapOr(this: Maybe<T>, defaultValue: T): T

unwrapOrElse

  • unwrapOrElse(this: Maybe<T>, elseFn: function): T

Generated using TypeDoc