@rbxts/rotype
Version:
Advanced runtime type checking library
49 lines (48 loc) • 2.13 kB
TypeScript
import type { Option as OptionType } from "./option";
export interface UnitType {
}
export declare function unit(): UnitType;
export declare class Result<T, E> {
protected readonly okValue: T | undefined;
protected readonly errValue: E | undefined;
private constructor();
static ok<R, E>(val: R): Result<R, E>;
static err<R, E>(val: E): Result<R, E>;
static fromCallback<T>(c: () => T): Result<T, OptionType<defined>>;
static fromVoidCallback(c: () => void): Result<UnitType, OptionType<defined>>;
static fromPromise<T>(p: Promise<T>): Promise<Result<T, OptionType<defined>>>;
static fromVoidPromise(p: Promise<void>): Promise<Result<UnitType, OptionType<defined>>>;
toString(): string;
isOk(): boolean;
isErr(): boolean;
contains(x: T): boolean;
containsErr(x: E): boolean;
okOption(): OptionType<T>;
errOption(): OptionType<E>;
map<U>(func: (item: T) => U): Result<U, E>;
mapOr<U>(def: U, func: (item: T) => U): U;
mapOrElse<U>(def: (item: E) => U, func: (item: T) => U): U;
mapErr<F>(func: (item: E) => F): Result<T, F>;
inspect(func: (item: T) => void): this;
inspectErr(func: (item: E) => void): this;
and<U>(other: Result<U, E>): Result<U, E>;
andWith<U>(func: (item: T) => Result<U, E>): Result<U, E>;
or<F>(other: Result<T, F>): Result<T, F>;
orElse<F>(other: (item: E) => Result<T, F>): Result<T, F>;
expect(msg: unknown): T | never;
unwrap(): T | never;
unwrapOr(def: T): T;
unwrapOrElse(gen: (err: E) => T): T;
expectErr(msg: unknown): E | never;
unwrapErr(): E | never;
transpose<R, E>(this: Result<OptionType<R>, E>): OptionType<Result<R, E>>;
flatten<R, E>(this: Result<Result<R, E>, E>): Result<R, E>;
/**
* Executes one of two callbacks based on the type of the contained value.
* Replacement for Rust's `match` expression.
* @param ifOk Callback executed when this Result contains an Ok value.
* @param ifErr Callback executed when this Result contains an Err value.
*/
match<R>(ifOk: (val: T) => R, ifErr: (err: E) => R): R;
asPtr(): T | E;
}