Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

TypeScript · 121 lines · 5.0 KiB · at 9472e3c

// 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 is the union of// the arms' types./** 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;// The arm results here are `any` only so that a match which fails this// constraint (the real error) does not also report an `unknown` result// everywhere the match is used. A match that type-checks takes its type from// the arms actually written, through ArmResult.export type MatchArms<V> = { [K in TagOf<V>]: (payload: Payload<VariantOf<V, K>>) => any };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;export declare const __tsus_match: <V extends { readonly tag: string }>(  value: V,) => <A extends MatchArms<V>>(arms: A & NoExtraArms<V, A>) => ArmResult<A>;export declare const __tsus_match_or: <V extends { readonly tag: string }>(  value: V,) => <A extends Partial<MatchArms<V>>, W>(  arms: A & NoExtraArms<V, A>,  otherwise: (value: V) => W,) => ArmResult<A> | W;// 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,) => <const C extends readonly (readonly [V, () => unknown])[], W = never>(  cases: C,  ...otherwise: [Exclude<V, CaseLiterals<C>>] extends [never] ? [] : [otherwise: (value: V) => W]) => CaseResult<C> | W;// --- the ? operator ---// `any` for the same reason as MatchArms: 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>>;