UNPKG

@mvst/ts-unions

Version:

TypeScript union types for Maybe and RemoteData with pattern matching

42 lines (41 loc) 1.39 kB
export type Maybe<T> = { type: "Nothing"; } | { type: "Just"; value: T; }; type Pattern<T, TResult> = { _?: () => TResult; nothing?: () => TResult; just?: (value: T) => TResult; }; declare function nothing<T>(): Maybe<T>; declare function just<T>(value: T): Maybe<T>; declare function isJust<T>(maybe: Maybe<T>): maybe is { type: "Just"; value: T; }; declare function isNothing<T>(maybe: Maybe<T>): maybe is { type: "Nothing"; }; interface CurriedWithDefault { <T>(defaultValue: T, maybe: Maybe<T>): T; <T>(defaultValue: T): (maybe: Maybe<T>) => T; } declare const withDefault: CurriedWithDefault; interface CurriedWhen { <T, TResult>(pattern: Pattern<T, TResult>, maybe: Maybe<T>): TResult; <T, TResult>(pattern: Pattern<T, TResult>): (maybe: Maybe<T>) => TResult; } declare const when: CurriedWhen; interface CurriedMap { <T, TResult>(fn: (value: T) => TResult, maybe: Maybe<T>): Maybe<TResult>; <T, TResult>(fn: (value: T) => TResult): (maybe: Maybe<T>) => Maybe<TResult>; } declare const map: CurriedMap; interface CurriedAndThen { <T, TResult>(fn: (value: T) => Maybe<TResult>, maybe: Maybe<T>): Maybe<TResult>; <T, TResult>(fn: (value: T) => Maybe<TResult>): (maybe: Maybe<T>) => Maybe<TResult>; } declare const andThen: CurriedAndThen; export { andThen, isJust, isNothing, just, map, nothing, when, withDefault };