• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2* Copyright (c) Microsoft Corporation. All rights reserved.
3* Copyright (c) 2023 Huawei Device Co., Ltd.
4* Licensed under the Apache License, Version 2.0 (the "License");
5* you may not use this file except in compliance with the License.
6* You may obtain a copy of the License at
7*
8*     http://www.apache.org/licenses/LICENSE-2.0
9*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*
16* This file has been modified by Huawei to verify type inference by adding verification statements.
17*/
18
19// === tests/cases/conformance/expressions/typeGuards/typeGuardTypeOfUndefined.ts ===
20declare function AssertType(value:any, type:string):void;
21// undefined type guard adds no new type information
22function test1(a: any) {
23    if (typeof a !== "undefined") {
24AssertType(typeof a !== "undefined", "boolean");
25AssertType(typeof a, "union");
26AssertType(a, "any");
27AssertType("undefined", "string");
28
29        if (typeof a === "boolean") {
30AssertType(typeof a === "boolean", "boolean");
31AssertType(typeof a, "union");
32AssertType(a, "any");
33AssertType("boolean", "string");
34
35            a;
36AssertType(a, "boolean");
37        }
38        else {
39            a;
40AssertType(a, "any");
41        }
42    }
43    else {
44        a;
45AssertType(a, "undefined");
46    }
47}
48
49function test2(a: any) {
50    if (typeof a === "undefined") {
51AssertType(typeof a === "undefined", "boolean");
52AssertType(typeof a, "union");
53AssertType(a, "any");
54AssertType("undefined", "string");
55
56        if (typeof a === "boolean") {
57AssertType(typeof a === "boolean", "boolean");
58AssertType(typeof a, "union");
59AssertType(a, "undefined");
60AssertType("boolean", "string");
61
62            a;
63AssertType(a, "never");
64        }
65        else {
66            a;
67AssertType(a, "undefined");
68        }
69    }
70    else {
71        a;
72AssertType(a, "any");
73    }
74}
75
76function test3(a: any) {
77    if (typeof a === "undefined" || typeof a === "boolean") {
78AssertType(typeof a === "undefined" || typeof a === "boolean", "boolean");
79AssertType(typeof a === "undefined", "boolean");
80AssertType(typeof a, "union");
81AssertType(a, "any");
82AssertType("undefined", "string");
83AssertType(typeof a === "boolean", "boolean");
84AssertType(typeof a, "union");
85AssertType(a, "any");
86AssertType("boolean", "string");
87
88		a;
89AssertType(a, "boolean");
90    }
91    else {
92        a;
93AssertType(a, "any");
94    }
95}
96
97function test4(a: any) {
98    if (typeof a !== "undefined" && typeof a === "boolean") {
99AssertType(typeof a !== "undefined" && typeof a === "boolean", "boolean");
100AssertType(typeof a !== "undefined", "boolean");
101AssertType(typeof a, "union");
102AssertType(a, "any");
103AssertType("undefined", "string");
104AssertType(typeof a === "boolean", "boolean");
105AssertType(typeof a, "union");
106AssertType(a, "any");
107AssertType("boolean", "string");
108
109		a;
110AssertType(a, "boolean");
111    }
112    else {
113        a;
114AssertType(a, "any");
115    }
116}
117
118function test5(a: boolean | void) {
119    if (typeof a !== "undefined") {
120AssertType(typeof a !== "undefined", "boolean");
121AssertType(typeof a, "union");
122AssertType(a, "union");
123AssertType("undefined", "string");
124
125        if (typeof a === "boolean") {
126AssertType(typeof a === "boolean", "boolean");
127AssertType(typeof a, "union");
128AssertType(a, "boolean");
129AssertType("boolean", "string");
130
131            a;
132AssertType(a, "boolean");
133        }
134        else {
135            a;
136AssertType(a, "never");
137        }
138    }
139    else {
140        a;
141AssertType(a, "undefined");
142    }
143}
144
145function test6(a: boolean | void) {
146    if (typeof a === "undefined") {
147AssertType(typeof a === "undefined", "boolean");
148AssertType(typeof a, "union");
149AssertType(a, "union");
150AssertType("undefined", "string");
151
152        if (typeof a === "boolean") {
153AssertType(typeof a === "boolean", "boolean");
154AssertType(typeof a, "union");
155AssertType(a, "undefined");
156AssertType("boolean", "string");
157
158            a;
159AssertType(a, "never");
160        }
161        else {
162            a;
163AssertType(a, "undefined");
164        }
165    }
166    else {
167        a;
168AssertType(a, "boolean");
169    }
170}
171
172function test7(a: boolean | void) {
173    if (typeof a === "undefined" || typeof a === "boolean") {
174AssertType(typeof a === "undefined" || typeof a === "boolean", "boolean");
175AssertType(typeof a === "undefined", "boolean");
176AssertType(typeof a, "union");
177AssertType(a, "union");
178AssertType("undefined", "string");
179AssertType(typeof a === "boolean", "boolean");
180AssertType(typeof a, "union");
181AssertType(a, "boolean");
182AssertType("boolean", "string");
183
184		a;
185AssertType(a, "boolean");
186    }
187    else {
188        a;
189AssertType(a, "never");
190    }
191}
192
193function test8(a: boolean | void) {
194    if (typeof a !== "undefined" && typeof a === "boolean") {
195AssertType(typeof a !== "undefined" && typeof a === "boolean", "boolean");
196AssertType(typeof a !== "undefined", "boolean");
197AssertType(typeof a, "union");
198AssertType(a, "union");
199AssertType("undefined", "string");
200AssertType(typeof a === "boolean", "boolean");
201AssertType(typeof a, "union");
202AssertType(a, "boolean");
203AssertType("boolean", "string");
204
205		a;
206AssertType(a, "boolean");
207    }
208    else {
209        a;
210AssertType(a, "undefined");
211    }
212}
213
214function test9(a: boolean | number) {
215    if (typeof a !== "undefined") {
216AssertType(typeof a !== "undefined", "boolean");
217AssertType(typeof a, "union");
218AssertType(a, "union");
219AssertType("undefined", "string");
220
221        if (typeof a === "boolean") {
222AssertType(typeof a === "boolean", "boolean");
223AssertType(typeof a, "union");
224AssertType(a, "union");
225AssertType("boolean", "string");
226
227            a;
228AssertType(a, "boolean");
229        }
230        else {
231            a;
232AssertType(a, "number");
233        }
234    }
235    else {
236        a;
237AssertType(a, "undefined");
238    }
239}
240
241function test10(a: boolean | number) {
242    if (typeof a === "undefined") {
243AssertType(typeof a === "undefined", "boolean");
244AssertType(typeof a, "union");
245AssertType(a, "union");
246AssertType("undefined", "string");
247
248        if (typeof a === "boolean") {
249AssertType(typeof a === "boolean", "boolean");
250AssertType(typeof a, "union");
251AssertType(a, "undefined");
252AssertType("boolean", "string");
253
254            a;
255AssertType(a, "never");
256        }
257        else {
258            a;
259AssertType(a, "undefined");
260        }
261    }
262    else {
263        a;
264AssertType(a, "union");
265    }
266}
267
268function test11(a: boolean | number) {
269    if (typeof a === "undefined" || typeof a === "boolean") {
270AssertType(typeof a === "undefined" || typeof a === "boolean", "boolean");
271AssertType(typeof a === "undefined", "boolean");
272AssertType(typeof a, "union");
273AssertType(a, "union");
274AssertType("undefined", "string");
275AssertType(typeof a === "boolean", "boolean");
276AssertType(typeof a, "union");
277AssertType(a, "union");
278AssertType("boolean", "string");
279
280		a;
281AssertType(a, "boolean");
282    }
283    else {
284        a;
285AssertType(a, "number");
286    }
287}
288
289function test12(a: boolean | number) {
290    if (typeof a !== "undefined" && typeof a === "boolean") {
291AssertType(typeof a !== "undefined" && typeof a === "boolean", "boolean");
292AssertType(typeof a !== "undefined", "boolean");
293AssertType(typeof a, "union");
294AssertType(a, "union");
295AssertType("undefined", "string");
296AssertType(typeof a === "boolean", "boolean");
297AssertType(typeof a, "union");
298AssertType(a, "union");
299AssertType("boolean", "string");
300
301		a;
302AssertType(a, "boolean");
303    }
304    else {
305        a;
306AssertType(a, "number");
307    }
308}
309
310function test13(a: boolean | number | void) {
311    if (typeof a !== "undefined") {
312AssertType(typeof a !== "undefined", "boolean");
313AssertType(typeof a, "union");
314AssertType(a, "union");
315AssertType("undefined", "string");
316
317        if (typeof a === "boolean") {
318AssertType(typeof a === "boolean", "boolean");
319AssertType(typeof a, "union");
320AssertType(a, "union");
321AssertType("boolean", "string");
322
323            a;
324AssertType(a, "boolean");
325        }
326        else {
327            a;
328AssertType(a, "number");
329        }
330    }
331    else {
332        a;
333AssertType(a, "undefined");
334    }
335}
336
337function test14(a: boolean | number | void) {
338    if (typeof a === "undefined") {
339AssertType(typeof a === "undefined", "boolean");
340AssertType(typeof a, "union");
341AssertType(a, "union");
342AssertType("undefined", "string");
343
344        if (typeof a === "boolean") {
345AssertType(typeof a === "boolean", "boolean");
346AssertType(typeof a, "union");
347AssertType(a, "undefined");
348AssertType("boolean", "string");
349
350            a;
351AssertType(a, "never");
352        }
353        else {
354            a;
355AssertType(a, "undefined");
356        }
357    }
358    else {
359        a;
360AssertType(a, "union");
361    }
362}
363
364function test15(a: boolean | number | void) {
365    if (typeof a === "undefined" || typeof a === "boolean") {
366AssertType(typeof a === "undefined" || typeof a === "boolean", "boolean");
367AssertType(typeof a === "undefined", "boolean");
368AssertType(typeof a, "union");
369AssertType(a, "union");
370AssertType("undefined", "string");
371AssertType(typeof a === "boolean", "boolean");
372AssertType(typeof a, "union");
373AssertType(a, "union");
374AssertType("boolean", "string");
375
376		a;
377AssertType(a, "boolean");
378    }
379    else {
380        a;
381AssertType(a, "number");
382    }
383}
384
385function test16(a: boolean | number | void) {
386    if (typeof a !== "undefined" && typeof a === "boolean") {
387AssertType(typeof a !== "undefined" && typeof a === "boolean", "boolean");
388AssertType(typeof a !== "undefined", "boolean");
389AssertType(typeof a, "union");
390AssertType(a, "union");
391AssertType("undefined", "string");
392AssertType(typeof a === "boolean", "boolean");
393AssertType(typeof a, "union");
394AssertType(a, "union");
395AssertType("boolean", "string");
396
397		a;
398AssertType(a, "boolean");
399    }
400    else {
401        a;
402AssertType(a, "number");
403    }
404}
405
406
407