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
|
=== tests/cases/conformance/expressions/typeGuards/typeGuardsInRightOperandOfAndAndOperator.ts ===
// In the right operand of a && operation,
// the type of a variable or parameter is narrowed by any type guard in the left operand when true.
function foo(x: number | string) {
>foo : (x: number | string) => boolean
>x : string | number
return typeof x === "string" && x.length === 10; // string
>typeof x === "string" && x.length === 10 : boolean
>typeof x === "string" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"
>x.length === 10 : boolean
>x.length : number
>x : string
>length : number
>10 : 10
}
function foo2(x: number | string) {
>foo2 : (x: number | string) => number
>x : string | number
// modify x in right hand operand
return typeof x === "string" && ((x = 10) && x); // string | number
>typeof x === "string" && ((x = 10) && x) : number
>typeof x === "string" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"
>((x = 10) && x) : number
>(x = 10) && x : number
>(x = 10) : 10
>x = 10 : 10
>x : string | number
>10 : 10
>x : number
}
function foo3(x: number | string) {
>foo3 : (x: number | string) => string
>x : string | number
// modify x in right hand operand with string type itself
return typeof x === "string" && ((x = "hello") && x); // string | number
>typeof x === "string" && ((x = "hello") && x) : string
>typeof x === "string" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number
>"string" : "string"
>((x = "hello") && x) : string
>(x = "hello") && x : string
>(x = "hello") : "hello"
>x = "hello" : "hello"
>x : string | number
>"hello" : "hello"
>x : string
}
function foo4(x: number | string | boolean) {
>foo4 : (x: number | string | boolean) => boolean
>x : string | number | boolean
return typeof x !== "string" // string | number | boolean
>typeof x !== "string" // string | number | boolean && typeof x !== "number" // number | boolean && x : boolean
>typeof x !== "string" // string | number | boolean && typeof x !== "number" : boolean
>typeof x !== "string" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number | boolean
>"string" : "string"
&& typeof x !== "number" // number | boolean
>typeof x !== "number" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : number | boolean
>"number" : "number"
&& x; // boolean
>x : boolean
}
function foo5(x: number | string | boolean) {
>foo5 : (x: number | string | boolean) => boolean
>x : string | number | boolean
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
var b: number | boolean;
>b : number | boolean
return typeof x !== "string" // string | number | boolean
>typeof x !== "string" // string | number | boolean && ((b = x) && (typeof x !== "number" // number | boolean && x)) : boolean
>typeof x !== "string" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number | boolean
>"string" : "string"
&& ((b = x) && (typeof x !== "number" // number | boolean
>((b = x) && (typeof x !== "number" // number | boolean && x)) : boolean
>(b = x) && (typeof x !== "number" // number | boolean && x) : boolean
>(b = x) : number | boolean
>b = x : number | boolean
>b : number | boolean
>x : number | boolean
>(typeof x !== "number" // number | boolean && x) : boolean
>typeof x !== "number" // number | boolean && x : boolean
>typeof x !== "number" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : number | true
>"number" : "number"
&& x)); // boolean
>x : true
}
function foo6(x: number | string | boolean) {
>foo6 : (x: number | string | boolean) => boolean
>x : string | number | boolean
// Mixing typeguard narrowing in if statement with conditional expression typeguard
return typeof x !== "string" // string | number | boolean
>typeof x !== "string" // string | number | boolean && (typeof x !== "number" // number | boolean ? x // boolean : x === 10) : boolean
>typeof x !== "string" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number | boolean
>"string" : "string"
&& (typeof x !== "number" // number | boolean
>(typeof x !== "number" // number | boolean ? x // boolean : x === 10) : boolean
>typeof x !== "number" // number | boolean ? x // boolean : x === 10 : boolean
>typeof x !== "number" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : number | boolean
>"number" : "number"
? x // boolean
>x : boolean
: x === 10) // number
>x === 10 : boolean
>x : number
>10 : 10
}
function foo7(x: number | string | boolean) {
>foo7 : (x: number | string | boolean) => string
>x : string | number | boolean
var y: number| boolean | string;
>y : string | number | boolean
var z: number| boolean | string;
>z : string | number | boolean
// Mixing typeguard narrowing
return typeof x !== "string"
>typeof x !== "string" && ((z = x) // number | boolean && (typeof x === "number" // change value of x ? ((x = 10) && x.toString()) // x is number // do not change value : ((y = x) && x.toString()))) : string
>typeof x !== "string" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : string | number | boolean
>"string" : "string"
&& ((z = x) // number | boolean
>((z = x) // number | boolean && (typeof x === "number" // change value of x ? ((x = 10) && x.toString()) // x is number // do not change value : ((y = x) && x.toString()))) : string
>(z = x) // number | boolean && (typeof x === "number" // change value of x ? ((x = 10) && x.toString()) // x is number // do not change value : ((y = x) && x.toString())) : string
>(z = x) : number | boolean
>z = x : number | boolean
>z : string | number | boolean
>x : number | boolean
&& (typeof x === "number"
>(typeof x === "number" // change value of x ? ((x = 10) && x.toString()) // x is number // do not change value : ((y = x) && x.toString())) : string
>typeof x === "number" // change value of x ? ((x = 10) && x.toString()) // x is number // do not change value : ((y = x) && x.toString()) : string
>typeof x === "number" : boolean
>typeof x : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
>x : number | true
>"number" : "number"
// change value of x
? ((x = 10) && x.toString()) // x is number
>((x = 10) && x.toString()) : string
>(x = 10) && x.toString() : string
>(x = 10) : 10
>x = 10 : 10
>x : string | number | boolean
>10 : 10
>x.toString() : string
>x.toString : (radix?: number) => string
>x : number
>toString : (radix?: number) => string
// do not change value
: ((y = x) && x.toString()))); // x is boolean
>((y = x) && x.toString()) : string
>(y = x) && x.toString() : string
>(y = x) : true
>y = x : true
>y : string | number | boolean
>x : true
>x.toString() : string
>x.toString : () => string
>x : true
>toString : () => string
}
|