matchigo - v1.0.2
    Preparing search index...

    Variable PConst

    P: {
        any: PAny;
        string: PString;
        number: PNumber;
        boolean: PBoolean;
        bigint: PBigint;
        symbol: PSymbol;
        function: PFunction;
        nullish: PNullish;
        defined: PDefined;
        nonNullable: PDefined;
        array: <Item>(item: Item) => PArray<Item>;
        union: <const Values extends readonly unknown[]>(
            ...values: Values,
        ) => PUnion<Values>;
        when: <T>(fn: (v: T) => boolean) => PWhen<T>;
        not: <Inner>(inner: Inner) => PNot<Inner>;
        optional: <Inner>(inner: Inner) => POptional<Inner>;
        intersection: <const Parts extends readonly unknown[]>(
            ...parts: Parts,
        ) => PIntersection<Parts>;
        instanceOf: <C extends new (...args: never[]) => unknown>(
            ctor: C,
        ) => PInstanceOf<C>;
        regex: (re: RegExp) => PRegex;
        startsWithStr: (s: string) => PStartsWithStr;
        endsWithStr: (s: string) => PEndsWithStr;
        minLengthStr: (n: number) => PMinLengthStr;
        maxLengthStr: (n: number) => PMaxLengthStr;
        lengthStr: (n: number) => PLengthStr;
        includesStr: (s: string) => PIncludesStr;
        between: (min: number, max: number) => PBetween;
        gt: (n: number) => PGt;
        gte: (n: number) => PGte;
        lt: (n: number) => PLt;
        lte: (n: number) => PLte;
        positive: PPositive;
        negative: PNegative;
        integer: PInteger;
        finite: PFinite;
        bigintGt: (n: bigint) => PBigintGt;
        bigintGte: (n: bigint) => PBigintGte;
        bigintLt: (n: bigint) => PBigintLt;
        bigintLte: (n: bigint) => PBigintLte;
        bigintBetween: (min: bigint, max: bigint) => PBigintBetween;
        bigintPositive: PBigintPositive;
        bigintNegative: PBigintNegative;
        tuple: <const Items extends readonly unknown[]>(
            ...items: Items,
        ) => PTuple<Items>;
        startsWith: <const Items extends readonly unknown[]>(
            ...items: Items,
        ) => PStartsWith<Items>;
        endsWith: <const Items extends readonly unknown[]>(
            ...items: Items,
        ) => PEndsWith<Items>;
        arrayOf: <Item>(
            item: Item,
            opts?: { min?: number; max?: number },
        ) => PArrayOf<Item>;
        arrayIncludes: <Item>(item: Item) => PArrayIncludes<Item>;
        map: <K, V>(key: K, value: V) => PMap<K, V>;
        set: <Item>(item: Item) => PSet<Item>;
        select: {
            (): PSelect<undefined, undefined>;
            <L extends string>(label: L): PSelect<L, undefined>;
            <SubPat>(pattern: SubPat): PSelect<undefined, SubPat>;
            <L extends string, SubPat>(label: L, pattern: SubPat): PSelect<L, SubPat>;
        };
    } = ...

    Pattern builders and sentinels. Use these inside a rule's with to match by type, shape, refinement, or predicate. P.* calls allocate — keep patterns hoisted where possible so the compile-once design pays off.

    Type Declaration

    • Readonlyany: PAny
    • Readonlystring: PString
    • Readonlynumber: PNumber
    • Readonlyboolean: PBoolean
    • Readonlybigint: PBigint
    • Readonlysymbol: PSymbol
    • Readonlyfunction: PFunction
    • Readonlynullish: PNullish
    • Readonlydefined: PDefined
    • ReadonlynonNullable: PDefined
    • Readonlyarray: <Item>(item: Item) => PArray<Item>
    • Readonlyunion: <const Values extends readonly unknown[]>(...values: Values) => PUnion<Values>

      Literal union — matches if the value is strictly equal (SameValue equality) to any of the given values. Apply it on a discriminant field to narrow a tagged union: { kind: P.union("a", "b") }.

      { with: P.union("GET", "POST", "PUT"), then: () => "method" }
      
    • Readonlywhen: <T>(fn: (v: T) => boolean) => PWhen<T>

      Arbitrary predicate — escape hatch when P.* doesn't cover your check. Runs on every call; keep the predicate cheap (no allocation, no I/O) and prefer narrower P.* when one applies.

      { with: { age: P.when((n: number) => n >= 18) }, then: () => "adult" }
      
    • Readonlynot: <Inner>(inner: Inner) => PNot<Inner>
    • Readonlyoptional: <Inner>(inner: Inner) => POptional<Inner>
    • Readonlyintersection: <const Parts extends readonly unknown[]>(
          ...parts: Parts,
      ) => PIntersection<Parts>

      Requires every sub-pattern to match the same value. Handy for layering refinements: P.intersection(P.string, P.minLengthStr(3)).

      { with: P.intersection(P.string, P.regex(/^[A-Z]/)), then: () => "capitalized" }
      
    • ReadonlyinstanceOf: <C extends new (...args: never[]) => unknown>(ctor: C) => PInstanceOf<C>
    • Readonlyregex: (re: RegExp) => PRegex
    • ReadonlystartsWithStr: (s: string) => PStartsWithStr
    • ReadonlyendsWithStr: (s: string) => PEndsWithStr
    • ReadonlyminLengthStr: (n: number) => PMinLengthStr
    • ReadonlymaxLengthStr: (n: number) => PMaxLengthStr
    • ReadonlylengthStr: (n: number) => PLengthStr
    • ReadonlyincludesStr: (s: string) => PIncludesStr
    • Readonlybetween: (min: number, max: number) => PBetween
    • Readonlygt: (n: number) => PGt
    • Readonlygte: (n: number) => PGte
    • Readonlylt: (n: number) => PLt
    • Readonlylte: (n: number) => PLte
    • Readonlypositive: PPositive
    • Readonlynegative: PNegative
    • Readonlyinteger: PInteger
    • Readonlyfinite: PFinite
    • ReadonlybigintGt: (n: bigint) => PBigintGt
    • ReadonlybigintGte: (n: bigint) => PBigintGte
    • ReadonlybigintLt: (n: bigint) => PBigintLt
    • ReadonlybigintLte: (n: bigint) => PBigintLte
    • ReadonlybigintBetween: (min: bigint, max: bigint) => PBigintBetween
    • ReadonlybigintPositive: PBigintPositive
    • ReadonlybigintNegative: PBigintNegative
    • Readonlytuple: <const Items extends readonly unknown[]>(...items: Items) => PTuple<Items>
    • ReadonlystartsWith: <const Items extends readonly unknown[]>(...items: Items) => PStartsWith<Items>
    • ReadonlyendsWith: <const Items extends readonly unknown[]>(...items: Items) => PEndsWith<Items>
    • ReadonlyarrayOf: <Item>(item: Item, opts?: { min?: number; max?: number }) => PArrayOf<Item>
    • ReadonlyarrayIncludes: <Item>(item: Item) => PArrayIncludes<Item>
    • Readonlymap: <K, V>(key: K, value: V) => PMap<K, V>
    • Readonlyset: <Item>(item: Item) => PSet<Item>
    • Readonlyselect: {
          (): PSelect<undefined, undefined>;
          <L extends string>(label: L): PSelect<L, undefined>;
          <SubPat>(pattern: SubPat): PSelect<undefined, SubPat>;
          <L extends string, SubPat>(label: L, pattern: SubPat): PSelect<L, SubPat>;
      }

      Extract the matched value into the handler. Overloads:

      • P.select() — extract the value at this position.
      • P.select("label") — labelled extract; handler receives { [label]: ... }.
      • P.select(subPattern) — extract only if subPattern also matches.
      • P.select("label", subPattern) — labelled + refined.

      A lone string arg is treated as a label (ts-pattern convention). To match a literal string and extract it, use P.intersection("literal", P.select()).

      matcher<{ user: { id: number } }, number>()
      .with({ user: { id: P.select() } }, (id) => id)
      .exhaustive();
    import { P, matcher } from "matchigo";

    const kind = matcher<unknown, string>()
    .with(P.string, () => "string")
    .with(P.number, () => "number")
    .with(P.array(P.any), () => "array")
    .with({ kind: P.union("a", "b") }, () => "tagged")
    .otherwise(() => "other");