-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathunionAndIntersectionInference1.js
113 lines (93 loc) · 2.39 KB
/
unionAndIntersectionInference1.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//// [unionAndIntersectionInference1.ts]
// Repro from #2264
interface Y { 'i am a very certain type': Y }
var y: Y = <Y>undefined;
function destructure<a, r>(
something: a | Y,
haveValue: (value: a) => r,
haveY: (value: Y) => r
): r {
return something === y ? haveY(y) : haveValue(<a>something);
}
var value = Math.random() > 0.5 ? 'hey!' : <Y>undefined;
var result = destructure(value, text => 'string', y => 'other one'); // text: string, y: Y
// Repro from #4212
function isVoid<a>(value: void | a): value is void {
return undefined;
}
function isNonVoid<a>(value: void | a) : value is a {
return undefined;
}
function foo1<a>(value: void|a): void {
if (isVoid(value)) {
value; // value is void
} else {
value; // value is a
}
}
function baz1<a>(value: void|a): void {
if (isNonVoid(value)) {
value; // value is a
} else {
value; // value is void
}
}
// Repro from #5417
type Maybe<T> = T | void;
function get<U>(x: U | void): U {
return null; // just an example
}
let foo: Maybe<string>;
get(foo).toUpperCase(); // Ok
// Repro from #5456
interface Man {
walks: boolean;
}
interface Bear {
roars: boolean;
}
interface Pig {
oinks: boolean;
}
declare function pigify<T>(y: T & Bear): T & Pig;
declare var mbp: Man & Bear;
pigify(mbp).oinks; // OK, mbp is treated as Pig
pigify(mbp).walks; // Ok, mbp is treated as Man
//// [unionAndIntersectionInference1.js]
// Repro from #2264
var y = undefined;
function destructure(something, haveValue, haveY) {
return something === y ? haveY(y) : haveValue(something);
}
var value = Math.random() > 0.5 ? 'hey!' : undefined;
var result = destructure(value, function (text) { return 'string'; }, function (y) { return 'other one'; }); // text: string, y: Y
// Repro from #4212
function isVoid(value) {
return undefined;
}
function isNonVoid(value) {
return undefined;
}
function foo1(value) {
if (isVoid(value)) {
value; // value is void
}
else {
value; // value is a
}
}
function baz1(value) {
if (isNonVoid(value)) {
value; // value is a
}
else {
value; // value is void
}
}
function get(x) {
return null; // just an example
}
var foo;
get(foo).toUpperCase(); // Ok
pigify(mbp).oinks; // OK, mbp is treated as Pig
pigify(mbp).walks; // Ok, mbp is treated as Man