Skip to content

Commit a57ee29

Browse files
committed
Add tests
1 parent ea1bdff commit a57ee29

4 files changed

Lines changed: 868 additions & 0 deletions

File tree

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
//// [typeGuardIntersectionTypes.ts]
2+
3+
interface X {
4+
x: string;
5+
}
6+
7+
interface Y {
8+
y: string;
9+
}
10+
11+
interface Z {
12+
z: string;
13+
}
14+
15+
declare function isX(obj: any): obj is X;
16+
declare function isY(obj: any): obj is Y;
17+
declare function isZ(obj: any): obj is Z;
18+
19+
function f1(obj: Object) {
20+
if (isX(obj) || isY(obj) || isZ(obj)) {
21+
obj;
22+
}
23+
if (isX(obj) && isY(obj) && isZ(obj)) {
24+
obj;
25+
}
26+
}
27+
28+
// Repro from #8911
29+
30+
// two interfaces
31+
interface A {
32+
a: string;
33+
}
34+
35+
interface B {
36+
b: string;
37+
}
38+
39+
// a type guard for B
40+
function isB(toTest: any): toTest is B {
41+
return toTest && toTest.b;
42+
}
43+
44+
// a function that turns an A into an A & B
45+
function union(a: A): A & B | null {
46+
if (isB(a)) {
47+
return a;
48+
} else {
49+
return null;
50+
}
51+
}
52+
53+
// Repro from #9011
54+
55+
declare function log(s: string): void;
56+
57+
// Supported beast features
58+
interface Beast { wings?: boolean; legs?: number }
59+
interface Legged { legs: number; }
60+
interface Winged { wings: boolean; }
61+
62+
// Beast feature detection via user-defined type guards
63+
function hasLegs(x: Beast): x is Legged { return x && typeof x.legs === 'number'; }
64+
function hasWings(x: Beast): x is Winged { return x && !!x.wings; }
65+
66+
// Function to identify a given beast by detecting its features
67+
function identifyBeast(beast: Beast) {
68+
69+
// All beasts with legs
70+
if (hasLegs(beast)) {
71+
72+
// All winged beasts with legs
73+
if (hasWings(beast)) {
74+
if (beast.legs === 4) { // ERROR TS2339: Property 'legs' does not exist on type 'Winged'.
75+
log(`pegasus - 4 legs, wings`);
76+
}
77+
else if (beast.legs === 2) { // ERROR TS2339...
78+
log(`bird - 2 legs, wings`);
79+
}
80+
else {
81+
log(`unknown - ${beast.legs} legs, wings`); // ERROR TS2339...
82+
}
83+
}
84+
85+
// All non-winged beasts with legs
86+
else {
87+
log(`manbearpig - ${beast.legs} legs, no wings`);
88+
}
89+
}
90+
91+
// All beasts without legs
92+
else {
93+
if (hasWings(beast)) {
94+
log(`quetzalcoatl - no legs, wings`)
95+
}
96+
else {
97+
log(`snake - no legs, no wings`)
98+
}
99+
}
100+
}
101+
102+
function beastFoo(beast: Object) {
103+
if (hasWings(beast) && hasLegs(beast)) {
104+
beast // beast is Legged
105+
// ideally, beast would be Winged && Legged here...
106+
}
107+
else {
108+
beast
109+
}
110+
111+
if (hasLegs(beast) && hasWings(beast)) {
112+
beast // beast is Winged
113+
// ideally, beast would be Legged && Winged here...
114+
}
115+
}
116+
117+
//// [typeGuardIntersectionTypes.js]
118+
function f1(obj) {
119+
if (isX(obj) || isY(obj) || isZ(obj)) {
120+
obj;
121+
}
122+
if (isX(obj) && isY(obj) && isZ(obj)) {
123+
obj;
124+
}
125+
}
126+
// a type guard for B
127+
function isB(toTest) {
128+
return toTest && toTest.b;
129+
}
130+
// a function that turns an A into an A & B
131+
function union(a) {
132+
if (isB(a)) {
133+
return a;
134+
}
135+
else {
136+
return null;
137+
}
138+
}
139+
// Beast feature detection via user-defined type guards
140+
function hasLegs(x) { return x && typeof x.legs === 'number'; }
141+
function hasWings(x) { return x && !!x.wings; }
142+
// Function to identify a given beast by detecting its features
143+
function identifyBeast(beast) {
144+
// All beasts with legs
145+
if (hasLegs(beast)) {
146+
// All winged beasts with legs
147+
if (hasWings(beast)) {
148+
if (beast.legs === 4) {
149+
log("pegasus - 4 legs, wings");
150+
}
151+
else if (beast.legs === 2) {
152+
log("bird - 2 legs, wings");
153+
}
154+
else {
155+
log("unknown - " + beast.legs + " legs, wings"); // ERROR TS2339...
156+
}
157+
}
158+
else {
159+
log("manbearpig - " + beast.legs + " legs, no wings");
160+
}
161+
}
162+
else {
163+
if (hasWings(beast)) {
164+
log("quetzalcoatl - no legs, wings");
165+
}
166+
else {
167+
log("snake - no legs, no wings");
168+
}
169+
}
170+
}
171+
function beastFoo(beast) {
172+
if (hasWings(beast) && hasLegs(beast)) {
173+
beast; // beast is Legged
174+
}
175+
else {
176+
beast;
177+
}
178+
if (hasLegs(beast) && hasWings(beast)) {
179+
beast; // beast is Winged
180+
}
181+
}

0 commit comments

Comments
 (0)