• 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/typeGuardOfFormTypeOfBoolean.ts ===
20declare function AssertType(value:any, type:string):void;
21class C { private p: string };
22
23let str: string;
24AssertType(str, "string");
25
26let bool: boolean;
27AssertType(bool, "boolean");
28
29let num: number;
30AssertType(num, "number");
31
32let strOrNum: string | number;
33AssertType(strOrNum, "union");
34
35let strOrBool: string | boolean;
36AssertType(strOrBool, "union");
37
38let numOrBool: number | boolean
39AssertType(numOrBool, "union");
40
41let strOrNumOrBool: string | number | boolean;
42AssertType(strOrNumOrBool, "union");
43
44let strOrC: string | C;
45AssertType(strOrC, "union");
46
47let numOrC: number | C;
48AssertType(numOrC, "union");
49
50let boolOrC: boolean | C;
51AssertType(boolOrC, "union");
52
53let c: C;
54AssertType(c, "C");
55
56//	A type guard of the form typeof x === s,
57//  where s is a string literal with the value 'string', 'number', or 'boolean',
58//  - when true, narrows the type of x to the given primitive type, or
59//  - when false, removes the primitive type from the type of x.
60if (typeof strOrBool === "boolean") {
61    bool = strOrBool; // boolean
62AssertType(bool = strOrBool, "boolean");
63AssertType(bool, "boolean");
64AssertType(strOrBool, "boolean");
65}
66else {
67    str = strOrBool; // string
68AssertType(str = strOrBool, "string");
69AssertType(str, "string");
70AssertType(strOrBool, "string");
71}
72if (typeof numOrBool === "boolean") {
73    bool = numOrBool; // boolean
74AssertType(bool = numOrBool, "boolean");
75AssertType(bool, "boolean");
76AssertType(numOrBool, "boolean");
77}
78else {
79    num = numOrBool; // number
80AssertType(num = numOrBool, "number");
81AssertType(num, "number");
82AssertType(numOrBool, "number");
83}
84if (typeof strOrNumOrBool === "boolean") {
85    bool = strOrNumOrBool; // boolean
86AssertType(bool = strOrNumOrBool, "boolean");
87AssertType(bool, "boolean");
88AssertType(strOrNumOrBool, "boolean");
89}
90else {
91    strOrNum = strOrNumOrBool; // string | number
92AssertType(strOrNum = strOrNumOrBool, "union");
93AssertType(strOrNum, "union");
94AssertType(strOrNumOrBool, "union");
95}
96if (typeof boolOrC === "boolean") {
97    bool = boolOrC; // boolean
98AssertType(bool = boolOrC, "boolean");
99AssertType(bool, "boolean");
100AssertType(boolOrC, "boolean");
101}
102else {
103    c = boolOrC; // C
104AssertType(c = boolOrC, "C");
105AssertType(c, "C");
106AssertType(boolOrC, "C");
107}
108
109if (typeof strOrNum === "boolean") {
110    let z1: {} = strOrNum; // {
111AssertType(z1, "{}");
112
113AssertType(strOrNum, "never");
114}
115}
116else {
117    let z2: string | number = strOrNum; // string | number
118AssertType(z2, "union");
119AssertType(strOrNum, "union");
120}
121
122
123// A type guard of the form typeof x !== s, where s is a string literal,
124//  - when true, narrows the type of x by typeof x === s when false, or
125//  - when false, narrows the type of x by typeof x === s when true.
126if (typeof strOrBool !== "boolean") {
127    str = strOrBool; // string
128AssertType(str = strOrBool, "string");
129AssertType(str, "string");
130AssertType(strOrBool, "string");
131}
132else {
133    bool = strOrBool; // boolean
134AssertType(bool = strOrBool, "boolean");
135AssertType(bool, "boolean");
136AssertType(strOrBool, "boolean");
137}
138if (typeof numOrBool !== "boolean") {
139    num = numOrBool; // number
140AssertType(num = numOrBool, "number");
141AssertType(num, "number");
142AssertType(numOrBool, "number");
143}
144else {
145    bool = numOrBool; // boolean
146AssertType(bool = numOrBool, "boolean");
147AssertType(bool, "boolean");
148AssertType(numOrBool, "boolean");
149}
150if (typeof strOrNumOrBool !== "boolean") {
151    strOrNum = strOrNumOrBool; // string | number
152AssertType(strOrNum = strOrNumOrBool, "union");
153AssertType(strOrNum, "union");
154AssertType(strOrNumOrBool, "union");
155}
156else {
157    bool = strOrNumOrBool; // boolean
158AssertType(bool = strOrNumOrBool, "boolean");
159AssertType(bool, "boolean");
160AssertType(strOrNumOrBool, "boolean");
161}
162if (typeof boolOrC !== "boolean") {
163    c = boolOrC; // C
164AssertType(c = boolOrC, "C");
165AssertType(c, "C");
166AssertType(boolOrC, "C");
167}
168else {
169    bool = boolOrC; // boolean
170AssertType(bool = boolOrC, "boolean");
171AssertType(bool, "boolean");
172AssertType(boolOrC, "boolean");
173}
174
175if (typeof strOrNum !== "boolean") {
176    let z1: string | number = strOrNum; // string | number
177AssertType(z1, "union");
178AssertType(strOrNum, "union");
179}
180else {
181    let z2: {} = strOrNum; // {
182AssertType(z2, "{}");
183
184AssertType(strOrNum, "never");
185}
186}
187
188
189