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
Provenance
- Commit
2b7c21e- Tree
00b16bf- Author
aleclarson <alec.stanford.larson@gmail.com>- Committer
aleclarson <alec.stanford.larson@gmail.com>- Parent
b357e54
2 files changed+18 added−15 removed
modified:__tests__/draft.ts+8 added-7 removed
3 unmodified lines4 declare const toDraft: <T>(value: T) => Draft<T>5 declare const fromDraft: <T>(draft: Draft<T>) => T6 7 // prettier-ignore8-type AssertEqual<T, U> = (<G>() => G extends T ? 1 : 0) extends (<G>() => G extends U ? 1 : 0) ? unknown : never8+type 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+ : never11 10-/** Trigger a compiler error when a value is _not_ an exact type. */11-declare 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. */13+declare const exactType: <Actual, Expected>(14+ actual: Actual & Exact<Actual, Expected>,15+ expected: Expected & Exact<Actual, Expected>16+) => Expected17 18 // To remove TS2454 errors.19 declare const _: any20 33 unmodified lines54 {55 let val: ReadonlyArray<ReadonlyArray<[1, 2]>> = _56 let draft: [1, 2][][] = _57 val = exactType(toDraft(val), draft)56- exactType(fromDraft(draft), val)58 }59 60 // Readonly tuple61 {240 unmodified linesmodified:__tests__/produce.ts+10 added-8 removed
5 unmodified lines67891010111213121314151614151617181920212247 unmodified lines707172737274757677787980818283828485868788107 unmodified linesExpand all5 unmodified lines Draft} from "../dist/immer.js"
// prettier-ignoretype AssertEqual<T, U> = (<G>() => G extends T ? 1 : 0) extends (<G>() => G extends U ? 1 : 0) ? unknown : nevertype Exact<A, B> = (<T>() => T extends A ? 1 : 0) extends (<T>() => T extends B ? 1 : 0) ? (A extends B ? (B extends A ? unknown : never) : never) : never
/** Trigger a compiler error when a value is _not_ an exact type. */declare const exactType: <T, U>( draft: T & AssertEqual<T, U>, expected: U & AssertEqual<T, U>) => U/** Fails when `actual` and `expected` have different types. */declare const exactType: <Actual, Expected>( actual: Actual & Exact<Actual, Expected>, expected: Expected & Exact<Actual, Expected>) => Expected
interface State { readonly num: number readonly foo?: string47 unmodified lines
// NOTE: only when the function type is inferredit("can infer state type from default state", () => { type Producer = <T>( base: (T extends number ? T : number) | undefined base: (Draft<T> extends number ? T : number) | undefined ) => number let foo = produce(_ => {}, 1) exactType(foo, {} as Producer) exactType(foo(2), 0 as number)})
it("can infer state type from recipe function", () => { type Base = string | number type Producer = <T>( base: (T extends Base ? T : Base) | undefined, base: (Draft<T> extends Base ? T : Base) | undefined, _2: number ) => Base
let foo = produce((_: string | number, _2: number) => {}, 1)107 unmodified linesExpand all