File: typeGuardIntersectionTypes.types

package info (click to toggle)
node-typescript 3.3.3333-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 324,548 kB
  • sloc: makefile: 6; sh: 3
file content (272 lines) | stat: -rw-r--r-- 6,261 bytes parent folder | download | duplicates (4)
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
=== tests/cases/conformance/expressions/typeGuards/typeGuardIntersectionTypes.ts ===
interface X {
    x: string;
>x : string
}

interface Y {
    y: string;
>y : string
}

interface Z {
    z: string;
>z : string
}

declare function isX(obj: any): obj is X;
>isX : (obj: any) => obj is X
>obj : any

declare function isY(obj: any): obj is Y;
>isY : (obj: any) => obj is Y
>obj : any

declare function isZ(obj: any): obj is Z;
>isZ : (obj: any) => obj is Z
>obj : any

function f1(obj: Object) {
>f1 : (obj: Object) => void
>obj : Object

    if (isX(obj) || isY(obj) || isZ(obj)) {
>isX(obj) || isY(obj) || isZ(obj) : boolean
>isX(obj) || isY(obj) : boolean
>isX(obj) : boolean
>isX : (obj: any) => obj is X
>obj : Object
>isY(obj) : boolean
>isY : (obj: any) => obj is Y
>obj : Object
>isZ(obj) : boolean
>isZ : (obj: any) => obj is Z
>obj : Object

        obj;
>obj : X | Y | Z
    }
    if (isX(obj) && isY(obj) && isZ(obj)) {
>isX(obj) && isY(obj) && isZ(obj) : boolean
>isX(obj) && isY(obj) : boolean
>isX(obj) : boolean
>isX : (obj: any) => obj is X
>obj : Object
>isY(obj) : boolean
>isY : (obj: any) => obj is Y
>obj : X
>isZ(obj) : boolean
>isZ : (obj: any) => obj is Z
>obj : X & Y

        obj;
>obj : X & Y & Z
    }
}

// Repro from #8911

// two interfaces
interface A {
  a: string;
>a : string
}

interface B {
  b: string;
>b : string
}

// a type guard for B
function isB(toTest: any): toTest is B {
>isB : (toTest: any) => toTest is B
>toTest : any

  return toTest && toTest.b;
>toTest && toTest.b : any
>toTest : any
>toTest.b : any
>toTest : any
>b : any
}

// a function that turns an A into an A & B
function union(a: A): A & B | null {
>union : (a: A) => (A & B) | null
>a : A
>null : null

  if (isB(a)) {
>isB(a) : boolean
>isB : (toTest: any) => toTest is B
>a : A

    return a;
>a : A & B

  } else {
    return null;
>null : null
  }
}

// Repro from #9016

declare function log(s: string): void;
>log : (s: string) => void
>s : string

// Supported beast features
interface Beast     { wings?: boolean; legs?: number }
>wings : boolean | undefined
>legs : number | undefined

interface Legged    { legs: number; }
>legs : number

interface Winged    { wings: boolean; }
>wings : boolean

// Beast feature detection via user-defined type guards
function hasLegs(x: Beast): x is Legged { return x && typeof x.legs === 'number'; }
>hasLegs : (x: Beast) => x is Legged
>x : Beast
>x && typeof x.legs === 'number' : boolean
>x : Beast
>typeof x.legs === 'number' : boolean
>typeof x.legs : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x.legs : number | undefined
>x : Beast
>legs : number | undefined
>'number' : "number"

function hasWings(x: Beast): x is Winged { return x && !!x.wings; }
>hasWings : (x: Beast) => x is Winged
>x : Beast
>x && !!x.wings : boolean
>x : Beast
>!!x.wings : boolean
>!x.wings : boolean
>x.wings : boolean | undefined
>x : Beast
>wings : boolean | undefined

// Function to identify a given beast by detecting its features
function identifyBeast(beast: Beast) {
>identifyBeast : (beast: Beast) => void
>beast : Beast

    // All beasts with legs
    if (hasLegs(beast)) {
>hasLegs(beast) : boolean
>hasLegs : (x: Beast) => x is Legged
>beast : Beast

        // All winged beasts with legs
        if (hasWings(beast)) {
>hasWings(beast) : boolean
>hasWings : (x: Beast) => x is Winged
>beast : Legged

            if (beast.legs === 4) {
>beast.legs === 4 : boolean
>beast.legs : number
>beast : Legged & Winged
>legs : number
>4 : 4

                log(`pegasus - 4 legs, wings`);
>log(`pegasus - 4 legs, wings`) : void
>log : (s: string) => void
>`pegasus - 4 legs, wings` : "pegasus - 4 legs, wings"
            }
            else if (beast.legs === 2) {
>beast.legs === 2 : boolean
>beast.legs : number
>beast : Legged & Winged
>legs : number
>2 : 2

                log(`bird - 2 legs, wings`);
>log(`bird - 2 legs, wings`) : void
>log : (s: string) => void
>`bird - 2 legs, wings` : "bird - 2 legs, wings"
            }
            else {
                log(`unknown - ${beast.legs} legs, wings`);
>log(`unknown - ${beast.legs} legs, wings`) : void
>log : (s: string) => void
>`unknown - ${beast.legs} legs, wings` : string
>beast.legs : number
>beast : Legged & Winged
>legs : number
            }
        }

        // All non-winged beasts with legs
        else {
            log(`manbearpig - ${beast.legs} legs, no wings`);
>log(`manbearpig - ${beast.legs} legs, no wings`) : void
>log : (s: string) => void
>`manbearpig - ${beast.legs} legs, no wings` : string
>beast.legs : number
>beast : Legged
>legs : number
        }
    }

    // All beasts without legs    
    else {
        if (hasWings(beast)) {
>hasWings(beast) : boolean
>hasWings : (x: Beast) => x is Winged
>beast : Beast

            log(`quetzalcoatl - no legs, wings`)
>log(`quetzalcoatl - no legs, wings`) : void
>log : (s: string) => void
>`quetzalcoatl - no legs, wings` : "quetzalcoatl - no legs, wings"
        }
        else {
            log(`snake - no legs, no wings`)
>log(`snake - no legs, no wings`) : void
>log : (s: string) => void
>`snake - no legs, no wings` : "snake - no legs, no wings"
        }
    }
}

function beastFoo(beast: Object) {
>beastFoo : (beast: Object) => void
>beast : Object

    if (hasWings(beast) && hasLegs(beast)) {
>hasWings(beast) && hasLegs(beast) : boolean
>hasWings(beast) : boolean
>hasWings : (x: Beast) => x is Winged
>beast : Object
>hasLegs(beast) : boolean
>hasLegs : (x: Beast) => x is Legged
>beast : Winged

        beast;  // Winged & Legged
>beast : Winged & Legged
    }
    else {
        beast;
>beast : Object
    }

    if (hasLegs(beast) && hasWings(beast)) {
>hasLegs(beast) && hasWings(beast) : boolean
>hasLegs(beast) : boolean
>hasLegs : (x: Beast) => x is Legged
>beast : Object
>hasWings(beast) : boolean
>hasWings : (x: Beast) => x is Winged
>beast : Legged

        beast;  // Legged & Winged
>beast : Legged & Winged
    }
}