Skip to content
Myr

test(ts): improved exact type detection

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by aleclarson · committed by aleclarson · parent b357e54 · Browse files · 2 files+18 added−15 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+18 added−15 removed

  • tests:+18 added−15 removed

2 files+18 added−15 removed

modified:__tests__/draft.ts+8 added-7 removed
3 unmodified lines
4declare const toDraft: <T>(value: T) => Draft<T>5declare const fromDraft: <T>(draft: Draft<T>) => T67// prettier-ignore8type AssertEqual<T, U> = (<G>() => G extends T ? 1 : 0) extends (<G>() => G extends U ? 1 : 0) ? unknown : never8type Exact<A, B> = (<T>() => T extends A ? 1 : 0) extends (<T>() => T extends B ? 1 : 0)9 ? (A extends B ? (B extends A ? unknown : never) : never)10 : never1110/** Trigger a compiler error when a value is _not_ an exact type. */11declare const exactType: <T, U>(12 draft: T & AssertEqual<T, U>,13 expected: U & AssertEqual<T, U>14) => U12/** Fails when `actual` and `expected` have different types. */13declare const exactType: <Actual, Expected>(14 actual: Actual & Exact<Actual, Expected>,15 expected: Expected & Exact<Actual, Expected>16) => Expected1718// To remove TS2454 errors.19declare const _: any20
33 unmodified lines
54{55 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _56 let draft: [1, 2][][] = _57 val = exactType(toDraft(val), draft)56 exactType(fromDraft(draft), val)58}5960// Readonly tuple61{
240 unmodified lines
modified:__tests__/produce.ts+10 added-8 removed