import { Either } from './Either.js'; export type MaybePatterns = { Just: (value: T) => U; Nothing: () => U; } | { _: () => U; }; interface AlwaysJust { kind: '$$MaybeAlwaysJust'; } type ExtractMaybe = T extends never ? TDefault : T | TDefault; export interface Maybe { /** Returns true if `this` is `Just`, otherwise it returns false */ isJust(): this is AlwaysJust; /** Returns true if `this` is `Nothing`, otherwise it returns false */ isNothing(): this is Nothing; inspect(): string; toString(): string; toJSON(): T; /** Compares the values inside `this` and the argument, returns true if both are Nothing or if the values are equal */ equals(other: Maybe): boolean; /** Transforms the value inside `this` with a given function. Returns `Nothing` if `this` is `Nothing` */ map(f: (value: T) => U): Maybe; /** Maps `this` with a `Maybe` function */ ap(maybeF: Maybe<(value: T) => U>): Maybe; /** Returns the first `Just` between `this` and another `Maybe` or `Nothing` if both `this` and the argument are `Nothing` */ alt(other: Maybe): Maybe; /** Lazy version of `alt` */ altLazy(other: () => Maybe): Maybe; /** Transforms `this` with a function that returns a `Maybe`. Useful for chaining many computations that may result in a missing value */ chain(f: (value: T) => Maybe): Maybe; /** Transforms `this` with a function that returns a nullable value. Equivalent to `m.chain(x => Maybe.fromNullable(f(x)))` */ chainNullable(f: (value: T) => U | undefined | null | void): Maybe; /** Flattens nested Maybes. `m.join()` is equivalent to `m.chain(x => x)` */ join(this: Maybe>): Maybe; /** Takes a reducer and an initial value and returns the initial value if `this` is `Nothing` or the result of applying the function to the initial value and the value inside `this` */ reduce(reducer: (accumulator: U, value: T) => U, initialValue: U): U; /** Returns `this` if it's `Nothing`, otherwise it returns the result of applying the function argument to `this` and wrapping it in a `Just` */ extend(f: (value: Maybe) => U): Maybe; /** Returns the value inside `this` or throws an error if `this` is `Nothing` */ unsafeCoerce(): T; /** Structural pattern matching for `Maybe` in the form of a function */ caseOf(patterns: MaybePatterns): U; /** Returns the default value if `this` is `Nothing`, otherwise it return the value inside `this` */ orDefault(defaultValue: T): T; /** Lazy version of `orDefault`. Takes a function that returns the default value, that function will be called only if `this` is `Nothing` */ orDefaultLazy(getDefaultValue: () => T): T; /** Returns empty list if the `Maybe` is `Nothing` or a list where the only element is the value of `Just` */ toList(): T[]; /** Maps over `this` and returns the resulting value or returns the default value if `this` is `Nothing` */ mapOrDefault(f: (value: T) => U, defaultValue: U): U; /** Returns the value inside `this` or undefined if `this` is `Nothing`. Use `extractNullable` if you need a null returned instead */ extract(): this extends AlwaysJust ? T : ExtractMaybe; /** Returns the value inside `this` or null if `this` is `Nothing`. Use `extract` if you need an undefined returned instead */ extractNullable(): this extends AlwaysJust ? T : ExtractMaybe; /** Constructs a `Right` from a `Just` or a `Left` with a provided left value if `this` is `Nothing` */ toEither(left: L): Either; /** Runs an effect if `this` is `Just`, returns `this` to make chaining other methods possible */ ifJust(effect: (value: T) => any): this; /** Runs an effect if `this` is `Nothing`, returns `this` to make chaining other methods possible */ ifNothing(effect: () => any): this; /** Takes a predicate function and returns `this` if the predicate returns true or Nothing if it returns false */ filter(pred: (value: T) => value is U): Maybe; /** Takes a predicate function and returns `this` if the predicate returns true or Nothing if it returns false */ filter(pred: (value: T) => boolean): Maybe; 'fantasy-land/equals'(other: Maybe): boolean; 'fantasy-land/map'(f: (value: T) => U): Maybe; 'fantasy-land/ap'(maybeF: Maybe<(value: T) => U>): Maybe; 'fantasy-land/alt'(other: Maybe): Maybe; 'fantasy-land/chain'(f: (value: T) => Maybe): Maybe; 'fantasy-land/reduce'(reducer: (accumulator: U, value: T) => U, initialValue: U): U; 'fantasy-land/extend'(f: (value: Maybe) => U): Maybe; 'fantasy-land/filter'(pred: (value: T) => boolean): Maybe; 'fantasy-land/filter'(pred: (value: T) => boolean): Maybe; } interface MaybeTypeRef { /** Takes a value and wraps it in a `Just` */ of(value: T): Maybe; /** Returns `Nothing` */ empty(): Nothing; /** Returns `Nothing` */ zero(): Nothing; /** Takes a value and returns `Nothing` if the value is null or undefined, otherwise a `Just` is returned */ fromNullable(value: T | undefined | null | void): Maybe; /** Takes a value and returns Nothing if the value is falsy, otherwise a Just is returned */ fromFalsy(value: T | undefined | null | void): Maybe; /** Takes a predicate and a value, passes the value to the predicate and returns a Just if it returns true, otherwise a Nothing is returned */ fromPredicate(pred: (value: T) => boolean): (value: T) => Maybe; fromPredicate(pred: (value: T) => boolean, value: T): Maybe; fromPredicate(pred: (value: T) => value is P, value: T): Maybe

; fromPredicate(pred: (value: T) => value is P): (value: T) => Maybe

; /** Returns only the `Just` values in a list */ catMaybes(list: readonly Maybe[]): T[]; /** Maps over a list of values and returns a list of all resulting `Just` values */ mapMaybe(f: (value: T) => Maybe): (list: readonly T[]) => U[]; mapMaybe(f: (value: T) => Maybe, list: readonly T[]): U[]; /** Calls a function that may throw and wraps the result in a `Just` if successful or `Nothing` if an error is caught */ encase(thunk: () => T): Maybe; isMaybe(x: unknown): x is Maybe; /** Turns a list of `Maybe`s into an `Maybe` of list if all items are `Just` */ sequence(maybes: readonly Maybe[]): Maybe; 'fantasy-land/of'(value: T): Maybe; 'fantasy-land/empty'(): Nothing; 'fantasy-land/zero'(): Nothing; } export declare const Maybe: MaybeTypeRef; declare class Nothing implements Maybe { private __value; isJust(): this is AlwaysJust; isNothing(): this is Nothing; inspect(): string; toString(): string; toJSON(): never; equals(other: Maybe): boolean; map(_: (value: never) => U): Maybe; ap(_: Maybe<(value: never) => U>): Maybe; alt(other: Maybe): Maybe; altLazy(other: () => Maybe): Maybe; chain(_: (value: never) => Maybe): Maybe; chainNullable(_: (value: never) => U | undefined | null | void): Maybe; join(this: Maybe>): Maybe; reduce(_: (accumulator: U, value: never) => U, initialValue: U): U; extend(_: (value: Maybe) => U): Maybe; unsafeCoerce(): T; caseOf(patterns: MaybePatterns): U; orDefault(defaultValue: T): T; orDefaultLazy(getDefaultValue: () => T): T; toList(): T[]; mapOrDefault(_: (value: never) => U, defaultValue: U): U; extract(): this extends AlwaysJust ? never : ExtractMaybe; extractNullable(): this extends AlwaysJust ? never : ExtractMaybe; toEither(left: L): Either; ifJust(_: (value: never) => any): this; ifNothing(effect: () => any): this; filter(_: (value: never) => boolean): Maybe; 'fantasy-land/equals': typeof this.equals; 'fantasy-land/map': typeof this.map; 'fantasy-land/ap': typeof this.ap; 'fantasy-land/alt': typeof this.alt; 'fantasy-land/chain': typeof this.chain; 'fantasy-land/reduce': typeof this.reduce; 'fantasy-land/extend': typeof this.extend; 'fantasy-land/filter': typeof this.filter; } /** Constructs a Just. Represents an optional value that exists */ declare const just: (value: T) => Maybe; /** Represents a missing value, you can think of it as a smart 'null' */ declare const nothing: Nothing; export { just as Just, nothing as Nothing };