import * as v from "vitest"; import { MatchField, MatchTag, MatchValue } from "../src/index.ts"; // Type-level tests. These are checked by `tsc` (the root tsconfig includes package test files); // every `@ts-expect-error` below must correspond to a real type error, otherwise tsc fails. // The JSX is never rendered — the function is only defined, not called. type TypeUnion = { type: "foo"; foo: "foo-value" } | { type: "bar"; bar: "bar-value" }; type KindUnion = { kind: "foo"; foo: "foo-value" } | { kind: "bar"; bar: "bar-value" }; type Literal = "foo" | "bar"; const typeCases = ( typeValue: TypeUnion | undefined, kindValue: KindUnion | undefined, literal: Literal | undefined, flag: boolean, numericValue: { t: 1; a: "a" } | { t: 2; b: "b" } | null, ) => [ // --- MatchTag: default `type` tag --- <>{v().foo}, bar: v => <>{v().bar}, }} />, <>{v().foo}, }} />, <>{v().foo}, bar: v => <>{v().bar}, // @ts-expect-error unknown case baz: () => <>, }} />, <>{v().bar}, bar: v => <>{v().bar}, }} />, // --- MatchTag: custom `tag` --- <>{v().foo}, bar: v => <>{v().bar}, }} />, <>{v().foo}, }} />, , , on={kindValue} tag="kind" case={{ foo: v => <>{v().foo}, bar: v => <>{v().bar}, }} />, // @ts-expect-error explicit custom tag key without the runtime `tag` prop on={kindValue} case={{ foo: () => <>, bar: () => <>, }} />, // --- MatchTag: partial --- <>{v().foo}, }} />, <>{v().bar}, }} />, , <>{v().foo}, }} />, <>{v().foo}, }} />, on={kindValue} tag="kind" case={{}} />, // explicit type arguments disable inference of `IsPartial`; `partial` must still work partial on={typeValue} case={{ foo: v => <>{v().foo} }} />, partial on={kindValue} tag="kind" case={{ bar: v => <>{v().bar} }} />, <>, }} />, // With explicit type arguments + `partial`, an error makes both overloads fail, so the // general one also flags `partial` (its `IsPartial` is stuck at `false`) next to the real error. // @ts-expect-error see above partial on={typeValue} case={{ // @ts-expect-error unknown case, even when partial with explicit type arguments baz: () => <>, }} />, <>{v().a}, 2: v => <>{v().b}, }} />, // --- MatchTag: fallback, deprecated alias --- <>{v().foo}, bar: v => <>{v().bar}, }} fallback={<>fallback} />, <>{v().foo}, bar: v => <>{v().bar}, }} />, // --- MatchValue --- <>{v satisfies "foo"}, bar: v => <>{v satisfies "bar"}, }} />, <>, }} />, <>, bar: () => <>, // @ts-expect-error unknown case baz: () => <>, }} />, <> }} />, partial on={literal} case={{ foo: () => <> }} />, <> }} />, <>, bar: () => <> }} fallback={<>fallback} />, ]; v.test("type cases are checked by tsc", () => { v.expect(typeof typeCases).toBe("function"); });