Library authors test runtime behavior with Vitest or Jest, but type-level contracts need their own gate. The `Equal<A, B>` trick exploits the fact that two function types with internal conditionals are assignable to each other only if those conditionals are identical for every `T` — which forces structural and variance equality, unlike the naive `A extends B ? B extends A ? true : false : false` that bivariantly accepts subtypes. `Expect<T extends true>` then weaponises this: the type only resolves when `T` is `true`, so a failing assertion lights up the file in red. Pair this with `tsc --noEmit` in CI and your `.d.ts` shipped to users is locked.
type Equal<A, B> =(<T>() => T extends A ? 1 : 2) extends(<T>() => T extends B ? 1 : 2)? true: false;type Expect<T extends true> = T;// Test cases — these are compile-time assertions, not runtime checks:type _01 = Expect<Equal<Awaited<Promise<Promise<string>>>, string>>;type _02 = Expect<Equal<Capitalize<'foo'>, 'Foo'>>;type _03 = Expect<Equal<NonNullable<string | null | undefined>, string>>;// Naive equality FAILS to distinguish these — `Equal` succeeds:type Naive<A, B> = A extends B ? (B extends A ? true : false) : false;type N1 = Naive<{ a: 1 }, { a: 1; b?: 2 }>; // true (wrong!)type E1 = Equal<{ a: 1 }, { a: 1; b?: 2 }>; // false (correct)// Would fail to compile — intentional:// type _bad = Expect<Equal<string, 'foo'>>;