@hazae41/result
Version:
Rust-like Result for TypeScript
350 lines (347 loc) • 11.5 kB
TypeScript
import { Some, None } from '@hazae41/option';
import { Awaitable } from '../../libs/promises/promises.js';
declare namespace Ok {
type Infer<T> = Ok<Inner<T>>;
type Inner<T> = T extends Ok<infer Inner> ? Inner : never;
}
declare class Ok<T = unknown> {
#private;
/**
* A success
* @param inner
*/
constructor(inner: T);
/**
* Create an empty `Ok`
* @returns `Ok(void)`
*/
static void(): Ok<void>;
/**
* Create an `Ok`
* @param inner
* @returns `Ok(inner)`
*/
static new<T>(inner: T): Ok<T>;
get inner(): T;
[Symbol.dispose](this: Ok<Disposable>): void;
[Symbol.asyncDispose](this: Ok<AsyncDisposable>): Promise<void>;
/**
* Type guard for `Ok`
* @returns `true` if `Ok`, `false` if `Err`
*/
isOk(): this is Ok<T>;
/**
* Returns true if the result is `Ok` and the value inside of it matches a predicate
* @param okPredicate
* @returns `true` if `Ok` and `await okPredicate(this.inner)`, `false` otherwise
*/
isOkAnd(okPredicate: (inner: T) => Awaitable<boolean>): Promise<boolean>;
/**
* Returns true if the result is `Ok` and the value inside of it matches a predicate
* @param okPredicate
* @returns `true` if `Ok` and `await okPredicate(this.inner)`, `false` otherwise
*/
isOkAndSync(okPredicate: (inner: T) => boolean): boolean;
/**
* Type guard for `Err`
* @returns `true` if `Err`, `false` if `Ok`
*/
isErr(): false;
/**
* Returns true if the result is `Err` and the value inside of it matches a predicate
* @param errPredicate
* @returns `true` if `Err` and `await errPredicate(this.inner)`, `false` otherwise
*/
isErrAnd(errPredicate: unknown): Promise<false>;
/**
* Returns true if the result is `Err` and the value inside of it matches a predicate
* @param errPredicate
* @returns `true` if `Err` and `await errPredicate(this.inner)`, `false` otherwise
*/
isErrAndSync(errPredicate: unknown): false;
/**
* Compile-time safely get Ok's inner type
* @returns `this.inner`
* @throws if `this` is `Err`
*/
get(): T;
/**
* Compile-time safely get Err's inner type
* @returns `this.inner`
* @throws if `this` is `Ok`
*/
getErr(this: [T] extends [never] ? unknown : never): never;
/**
* Get inner type
* @returns
*/
getAny(): T;
/**
* Transform `Result<T, E>` into `Option<T>`
* @returns `Some(this.inner)` if `Ok`, `None` if `Err`
*/
ok(): Some<T>;
/**
* Transform `Result<T, E>` into `Option<E>`
* @returns `Some(this.inner)` if `Err`, `None` if `Ok`
*/
err(): None;
/**
* Returns an iterator over the possibly contained value
* @yields `this.inner` if `Ok`
*/
[Symbol.iterator](): Iterator<T, void>;
/**
* Transform `Result<T,E>` into `[T,E]`
* @returns `[this.inner, undefined]` if `Ok`, `[undefined, this.inner]` if `Err`
*/
split(): [T, undefined];
/**
* Returns true if the result is an `Ok` value containing the given value
* @param value
* @returns `true` if `Ok` and `this.inner === value`, `false` otherwise
*/
contains(value: T): boolean;
/**
* Returns true if the result is an `Err` value containing the given value
* @param value
* @returns `true` if `Err` and `this.inner === value`, `false` otherwise
*/
containsErr(value: unknown): false;
/**
* Just like `unwrap` but it throws to the closest `Result.unthrow`
* @returns `this.inner` if `Ok`
* @throws `this` if `Err`
* @see Result.unthrow
* @see Result.unthrowSync
*/
throw(thrower: unknown): T;
/**
* Get the inner value if Ok or throw the inner error
* @returns `this.inner` if `Ok`
* @throws `this.inner` if `Err`
*/
getOrThrow(): T;
/**
* Get the inner error if Err or throw the inner value
* @returns `this.inner` if `Err`
* @throws `this.inner` if `Ok`
*/
getErrOrThrow(): never;
/**
* Get the inner value if Ok or null if Err
* @returns `this.inner` if `Ok`, `null` if `Err`
*/
getOrNull(): T;
/**
* Get the inner error if Err or null if Ok
* @returns `this.inner` if `Err`, `null` if `Ok`
*/
getErrOrNull(): null;
/**
* Get the inner value or a default one
* @param value
* @returns `this.inner` if `Ok`, `value` if `Err`
*/
getOr(value: unknown): T;
/**
* Get the inner value or compute a default one from the inner error
* @param errMapper
* @returns `this.inner` if `Ok`, `await errMapper(this.inner)` if `Err`
* @throws if `await errMapper(this.inner)` throws
*/
getOrElse(errMapper: unknown): Promise<T>;
/**
* Get the inner value or compute a default one from the inner error
* @param errMapper
* @returns `this.inner` if `Ok`, `errMapper(this.inner)` if `Err`
* @throws if `errMapper(this.inner)` throws
*/
getOrElseSync(errMapper: unknown): T;
/**
* Get this if Ok or throw the inner error
* @returns
*/
checkOrThrow(): this;
/**
* Get this if Err or throw the inner value
*/
checkErrOrThrow(): never;
/**
* Get this if Ok or return null
* @returns
*/
checkOrNull(): this;
/**
* Get this if Err or return null
*/
checkErrOrNull(): null;
/**
* Transform Result<Promise<T>, E> into Promise<Result<T, E>>
* @returns `await this.inner` if `Ok`, `this` if `Err`
*/
await<T>(this: Ok<PromiseLike<T>>): Promise<Ok<Awaited<T>>>;
/**
* Transform Result<T, Promise<E>> into Promise<Result<T, E>>
* @returns `await this.inner` if `Err`, `this` if `Ok`
*/
awaitErr(): Promise<this>;
/**
* Transform Result<Promise<T>, Promise<E>> into Promise<Result<T, E>>
* @returns `await this.inner`
*/
awaitAll<T>(this: Ok<PromiseLike<T>>): Promise<Ok<Awaited<T>>>;
/**
* Transform `Result<T, E>` into `Result<void, E>`
* @returns `Ok<void>` if `Ok<T>`, `Err<E>` if `E<E>`
*/
clear(): Ok<void>;
/**
* Transform `Result<T, E>` into `Result<T, void>`
* @returns `Ok<T>` if `Ok<T>`, `Err<void>` if `E<E>`
*/
clearErr(): this;
/**
* Calls the given callback with the inner value if `Ok`
* @param okCallback
* @returns `this`
*/
inspect(okCallback: (inner: T) => Awaitable<void>): Promise<this>;
/**
* Calls the given callback with the inner value if `Ok`
* @param okCallback
* @returns `this`
*/
inspectSync(okCallback: (inner: T) => void): this;
/**
* Calls the given callback with the inner value if `Err`
* @param errCallback
* @returns `this`
*/
inspectErr(errCallback: unknown): Promise<this>;
/**
* Calls the given callback with the inner value if `Err`
* @param errCallback
* @returns `this`
*/
inspectErrSync(errCallback: unknown): this;
/**
* Return a new `Ok` but with the given `inner`
* @param inner
* @returns `Ok(inner)` if `Ok`, `this` if `Err`
*/
set<U>(inner: U): Ok<U>;
/**
* Return a new `Err` but with the given `inner`
* @param inner
* @returns `Err(inner)` if `Err`, `this` if `Ok`
*/
setErr(inner: unknown): this;
/**
* Map the inner value into another
* @param okMapper
* @returns `Ok(await okMapper(this.inner))` if `Ok`, `this` if `Err`
* @throws if `await okMapper(this.inner)` throws
*/
map<U>(okMapper: (inner: T) => Awaitable<U>): Promise<Ok<U>>;
/**
* Map the inner value into another
* @param okMapper
* @returns `Ok(okMapper(this.inner))` if `Ok`, `this` if `Err`
* @throws if `okMapper(this.inner)` throws
*/
mapSync<U>(okMapper: (inner: T) => U): Ok<U>;
/**
* Map the inner error into another
* @param errMapper
* @returns `Err(await errMapper(this.inner))` if `Err`, `this` if `Ok`
* @throws if `await errMapper(this.inner)` throws
*/
mapErr(errMapper: unknown): Promise<this>;
/**
* Map the inner error into another
* @param errMapper
* @returns `Err(errMapper(this.inner))` if `Err`, `this` if `Ok`
* @throws if `errMapper(this.inner)` throws
*/
mapErrSync(errMapper: unknown): this;
/**
* Map the inner value into another, or a default one
* @param value
* @param okMapper
* @returns `await okMapper(this.inner)` if `Ok`, `value` if `Err`
* @throws if `await okMapper(this.inner)` throws
*/
mapOr<U>(value: U, okMapper: (inner: T) => Awaitable<U>): Promise<U>;
/**
* Map the inner value into another, or a default one
* @param value
* @param okMapper
* @returns `okMapper(this.inner)` if `Ok`, `value` if `Err`
* @throws if `okMapper(this.inner)` throws
*/
mapOrSync<U>(value: U, okMapper: (inner: T) => U): U;
/**
* Map the inner value into another, or a default one
* @param errMapper
* @param okMapper
* @returns `await okMapper(this.inner)` if `Ok`, `await errMapper(this.inner)` if `Err`
* @throws if `await okMapper(this.inner)` or `await errMapper(this.inner)` throws
*/
mapOrElse<U>(errMapper: unknown, okMapper: (inner: T) => Awaitable<U>): Promise<U>;
/**
* Map the inner value into another, or a default one
* @param errMapper
* @param okMapper
* @returns `okMapper(this.inner)` if `Ok`, `errMapper(this.inner)` if `Err`
* @throws if `okMapper(this.inner)` or `errMapper(this.inner)` throws
*/
mapOrElseSync<U>(errMapper: unknown, okMapper: (inner: T) => U): U;
/**
* Return `value` if `Ok`, return `this` if `Err`
* @param value
* @returns `value` if `Ok`, `this` if `Err`
*/
and<U>(value: U): U;
/**
* Return `await okMapper(this.inner)` if `Ok`, return `this` if `Err`
* @param okMapper
* @returns `await okMapper(this.inner)` if `Ok`, `this` if `Err`
* @throws if `await okMapper(this.inner)` throws
*/
andThen<U>(okMapper: (inner: T) => Awaitable<U>): Promise<U>;
/**
* Return `okMapper(this.inner)` if `Ok`, return `this` if `Err`
* @param okMapper
* @returns `okMapper(this.inner)` if `Ok`, `this` if `Err`
* @throws if `okMapper(this.inner)` throws
*/
andThenSync<U>(okMapper: (inner: T) => U): U;
/**
* Return `value` if `Err`, return `this` if `Ok`
* @param value
* @returns `value` if `Err`, `this` if `Ok`
*/
or(value: unknown): this;
/**
* Return `await errMapper(this.inner)` if `Err`, return `this` if `Ok`
* @param errMapper
* @returns `await errMapper(this.inner)` if `Err`, `this` if `Ok`
* @throws if `await errMapper(this.inner)` throws
*/
orElse(errMapper: unknown): Promise<this>;
/**
* Return `errMapper(this.inner)` if `Err`, return `this` if `Ok`
* @param errMapper
* @returns `errMapper(this.inner)` if `Err`, `this` if `Ok`
* @throws if `errMapper(this.inner)` throws
*/
orElseSync(errMapper: unknown): this;
/**
* Transform Result<Result<T, E1>, E2> into Result<T, E1 | E2>
* @param result
* @returns `this` if `Err`, `this.inner` if `Ok`
*/
flatten(): T;
}
export { Ok };