Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

TypeScript · 133 lines · 5.7 KiB · at eb4a6ea

// Option and Result are discriminated unions on `tag`, so the match macro's// typed arms (below) can key on the tag and narrow each arm to its variant.interface OptionMethods<T> {  isSome(): this is SomeVariant<T>;  isNone(): this is NoneVariant<T>;  map<U>(f: (value: T) => U): Option<U>;  andThen<U>(f: (value: T) => Option<U>): Option<U>;  unwrapOr(fallback: T): T;  okOr<E>(error: E): Result<T, E>;}export interface SomeVariant<T> extends OptionMethods<T> {  readonly tag: "Some";  readonly value: T;}export interface NoneVariant<T = never> extends OptionMethods<T> {  readonly tag: "None";}export type Option<T> = SomeVariant<T> | NoneVariant<T>;interface ResultMethods<T, E> {  isOk(): this is OkVariant<T, E>;  isErr(): this is ErrVariant<T, E>;  map<U>(f: (value: T) => U): Result<U, E>;  mapErr<F>(f: (error: E) => F): Result<T, F>;  andThen<U>(f: (value: T) => Result<U, E>): Result<U, E>;  unwrapOr(fallback: T): T;  ok(): Option<T>;}export interface OkVariant<T, E = never> extends ResultMethods<T, E> {  readonly tag: "Ok";  readonly value: T;}export interface ErrVariant<T, E> extends ResultMethods<T, E> {  readonly tag: "Err";  readonly error: E;}export type Result<T, E> = OkVariant<T, E> | ErrVariant<T, E>;export declare const Some: <T>(value: T) => Option<T>;export declare const None: NoneVariant<never>;export declare const Ok: <T, E = never>(value: T) => Result<T, E>;export declare const Err: <E, T = never>(error: E) => Result<T, E>;export type Tagged<K extends string, V> = { readonly tag: K; readonly value: V };export declare const tag: <K extends string, V>(t: K, value: V) => Tagged<K, V>;// --- match ---//// Arms are an object keyed by the subject's tag values. A mapped type over// the union's `tag` field makes every variant a required key (that is the// exhaustiveness check), rejects keys the union cannot hold, and types each// arm's parameter as that variant's payload. Works for Option, Result, and// any user union with a string `tag` field.//// The match's type depends on where it sits:// - With a contextual type (a declared return type, an annotated const, an//   argument), R is inferred from that context alone (NoInfer keeps the arms//   out of it). Every arm is contextually typed by R and must be assignable//   to it, so `Err({ kind: "missing" })` infers the declared error union and//   a wrong arm is reported on that arm.// - With no context, R stays unknown and the match's type is the union of//   what the arms return./** The tag values of a tagged union. */export type TagOf<V> = V extends { readonly tag: infer K extends string } ? K : never;/** The member of union V whose tag is K. */export type VariantOf<V, K> = Extract<V, { readonly tag: K }>;/** What an arm binds: `value` if present (Some, Ok, Tagged), else `error` (Err), else the variant itself. */export type Payload<V> = V extends { readonly value: infer P }  ? P  : V extends { readonly error: infer E }    ? E    : V;export type MatchArms<V, R> = { [K in TagOf<V>]: (payload: Payload<VariantOf<V, K>>) => R };type NoExtraArms<V, A> = { [K in Exclude<keyof A, TagOf<V>>]: never };type ArmResult<A> = A[keyof A] extends infer F ? (F extends (...args: never[]) => infer R ? R : never) : never;/** True when no contextual type reached R. */type NoContext<R> = [unknown] extends [R] ? true : false;// Without a context the arms' constraint is `any` rather than `unknown`, so a// match that fails the constraint (the real error) does not also turn into// an `unknown` that errors everywhere it is used.type ArmContext<R> = NoContext<R> extends true ? any : NoInfer<R>;export declare const __tsus_match: <V extends { readonly tag: string }>(  value: V,) => <R, A extends MatchArms<V, ArmContext<R>>>(  arms: A & NoExtraArms<V, A>,) => NoContext<R> extends true ? ArmResult<A> : R;export declare const __tsus_match_or: <V extends { readonly tag: string }>(  value: V,) => <R, A extends Partial<MatchArms<V, ArmContext<R>>>, W extends ArmContext<R>>(  arms: A & NoExtraArms<V, A>,  otherwise: (value: V) => W,) => NoContext<R> extends true ? ArmResult<A> | W : R;// Literal patterns (`"/" => ...`) cannot key an object without colliding// with `_` and losing number/string distinctions, so they lower to a list of// [literal, handler] pairs. Each literal must be a value of the subject's// type; the wildcard is required unless the literals cover a finite union.type Literal = string | number | bigint | boolean;type CaseLiterals<C> = C extends readonly (readonly [infer L, unknown])[] ? L : never;type CaseResult<C> = C extends readonly (readonly [unknown, () => infer R])[] ? R : never;export declare const __tsus_match_lit: <V extends Literal>(  value: V,) => <R, const C extends readonly (readonly [V, () => ArmContext<R>])[], W extends ArmContext<R> = never>(  cases: C,  ...otherwise: [Exclude<V, CaseLiterals<C>>] extends [never] ? [] : [otherwise: (value: V) => W]) => NoContext<R> extends true ? CaseResult<C> | W : R;// --- the ? operator ---// `any` for the same reason as ArmContext: when ? is applied to a non-Result// (TSUS301), the unwrapped value should not cascade into more errors.type Tryable = Result<any, any> | Option<any>;type Bail = { readonly tag: "Err" } | { readonly tag: "None" };export declare const __tsus_try: <V extends Tryable>(value: V) => V;export declare const __tsus_isBail: <V extends Tryable>(value: V) => value is Extract<V, Bail>;/** Re-types an Err/None so it fits any Result/Option with the same error type. */export declare const __tsus_bail: <V>(  value: V,) => V extends { readonly tag: "Err"; readonly error: infer E } ? ErrVariant<never, E> : NoneVariant<never>;export declare const __tsus_unwrap: <V extends Tryable>(value: V) => Payload<Exclude<V, Bail>>;