kojima/types/result.d.ts
2025-10-21 18:30:49 -04:00

256 lines
No EOL
5.8 KiB
TypeScript

/**
* @template T, E
*/
export class Result<T, E> {
/**
* @template T
* @param {T} value
*/
static ok<T_1>(value: T_1): Ok<T_1, any>;
/**
* @template E
* @param {E} error
*/
static err<E_1>(error: E_1): Err<any, any>;
static chain(fn: any, self: any): any;
static map(fn: any, self: any): any;
static alt(other: any, self: any): any;
static equals(other: any, self: any): any;
static lte(other: any, self: any): any;
/**
* @returns {this is Ok<T, E>}
*/
isOk(): this is Ok<T, E>;
/**
* @returns {this is Err<T, E>}
*/
isErr(): this is Err<T, E>;
}
/** @template T, E */
export class Ok<T, E> extends Result<any, any> {
/** @param {T} value */
constructor(value: T);
/**
* @returns {this is Ok<T, E>}
*/
isOk(): this is Ok<T, E>;
/**
* @returns {this is Err<T, E>}
*/
isErr(): this is Err<T, E>;
/**
* @template {Result<T, E>} R
* @param {(value: T) => R} fn
* @returns {R}
*/
andThen<R extends Result<T, E>>(fn: (value: T) => R): R;
/**
* @template V
* @template {Result<V, E>} R
* @param {(value: T) => V} fn
* @returns {R}
*/
map<V, R extends Result<V, E>>(fn: (value: T) => V): R;
/**
* @template V
* @param {V} _defaultValue
* @param {(value: T) => V} fn
* @returns {V}
*/
mapOr<V>(_defaultValue: V, fn: (value: T) => V): V;
/**
* @param {Result<T, E>} other
* @returns {Result<T, E>}
*/
and(other: Result<T, E>): Result<T, E>;
/**
* @param {Result<T, E>} other
* @returns {Result<T, E>}
*/
or(_other: any): Result<T, E>;
/**
* @template F
* @param {(error: E) => Result<T, F>} other
* @returns {Result<T, F>}
*/
orElse<F>(_fn: any): Result<T, F>;
/**
* @param {Result<T, E>} other
* @returns {bool}
*/
equals(other: Result<T, E>): bool;
/**
* @param {Result<T, E>} other
* @returns {bool}
*/
lte(other: Result<T, E>): bool;
/**
* @template [V=T]
* @param {(acc: V, value: T) => V} reducer
* @param {V} init
* @returns {V}
*/
reduce<V = T>(reducer: (acc: V, value: T) => V, init: V): V;
/**
* @template {Result<T, E>} R
* @returns {R}
*/
flatten<R extends Result<T, E>>(): R;
/**
* @returns {Option<T>}
*/
ok(): Option<T>;
/**
* @returns {Option<T>}
*/
err(): Option<T>;
/**
* @param {(value: T) => void} fn
* @returns {this}
*/
inspect(fn: (value: T) => void): this;
/**
* @returns {T}
* @throws {UnwrapError}
*/
unwrap(): T;
/**
* @returns {E}
* @throws {UnwrapError}
*/
unwrapErr(): E;
/**
* @param {T} _value
* @returns {T}
*/
unwrapOr(_value: T): T;
/**
* @param {() => T} _fn
* @returns {T}
*/
unwrapOrElse(_fn: () => T): T;
#private;
}
/** @template T, E */
export class Err<T, E> extends Result<any, any> {
/** @param {E} value */
constructor(error: any);
/**
* @returns {this is Ok<T, E>}
*/
isOk(): this is Ok<T, E>;
/**
* @returns {this is Err<T, E>}
*/
isErr(): this is Err<T, E>;
/**
* @template T
* @template {Result<T, E>} R
* @param {(value: T) => R} fn
* @returns {R}
*/
andThen<T_1, R extends Result<T, E>>(_fn: any): R;
/**
* @template V, E
* @template {Result<V, E>} R
* @param {(value: T) => V} _fn
* @returns {R}
*/
map<V, E_1, R extends Result<V, E>>(_fn: (value: T) => V): R;
/**
* @template V
* @param {V} defaultValue
* @param {(value: T) => V} _fn
* @returns {V}
*/
mapOr<V>(defaultValue: V, _fn: (value: T) => V): V;
/**
* @template V
* @param {() => V} defaultFn
* @param {(value: T) => V} _fn
* @returns {V}
*/
mapOrElse<V>(defaultFn: () => V, _fn: (value: T) => V): V;
/**
* @template T
* @param {Result<T, E>} _other
* @returns {Result<T, E>}
*/
and<T_1>(_other: Result<T_1, E>): Result<T_1, E>;
/**
* @template T
* @param {Result<T, E>} other
* @returns {Result<T, E>}
*/
or<T_1>(other: Result<T_1, E>): Result<T_1, E>;
/**
* @template T, F
* @param {(error: E) => Result<T, F>} other
* @returns {Result<T, F>}
*/
orElse<T_1, F>(fn: any): Result<T_1, F>;
/**
* @template T
* @param {Result<T, E>} other
* @returns {bool}
*/
equals<T_1>(other: Result<T_1, E>): bool;
/**
* @template T
* @param {Result<T, E>} other
* @returns {bool}
*/
lte<T_1>(other: Result<T_1, E>): bool;
/**
* @template [V=T]
* @param {(acc: V, value: T) => V} _reducer
* @param {V} init
* @returns {V}
*/
reduce<V = T>(_reducer: (acc: V, value: T) => V, init: V): V;
/**
* @template T
* @template {Result<T, E>} R
* @returns {R}
*/
flatten<T_1, R extends Result<T, E>>(): R;
/**
* @returns {Option<T>}
*/
ok(): Option<T>;
/**
* @returns {Option<T>}
*/
err(): Option<T>;
/**
* @template T
* @param {(value: T) => void} _fn
* @returns {this}
*/
inspect<T_1>(_fn: (value: T_1) => void): this;
/**
* @template T
* @returns {T}
* @throws {UnwrapError}
*/
unwrap<T_1>(): T_1;
/**
* @returns {E}
* @throws {UnwrapError}
*/
unwrapErr(): E;
/**
* @param {T} value
* @returns {T}
*/
unwrapOr(value: T): T;
/**
* @template T
* @param {() => T} fn
* @returns {T}
*/
unwrapOrElse<T_1>(fn: () => T_1): T_1;
#private;
}
import { Option } from './option.js';
//# sourceMappingURL=result.d.ts.map