-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathtypeGuardIntersectionTypes.js
181 lines (159 loc) · 4.13 KB
/
typeGuardIntersectionTypes.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
//// [typeGuardIntersectionTypes.ts]
interface X {
x: string;
}
interface Y {
y: string;
}
interface Z {
z: string;
}
declare function isX(obj: any): obj is X;
declare function isY(obj: any): obj is Y;
declare function isZ(obj: any): obj is Z;
function f1(obj: Object) {
if (isX(obj) || isY(obj) || isZ(obj)) {
obj;
}
if (isX(obj) && isY(obj) && isZ(obj)) {
obj;
}
}
// Repro from #8911
// two interfaces
interface A {
a: string;
}
interface B {
b: string;
}
// a type guard for B
function isB(toTest: any): toTest is B {
return toTest && toTest.b;
}
// a function that turns an A into an A & B
function union(a: A): A & B | null {
if (isB(a)) {
return a;
} else {
return null;
}
}
// Repro from #9011
declare function log(s: string): void;
// Supported beast features
interface Beast { wings?: boolean; legs?: number }
interface Legged { legs: number; }
interface Winged { wings: boolean; }
// Beast feature detection via user-defined type guards
function hasLegs(x: Beast): x is Legged { return x && typeof x.legs === 'number'; }
function hasWings(x: Beast): x is Winged { return x && !!x.wings; }
// Function to identify a given beast by detecting its features
function identifyBeast(beast: Beast) {
// All beasts with legs
if (hasLegs(beast)) {
// All winged beasts with legs
if (hasWings(beast)) {
if (beast.legs === 4) { // ERROR TS2339: Property 'legs' does not exist on type 'Winged'.
log(`pegasus - 4 legs, wings`);
}
else if (beast.legs === 2) { // ERROR TS2339...
log(`bird - 2 legs, wings`);
}
else {
log(`unknown - ${beast.legs} legs, wings`); // ERROR TS2339...
}
}
// All non-winged beasts with legs
else {
log(`manbearpig - ${beast.legs} legs, no wings`);
}
}
// All beasts without legs
else {
if (hasWings(beast)) {
log(`quetzalcoatl - no legs, wings`)
}
else {
log(`snake - no legs, no wings`)
}
}
}
function beastFoo(beast: Object) {
if (hasWings(beast) && hasLegs(beast)) {
beast // beast is Legged
// ideally, beast would be Winged && Legged here...
}
else {
beast
}
if (hasLegs(beast) && hasWings(beast)) {
beast // beast is Winged
// ideally, beast would be Legged && Winged here...
}
}
//// [typeGuardIntersectionTypes.js]
function f1(obj) {
if (isX(obj) || isY(obj) || isZ(obj)) {
obj;
}
if (isX(obj) && isY(obj) && isZ(obj)) {
obj;
}
}
// a type guard for B
function isB(toTest) {
return toTest && toTest.b;
}
// a function that turns an A into an A & B
function union(a) {
if (isB(a)) {
return a;
}
else {
return null;
}
}
// Beast feature detection via user-defined type guards
function hasLegs(x) { return x && typeof x.legs === 'number'; }
function hasWings(x) { return x && !!x.wings; }
// Function to identify a given beast by detecting its features
function identifyBeast(beast) {
// All beasts with legs
if (hasLegs(beast)) {
// All winged beasts with legs
if (hasWings(beast)) {
if (beast.legs === 4) {
log("pegasus - 4 legs, wings");
}
else if (beast.legs === 2) {
log("bird - 2 legs, wings");
}
else {
log("unknown - " + beast.legs + " legs, wings"); // ERROR TS2339...
}
}
else {
log("manbearpig - " + beast.legs + " legs, no wings");
}
}
else {
if (hasWings(beast)) {
log("quetzalcoatl - no legs, wings");
}
else {
log("snake - no legs, no wings");
}
}
}
function beastFoo(beast) {
if (hasWings(beast) && hasLegs(beast)) {
beast; // beast is Legged
}
else {
beast;
}
if (hasLegs(beast) && hasWings(beast)) {
beast; // beast is Winged
}
}