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
|
=== tests/cases/conformance/expressions/typeGuards/typeGuardOfFormTypeOfBoolean.ts ===
class C { private p: string };
>C : C
>p : string
var str: string;
>str : string
var bool: boolean;
>bool : boolean
var num: number;
>num : number
var strOrNum: string | number;
>strOrNum : string | number
var strOrBool: string | boolean;
>strOrBool : string | boolean
var numOrBool: number | boolean
>numOrBool : number | boolean
var strOrNumOrBool: string | number | boolean;
>strOrNumOrBool : string | number | boolean
var strOrC: string | C;
>strOrC : string | C
var numOrC: number | C;
>numOrC : number | C
var boolOrC: boolean | C;
>boolOrC : boolean | C
var c: C;
>c : C
// A type guard of the form typeof x === s,
// where s is a string literal with the value 'string', 'number', or 'boolean',
// - when true, narrows the type of x to the given primitive type, or
// - when false, removes the primitive type from the type of x.
if (typeof strOrBool === "boolean") {
>typeof strOrBool === "boolean" : boolean
>typeof strOrBool : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>strOrBool : string | boolean
>"boolean" : "boolean"
bool = strOrBool; // boolean
>bool = strOrBool : boolean
>bool : boolean
>strOrBool : boolean
}
else {
str = strOrBool; // string
>str = strOrBool : string
>str : string
>strOrBool : string
}
if (typeof numOrBool === "boolean") {
>typeof numOrBool === "boolean" : boolean
>typeof numOrBool : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>numOrBool : number | boolean
>"boolean" : "boolean"
bool = numOrBool; // boolean
>bool = numOrBool : boolean
>bool : boolean
>numOrBool : boolean
}
else {
num = numOrBool; // number
>num = numOrBool : number
>num : number
>numOrBool : number
}
if (typeof strOrNumOrBool === "boolean") {
>typeof strOrNumOrBool === "boolean" : boolean
>typeof strOrNumOrBool : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>strOrNumOrBool : string | number | boolean
>"boolean" : "boolean"
bool = strOrNumOrBool; // boolean
>bool = strOrNumOrBool : boolean
>bool : boolean
>strOrNumOrBool : boolean
}
else {
strOrNum = strOrNumOrBool; // string | number
>strOrNum = strOrNumOrBool : string | number
>strOrNum : string | number
>strOrNumOrBool : string | number
}
if (typeof boolOrC === "boolean") {
>typeof boolOrC === "boolean" : boolean
>typeof boolOrC : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>boolOrC : boolean | C
>"boolean" : "boolean"
bool = boolOrC; // boolean
>bool = boolOrC : boolean
>bool : boolean
>boolOrC : boolean
}
else {
c = boolOrC; // C
>c = boolOrC : C
>c : C
>boolOrC : C
}
if (typeof strOrNum === "boolean") {
>typeof strOrNum === "boolean" : boolean
>typeof strOrNum : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>strOrNum : string | number
>"boolean" : "boolean"
let z1: {} = strOrNum; // {}
>z1 : {}
>strOrNum : never
}
else {
let z2: string | number = strOrNum; // string | number
>z2 : string | number
>strOrNum : string | number
}
// A type guard of the form typeof x !== s, where s is a string literal,
// - when true, narrows the type of x by typeof x === s when false, or
// - when false, narrows the type of x by typeof x === s when true.
if (typeof strOrBool !== "boolean") {
>typeof strOrBool !== "boolean" : boolean
>typeof strOrBool : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>strOrBool : string | boolean
>"boolean" : "boolean"
str = strOrBool; // string
>str = strOrBool : string
>str : string
>strOrBool : string
}
else {
bool = strOrBool; // boolean
>bool = strOrBool : boolean
>bool : boolean
>strOrBool : boolean
}
if (typeof numOrBool !== "boolean") {
>typeof numOrBool !== "boolean" : boolean
>typeof numOrBool : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>numOrBool : number | boolean
>"boolean" : "boolean"
num = numOrBool; // number
>num = numOrBool : number
>num : number
>numOrBool : number
}
else {
bool = numOrBool; // boolean
>bool = numOrBool : boolean
>bool : boolean
>numOrBool : boolean
}
if (typeof strOrNumOrBool !== "boolean") {
>typeof strOrNumOrBool !== "boolean" : boolean
>typeof strOrNumOrBool : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>strOrNumOrBool : string | number | boolean
>"boolean" : "boolean"
strOrNum = strOrNumOrBool; // string | number
>strOrNum = strOrNumOrBool : string | number
>strOrNum : string | number
>strOrNumOrBool : string | number
}
else {
bool = strOrNumOrBool; // boolean
>bool = strOrNumOrBool : boolean
>bool : boolean
>strOrNumOrBool : boolean
}
if (typeof boolOrC !== "boolean") {
>typeof boolOrC !== "boolean" : boolean
>typeof boolOrC : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>boolOrC : boolean | C
>"boolean" : "boolean"
c = boolOrC; // C
>c = boolOrC : C
>c : C
>boolOrC : C
}
else {
bool = boolOrC; // boolean
>bool = boolOrC : boolean
>bool : boolean
>boolOrC : boolean
}
if (typeof strOrNum !== "boolean") {
>typeof strOrNum !== "boolean" : boolean
>typeof strOrNum : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>strOrNum : string | number
>"boolean" : "boolean"
let z1: string | number = strOrNum; // string | number
>z1 : string | number
>strOrNum : string | number
}
else {
let z2: {} = strOrNum; // {}
>z2 : {}
>strOrNum : never
}
|