minifunc

Option

An Option is a type that represents either a value or nothing. It is similar to Maybe in Haskell or Option in Rust.

Initialization

import { Option, Some, None } from 'minifunc';

const some = Some(1);
const none = None();

[!NOTE] None is a function for type safety. If you’re using javascript/don’t care about type safety, you can use None as a value: const none = None();. This is not recommended.

Notes

API

Option<T>

interface Option<T> {
    isSome(): boolean;
    isNone(): boolean;
    fromNullable<T>(value: T | undefined | null): Option<T>;
    unwrap(): T;
    unwrapOr(value: T): T;
    unwrapOrElse(fn: () => T): T;
    unwrapUnchecked(): T;
    map<U>(fn: (value: T) => U): Option<U>;
}

Some(..) and None(..)

Both are exported type aliases for Option.Some and Option.None respectively.