@consolidados/results
Version:
Result types, ease and simple
89 lines (87 loc) • 3.88 kB
text/typescript
import { R as Result, O as Option, a as Ok, E as Err, S as Some, N as None } from '../option-B_KKIecf.cjs';
type PrimitiveMembers<T> = Extract<T, PropertyKey>;
type ObjectKeys<T> = T extends object ? keyof T : never;
type ObjectPropertyType<T, K extends PropertyKey> = T extends object ? K extends keyof T ? T[K] : never : never;
type HandlerFor<T, K extends PropertyKey, R> = K extends PrimitiveMembers<T> ? () => R : K extends ObjectKeys<T> ? (value: ObjectPropertyType<T, K>) => R : never;
type MatchCases<T, R, HasDefault extends boolean = false> = (HasDefault extends true ? Partial<{
[K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;
}> : {
[K in PrimitiveMembers<T> | ObjectKeys<T>]: HandlerFor<T, K, R>;
}) & (HasDefault extends true ? {
default: () => R;
} : {});
declare global {
export function match<T, E, ROk, RErr>(matcher: Result<T, E>, cases: {
Ok: (value: T) => ROk;
Err: (error: E) => RErr;
}): ROk | RErr;
export function match<T, RSome, RNone>(matcher: Option<T>, cases: {
Some: (value: T) => RSome;
None: () => RNone;
}): RSome | RNone;
export function match<T extends PropertyKey | object, R>(matcher: T, cases: MatchCases<T, R, true>): R;
export function match<T extends PropertyKey | object, R>(matcher: T, cases: MatchCases<T, R, false>): R;
export function match<T extends {
[K in D]: string | number | symbol;
}, D extends keyof T, R>(matcher: T, cases: {
[K in T[D]]?: (value: Extract<T, {
[P in D]: K;
}>) => R;
} & {
default: (value: T) => R;
}, discriminant: D): R;
export function match<T extends {
[K in D]: string | number | symbol;
}, D extends keyof T, R>(matcher: T, cases: {
[K in T[D]]: (value: Extract<T, {
[P in D]: K;
}>) => R;
}, discriminant: D): R;
export function match<T extends PropertyKey, R>(matcher: T, cases: Partial<Record<T, () => R>> & {
default: () => R;
}): R;
export function match<T extends PropertyKey, R>(matcher: T, cases: Record<T, () => R>): R;
/**
* Creates a new `Ok` instance, representing a successful result.
* @template T The type of the value contained in the `Ok`.
* @param value The value to wrap in the `Ok` instance.
* @returns An `Ok` instance containing the given value.
* @example
* const result = Ok(42);
* console.log(result.isOk()); // true
* console.log(result.unwrap()); // 42
*/
function Ok<T>(value: T): Ok<T>;
/**
* Creates a new `Err` instance, representing a failed result.
* @template E The type of the error contained in the `Err`.
* @param error The error to wrap in the `Err` instance.
* @returns An `Err` instance containing the given error.
* @example
* const result = Err("Something went wrong");
* console.log(result.isErr()); // true
* console.log(result.unwrapErr()); // "Something went wrong"
*/
function Err<E>(error: E): Err<E>;
/**
* creates a new `Some` instance, representing some value.
* @template T the type of the value contained in the `Some`.
* @param value the value to wrap in the `some` instance.
* @returns an `Some` instance containing the given value.
* @example
* const option = Some("some value");
* console.log(option.isSome()); // true
* console.log(option.unwrap()); // "some value"
*/
function Some<T>(value: T): Some<T>;
/**
* creates a new `None` instance, representing no value.
* @param `` There are no paramaters in the `None` instance.
* @returns an `None` instance containing no value.
* @example
* const option = None();
* console.log(option.isNone()); // true
* console.log(option.unwrap()); // throws
*/
function None(): None;
}