-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
covariance.ts
87 lines (63 loc) · 1.72 KB
/
covariance.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
80
81
82
83
84
85
86
87
import { expect, test } from 'tstyche';
import {
List,
Map,
MapOf,
OrderedMap,
OrderedSet,
Set,
Stack,
} from 'immutable';
class A {
x: number;
constructor() {
this.x = 1;
}
}
class B extends A {
y: string;
constructor() {
super();
this.y = 'B';
}
}
class C {
z: string;
constructor() {
this.z = 'C';
}
}
test('List covariance', () => {
expect<List<A>>().type.toBeAssignableWith(List<B>());
expect(List([new B()])).type.toBe<List<B>>();
expect<List<C>>().type.not.toBeAssignableWith(List<B>());
});
test('Map covariance', () => {
expect<Map<string, A>>().type.toBeAssignableWith<Map<string, B>>();
expect(Map({ b: new B() })).type.toBe<MapOf<{ b: B }>>();
expect<Map<string, C>>().type.not.toBeAssignableWith<Map<string, B>>();
});
test('Set covariance', () => {
expect<Set<A>>().type.toBeAssignableWith<Set<B>>();
expect(Set([new B()])).type.toBe<Set<B>>();
expect<Set<C>>().type.not.toBeAssignableWith<Set<B>>();
});
test('Stack covariance', () => {
expect<Stack<A>>().type.toBeAssignableWith<Stack<B>>();
expect(Stack([new B()])).type.toBe<Stack<B>>();
expect<Stack<C>>().type.not.toBeAssignableWith<Stack<B>>();
});
test('OrderedMap covariance', () => {
expect<OrderedMap<string, A>>().type.toBeAssignableWith<
OrderedMap<string, B>
>();
expect(OrderedMap({ b: new B() })).type.toBe<OrderedMap<string, B>>();
expect<OrderedMap<string, C>>().type.not.toBeAssignableWith<
OrderedMap<string, B>
>();
});
test('OrderedSet covariance', () => {
expect<OrderedSet<A>>().type.toBeAssignableWith<OrderedSet<B>>();
expect(OrderedSet([new B()])).type.toBe<OrderedSet<B>>();
expect<OrderedSet<C>>().type.not.toBeAssignableWith<OrderedSet<B>>();
});