|
| 1 | +/** |
| 2 | + * Type-level regression guard for the static `create` / `_create` contract. |
| 3 | + * |
| 4 | + * `yarn types` compiles `src/**` but NOT `test/**` (the sample simulators import |
| 5 | + * generated, gitignored contract artifacts), and `vitest` strips types without |
| 6 | + * checking them — so a regression in the `create` / `_create` typing would |
| 7 | + * otherwise pass CI. This file reproduces the subclass-override pattern against a |
| 8 | + * synthetic contract type, so `yarn types` fails on a regression. |
| 9 | + * |
| 10 | + * It exports nothing and is imported by nothing (inert at runtime). The build |
| 11 | + * config (`tsconfig.json`) excludes `*.type-test.ts` from emit, so it never |
| 12 | + * reaches `dist`; the type check runs it via `tsconfig.types.json`. |
| 13 | + * |
| 14 | + * Typechecking the real test simulators (which need the generated artifacts) is a |
| 15 | + * separate, larger effort; this is the minimal guard for the contract that |
| 16 | + * actually regressed. |
| 17 | + */ |
| 18 | +import { createSimulator } from './factory/createSimulator.js'; |
| 19 | +import type { SimulatorConfig } from './factory/SimulatorConfig.js'; |
| 20 | +import type { IMinimalContract } from './types/Contract.js'; |
| 21 | + |
| 22 | +type GuardPrivateState = { readonly value: number }; |
| 23 | +type GuardArgs = readonly [a: number, b: string]; |
| 24 | + |
| 25 | +// A synthetic config — never executed, only used to instantiate the factory's |
| 26 | +// generics for the type-level checks below. |
| 27 | +const config = {} as unknown as SimulatorConfig< |
| 28 | + GuardPrivateState, |
| 29 | + unknown, |
| 30 | + unknown, |
| 31 | + IMinimalContract, |
| 32 | + GuardArgs |
| 33 | +>; |
| 34 | + |
| 35 | +class Guard extends createSimulator(config) { |
| 36 | + // A concrete `Promise<Guard>` return must stay assignable to the base static |
| 37 | + // side. If `create`'s return goes generic again, this fails the static-side |
| 38 | + // `extends` check (TS2417). |
| 39 | + static async create(a: number, b: string, options = {}): Promise<Guard> { |
| 40 | + // biome-ignore lint/complexity/noThisInStatic: super._create must keep the subclass `this` |
| 41 | + return super._create([a, b], options) as Promise<Guard>; |
| 42 | + } |
| 43 | + |
| 44 | + // `_create` must type its args tuple against `GuardArgs`. If that check |
| 45 | + // regresses (e.g. `_create` widens to `...args: unknown[]`), the wrong-arity |
| 46 | + // call below stops erroring and the `@ts-expect-error` becomes unused → CI red. |
| 47 | + static async _argCheck(): Promise<unknown> { |
| 48 | + // Call via the class name (not `super`) so this negative assertion needs |
| 49 | + // only the `@ts-expect-error` below — no `noThisInStatic` ignore to stack. |
| 50 | + // @ts-expect-error a 1-element tuple must not satisfy `[number, string]`. |
| 51 | + return Guard._create([1], {}); |
| 52 | + } |
| 53 | + |
| 54 | + // A subclass-only member. Without it, `Guard`'s instance type would equal the |
| 55 | + // base `Simulator`'s and the call-site check below would pass even if `create` |
| 56 | + // stopped narrowing to the subclass. This mirrors real subclasses, which add |
| 57 | + // circuit methods that a base-typed return would hide. |
| 58 | + public marker(): string { |
| 59 | + return 'guard'; |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +// The override must narrow the call-site return to the subclass, not widen to |
| 64 | +// the base `Simulator` (which lacks `marker`). |
| 65 | +async function _callSiteSubtype(): Promise<void> { |
| 66 | + const instance: Guard = await Guard.create(1, 'x'); |
| 67 | + void instance.marker(); |
| 68 | +} |
| 69 | + |
| 70 | +void Guard; |
| 71 | +void _callSiteSubtype; |
0 commit comments