UNPKG

@altostra/core

Version:

Core library for shared types and logic

23 lines (22 loc) 839 B
import type { Result } from "./Result"; export declare type Option<T> = None<T> | Some<T>; export interface OptionMethods<T> { map: <U>(f: (x: T) => U) => Option<U>; chain: <U>(f: (x: T) => Option<U>) => Option<U>; do: (f: (x: T) => void) => Option<T>; runAsync: <U>(f: (x: T) => Promise<U>) => Promise<U>; get: (defaultValue?: T) => T | undefined; isSome: boolean; isNone: boolean; } export interface Some<T> extends OptionMethods<T> { type: 'option-some'; value: T; } export interface None<T> extends OptionMethods<T> { type: 'option-none'; } export declare const some: <T>(value: T) => Option<T>; export declare function none<T>(): Option<T>; export declare const fromResult: <T, E>(r: Result<T, E>) => Option<T>; export declare const fromNullable: <T>(value: T | null | undefined) => Option<T>;