=== tests/cases/conformance/expressions/typeGuards/typeGuardsInConditionalExpression.ts === // In the true expression of a conditional expression, // the type of a variable or parameter is narrowed by any type guard in the condition when true, // provided the true expression contains no assignments to the variable or parameter. // In the false expression of a conditional expression, // the type of a variable or parameter is narrowed by any type guard in the condition when false, // provided the false expression contains no assignments to the variable or parameter. function foo(x: number | string) { >foo : (x: string | number) => number, Symbol(foo, Decl(typeGuardsInConditionalExpression.ts, 0, 0)) >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 7, 13)) return typeof x === "string" >typeof x === "string" ? x.length // string : x++ : number >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 7, 13)) >"string" : string ? x.length // string >x.length : number, Symbol(String.length, Decl(lib.d.ts, 414, 19)) >x : string, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 7, 13)) >length : number, Symbol(String.length, Decl(lib.d.ts, 414, 19)) : x++; // number >x++ : number >x : number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 7, 13)) } function foo2(x: number | string) { >foo2 : (x: string | number) => string | number, Symbol(foo2, Decl(typeGuardsInConditionalExpression.ts, 11, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 12, 14)) // x is assigned in the if true branch, the type is not narrowed return typeof x === "string" >typeof x === "string" ? (x = 10 && x)// string | number : x : string | number >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 12, 14)) >"string" : string ? (x = 10 && x)// string | number >(x = 10 && x) : string | number >x = 10 && x : string | number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 12, 14)) >10 && x : string | number >10 : number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 12, 14)) : x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 12, 14)) } function foo3(x: number | string) { >foo3 : (x: string | number) => string | number, Symbol(foo3, Decl(typeGuardsInConditionalExpression.ts, 17, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 18, 14)) // x is assigned in the if false branch, the type is not narrowed // even though assigned using same type as narrowed expression return typeof x === "string" >typeof x === "string" ? (x = "Hello" && x) // string | number : x : string | number >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 18, 14)) >"string" : string ? (x = "Hello" && x) // string | number >(x = "Hello" && x) : string | number >x = "Hello" && x : string | number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 18, 14)) >"Hello" && x : string | number >"Hello" : string >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 18, 14)) : x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 18, 14)) } function foo4(x: number | string) { >foo4 : (x: string | number) => string | number, Symbol(foo4, Decl(typeGuardsInConditionalExpression.ts, 24, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 25, 14)) // false branch updates the variable - so here it is not number // even though assigned using same type as narrowed expression return typeof x === "string" >typeof x === "string" ? x // string | number : (x = 10 && x) : string | number >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 25, 14)) >"string" : string ? x // string | number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 25, 14)) : (x = 10 && x); // string | number >(x = 10 && x) : string | number >x = 10 && x : string | number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 25, 14)) >10 && x : string | number >10 : number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 25, 14)) } function foo5(x: number | string) { >foo5 : (x: string | number) => string | number, Symbol(foo5, Decl(typeGuardsInConditionalExpression.ts, 31, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 32, 14)) // false branch updates the variable - so here it is not number return typeof x === "string" >typeof x === "string" ? x // string | number : (x = "hello" && x) : string | number >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 32, 14)) >"string" : string ? x // string | number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 32, 14)) : (x = "hello" && x); // string | number >(x = "hello" && x) : string | number >x = "hello" && x : string | number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 32, 14)) >"hello" && x : string | number >"hello" : string >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 32, 14)) } function foo6(x: number | string) { >foo6 : (x: string | number) => string | number, Symbol(foo6, Decl(typeGuardsInConditionalExpression.ts, 37, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14)) // Modify in both branches return typeof x === "string" >typeof x === "string" ? (x = 10 && x) // string | number : (x = "hello" && x) : string | number >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14)) >"string" : string ? (x = 10 && x) // string | number >(x = 10 && x) : string | number >x = 10 && x : string | number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14)) >10 && x : string | number >10 : number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14)) : (x = "hello" && x); // string | number >(x = "hello" && x) : string | number >x = "hello" && x : string | number >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14)) >"hello" && x : string | number >"hello" : string >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14)) } function foo7(x: number | string | boolean) { >foo7 : (x: string | number | boolean) => boolean, Symbol(foo7, Decl(typeGuardsInConditionalExpression.ts, 43, 1)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14)) return typeof x === "string" >typeof x === "string" ? x === "hello" // string : typeof x === "boolean" ? x // boolean : x == 10 : boolean >typeof x === "string" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14)) >"string" : string ? x === "hello" // string >x === "hello" : boolean >x : string, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14)) >"hello" : string : typeof x === "boolean" >typeof x === "boolean" ? x // boolean : x == 10 : boolean >typeof x === "boolean" : boolean >typeof x : string >x : number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14)) >"boolean" : string ? x // boolean >x : boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14)) : x == 10; // number >x == 10 : boolean >x : number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14)) >10 : number } function foo8(x: number | string | boolean) { >foo8 : (x: string | number | boolean) => boolean, Symbol(foo8, Decl(typeGuardsInConditionalExpression.ts, 50, 1)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14)) var b: number | boolean; >b : number | boolean, Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 52, 7)) return typeof x === "string" >typeof x === "string" ? x === "hello" : ((b = x) && // number | boolean (typeof x === "boolean" ? x // boolean : x == 10)) : boolean >typeof x === "string" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14)) >"string" : string ? x === "hello" >x === "hello" : boolean >x : string, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14)) >"hello" : string : ((b = x) && // number | boolean >((b = x) && // number | boolean (typeof x === "boolean" ? x // boolean : x == 10)) : boolean >(b = x) && // number | boolean (typeof x === "boolean" ? x // boolean : x == 10) : boolean >(b = x) : number | boolean >b = x : number | boolean >b : number | boolean, Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 52, 7)) >x : number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14)) (typeof x === "boolean" >(typeof x === "boolean" ? x // boolean : x == 10) : boolean >typeof x === "boolean" ? x // boolean : x == 10 : boolean >typeof x === "boolean" : boolean >typeof x : string >x : number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14)) >"boolean" : string ? x // boolean >x : boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14)) : x == 10)); // number >x == 10 : boolean >x : number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14)) >10 : number } function foo9(x: number | string) { >foo9 : (x: string | number) => boolean, Symbol(foo9, Decl(typeGuardsInConditionalExpression.ts, 59, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 60, 14)) var y = 10; >y : number, Symbol(y, Decl(typeGuardsInConditionalExpression.ts, 61, 7)) >10 : number // usage of x or assignment to separate variable shouldn't cause narrowing of type to stop return typeof x === "string" >typeof x === "string" ? ((y = x.length) && x === "hello") // string : x === 10 : boolean >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 60, 14)) >"string" : string ? ((y = x.length) && x === "hello") // string >((y = x.length) && x === "hello") : boolean >(y = x.length) && x === "hello" : boolean >(y = x.length) : number >y = x.length : number >y : number, Symbol(y, Decl(typeGuardsInConditionalExpression.ts, 61, 7)) >x.length : number, Symbol(String.length, Decl(lib.d.ts, 414, 19)) >x : string, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 60, 14)) >length : number, Symbol(String.length, Decl(lib.d.ts, 414, 19)) >x === "hello" : boolean >x : string, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 60, 14)) >"hello" : string : x === 10; // number >x === 10 : boolean >x : number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 60, 14)) >10 : number } function foo10(x: number | string | boolean) { >foo10 : (x: string | number | boolean) => string, Symbol(foo10, Decl(typeGuardsInConditionalExpression.ts, 66, 1)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15)) // Mixing typeguards var b: boolean | number; >b : number | boolean, Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 69, 7)) return typeof x === "string" >typeof x === "string" ? x // string : ((b = x) // x is number | boolean && typeof x === "number" && x.toString()) : string >typeof x === "string" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15)) >"string" : string ? x // string >x : string, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15)) : ((b = x) // x is number | boolean >((b = x) // x is number | boolean && typeof x === "number" && x.toString()) : string >(b = x) // x is number | boolean && typeof x === "number" && x.toString() : string >(b = x) // x is number | boolean && typeof x === "number" : boolean >(b = x) : number | boolean >b = x : number | boolean >b : number | boolean, Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 69, 7)) >x : number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15)) && typeof x === "number" >typeof x === "number" : boolean >typeof x : string >x : number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15)) >"number" : string && x.toString()); // x is number >x.toString() : string >x.toString : (radix?: number) => string, Symbol(Number.toString, Decl(lib.d.ts, 458, 18)) >x : number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15)) >toString : (radix?: number) => string, Symbol(Number.toString, Decl(lib.d.ts, 458, 18)) } function foo11(x: number | string | boolean) { >foo11 : (x: string | number | boolean) => string | number | boolean, Symbol(foo11, Decl(typeGuardsInConditionalExpression.ts, 75, 1)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15)) // Mixing typeguards // Assigning value to x deep inside another guard stops narrowing of type too var b: number | boolean | string; >b : string | number | boolean, Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 79, 7)) return typeof x === "string" >typeof x === "string" ? x // number | boolean | string - changed in the false branch : ((b = x) // x is number | boolean | string - because the assignment changed it && typeof x === "number" && (x = 10) // assignment to x && x) : string | number | boolean >typeof x === "string" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15)) >"string" : string ? x // number | boolean | string - changed in the false branch >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15)) : ((b = x) // x is number | boolean | string - because the assignment changed it >((b = x) // x is number | boolean | string - because the assignment changed it && typeof x === "number" && (x = 10) // assignment to x && x) : string | number | boolean >(b = x) // x is number | boolean | string - because the assignment changed it && typeof x === "number" && (x = 10) // assignment to x && x : string | number | boolean >(b = x) // x is number | boolean | string - because the assignment changed it && typeof x === "number" && (x = 10) : number >(b = x) // x is number | boolean | string - because the assignment changed it && typeof x === "number" : boolean >(b = x) : string | number | boolean >b = x : string | number | boolean >b : string | number | boolean, Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 79, 7)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15)) && typeof x === "number" >typeof x === "number" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15)) >"number" : string && (x = 10) // assignment to x >(x = 10) : number >x = 10 : number >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15)) >10 : number && x); // x is number | boolean | string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15)) } function foo12(x: number | string | boolean) { >foo12 : (x: string | number | boolean) => number, Symbol(foo12, Decl(typeGuardsInConditionalExpression.ts, 86, 1)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15)) // Mixing typeguards // Assigning value to x in outer guard shouldn't stop narrowing in the inner expression var b: number | boolean | string; >b : string | number | boolean, Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 90, 7)) return typeof x === "string" >typeof x === "string" ? (x = 10 && x.toString().length) // number | boolean | string - changed here : ((b = x) // x is number | boolean | string - changed in true branch && typeof x === "number" && x) : number >typeof x === "string" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15)) >"string" : string ? (x = 10 && x.toString().length) // number | boolean | string - changed here >(x = 10 && x.toString().length) : number >x = 10 && x.toString().length : number >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15)) >10 && x.toString().length : number >10 : number >x.toString().length : number, Symbol(String.length, Decl(lib.d.ts, 414, 19)) >x.toString() : string >x.toString : (radix?: number) => string, Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 96, 26)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15)) >toString : (radix?: number) => string, Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 96, 26)) >length : number, Symbol(String.length, Decl(lib.d.ts, 414, 19)) : ((b = x) // x is number | boolean | string - changed in true branch >((b = x) // x is number | boolean | string - changed in true branch && typeof x === "number" && x) : number >(b = x) // x is number | boolean | string - changed in true branch && typeof x === "number" && x : number >(b = x) // x is number | boolean | string - changed in true branch && typeof x === "number" : boolean >(b = x) : string | number | boolean >b = x : string | number | boolean >b : string | number | boolean, Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 90, 7)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15)) && typeof x === "number" >typeof x === "number" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15)) >"number" : string && x); // x is number >x : number, Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15)) }