/
deepCopy.ts
79 lines (57 loc) · 2.31 KB
/
deepCopy.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
67
68
69
70
71
72
73
74
75
76
77
78
79
import { List, Map, Record, Set, Seq, DeepCopy, Collection } from 'immutable';
{
// Basic types
// $ExpectType { a: number; b: number; }
type Test = DeepCopy<{ a: number; b: number }>;
}
{
// Iterables
// $ExpectType string[]
type Test = DeepCopy<string[]>;
// $ExpectType number[]
type Keyed = DeepCopy<Collection.Indexed<number>>;
}
{
// Immutable first-level types
// $ExpectType { [x: string]: string; }
type StringKey = DeepCopy<Map<string, string>>;
// should be `{ [x: string]: object; }` but there is an issue with circular references
// $ExpectType { [x: string]: unknown; }
type ObjectKey = DeepCopy<Map<object, object>>;
// should be `{ [x: string]: object; [x: number]: object; }` but there is an issue with circular references
// $ExpectType { [x: string]: unknown; [x: number]: unknown; }
type MixedKey = DeepCopy<Map<object | number, object>>;
// $ExpectType string[]
type ListDeepCopy = DeepCopy<List<string>>;
// $ExpectType string[]
type SetDeepCopy = DeepCopy<Set<string>>;
}
{
// Keyed
// $ExpectType { [x: string]: number; }
type Keyed = DeepCopy<Collection.Keyed<string, number>>;
// $ExpectType { [x: string]: number; [x: number]: number; }
type KeyedMixed = DeepCopy<Collection.Keyed<string | number, number>>;
// $ExpectType { [x: string]: number; [x: number]: number; }
type KeyedSeqMixed = DeepCopy<Seq.Keyed<string | number, number>>;
// $ExpectType { [x: string]: number; [x: number]: number; }
type MapMixed = DeepCopy<Map<string | number, number>>;
}
{
// Nested
// should be `{ map: { [x: string]: string; }; list: string[]; set: string[]; }` but there is an issue with circular references
// $ExpectType { map: unknown; list: unknown; set: unknown; }
type NestedObject = DeepCopy<{ map: Map<string, string>; list: List<string>; set: Set<string>; }>;
// should be `{ map: { [x: string]: string; }; }`, but there is an issue with circular references
// $ExpectType { map: unknown; }
type NestedMap = DeepCopy<Map<'map', Map<string, string>>>;
}
{
// Circular references
type Article = Record<{ title: string; tag: Tag; }>;
type Tag = Record<{ name: string; article: Article; }>;
// should handle circular references here somehow
// $ExpectType { title: string; tag: unknown; }
type Circular = DeepCopy<Article>;
// ^?
}