UNPKG

rivo

Version:

🤖 The ultimate library you need for composable type-level programming in TypeScript, powered by HKT.

98 lines (88 loc) • 2.55 kB
import type FlattenFn from "./Flatten"; import type MapFn from "./Map"; import type UnwrapFn from "./Unwrap"; import type UnwrapOrFn from "./UnwrapOr"; import type { IsNoneFn, IsSomeFn, None, OfSomeFn, Some, SomeL } from "./core"; import type { Fn1, PartialApply } from "../HKT"; import type { Lazied } from "../helpers"; /** * A type that represents a value that may or may not exist. */ export type Option<T = unknown> = Some<T> | None; /** * {@link Lazied} version of {@link Option}. */ export type OptionL<L extends Lazied> = SomeL<L> | None; export type { None, Some, SomeL }; /*********** * Methods * ***********/ /** * Methods for `Option`. */ export namespace Option { /** * [Fn] Check if an {@link Option} is {@link None}. * * Sig: `(o: Option<unknown>) => boolean` */ export type IsNone = IsNoneFn; /** * [Fn] Check if an {@link Option} is {@link Some}. * * Sig: `(o: Option<unknown>) => boolean` */ export type IsSome = IsSomeFn; /** * [Fn] Apply a function to an {@link Option} value if it is {@link Some}, otherwise * return {@link None}. * * Sig: `<T, U>[f: (x: T) => U](o: Option<T>) => Option<U>` */ export type Map<F extends Fn1> = PartialApply<MapFn, [F]>; /** * [Fn] Flatten an {@link Option} of an {@link Option} into an {@link Option}. * * Sig: `<T>(o: Option<Option<T>>) => Option<T>` */ export type Flatten = FlattenFn; /** * [Fn] Get the inner value of an {@link Option} if it is {@link Some}, otherwise return `never`. * * Type safety is **not guaranteed**. * * Sig: `<T>(o: Option<T>) => T` */ export type Unwrap = UnwrapFn; /** * [Fn] Get the inner value of an {@link Option} if it is {@link Some}, otherwise return * `Default`. * * Sig: `<T>[default: T](o: Option<T>) => T` */ export type UnwrapOr<Default> = PartialApply<UnwrapOrFn, [Default]>; } /****************** * Static members * ******************/ /** * [Fn] Wrap a value in a {@link Some}. * * Sig: `<T>(value: T) => Some<T>` * * @example * ```typescript * type R = $<Option$$OfSome, 42>; * // ^?: Some<42> * ``` */ export type Option$$OfSome = OfSomeFn; /**************** * Type classes * ****************/ export type { Option$$HKT$$Builder, Option$$HKT$$Extractor } from "./HKT"; export type { Option$$Applicative } from "./Applicative"; export type { Option$$Functor } from "./Functor"; export type { Option$$Monad } from "./Monad"; export type { Option$$Ord } from "./Ord"; export type { Option$$Show } from "./Show";