-
-
Notifications
You must be signed in to change notification settings - Fork 842
/
immutable.ts
66 lines (55 loc) · 1.59 KB
/
immutable.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import {Immutable} from "../dist/immer.js"
// prettier-ignore
type 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
/** Fails when `actual` and `expected` have different types. */
declare const exactType: <Actual, Expected>(
actual: Actual & Exact<Actual, Expected>,
expected: Expected & Exact<Actual, Expected>
) => Expected
// array in tuple
{
let val = {} as Immutable<[string[], 1]>
exactType(val, {} as readonly [ReadonlyArray<string>, 1])
}
// tuple in array
{
let val = {} as Immutable<[string, 1][]>
exactType(val, {} as ReadonlyArray<readonly [string, 1]>)
}
// tuple in tuple
{
let val = {} as Immutable<[[string, 1], 1]>
exactType(val, {} as readonly [readonly [string, 1], 1])
}
// array in array
{
let val = {} as Immutable<string[][]>
exactType(val, {} as ReadonlyArray<ReadonlyArray<string>>)
}
// tuple in object
{
let val = {} as Immutable<{a: [string, 1]}>
exactType(val, {} as {readonly a: readonly [string, 1]})
}
// object in tuple
{
let val = {} as Immutable<[{a: string}, 1]>
exactType(val, {} as readonly [{readonly a: string}, 1])
}
// array in object
{
let val = {} as Immutable<{a: string[]}>
exactType(val, {} as {readonly a: ReadonlyArray<string>})
}
// object in array
{
let val = {} as Immutable<Array<{a: string}>>
exactType(val, {} as ReadonlyArray<{readonly a: string}>)
}
// object in object
{
let val = {} as Immutable<{a: {b: string}}>
exactType(val, {} as {readonly a: {readonly b: string}})
}