=== tests/cases/conformance/expressions/typeGuards/typeGuardsInIfStatement.ts === // In the true branch statement of an �if� statement, // the type of a variable or parameter is narrowed by any type guard in the �if� condition when true, // provided the true branch statement contains no assignments to the variable or parameter. // In the false branch statement of an �if� statement, // the type of a variable or parameter is narrowed by any type guard in the �if� condition when false, // provided the false branch statement contains no assignments to the variable or parameter function foo(x: number | string) { >foo : (x: string | number) => number, Symbol(foo, Decl(typeGuardsInIfStatement.ts, 0, 0)) >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 6, 13)) if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 6, 13)) >"string" : string return x.length; // string >x.length : number, Symbol(String.length, Decl(lib.d.ts, 414, 19)) >x : string, Symbol(x, Decl(typeGuardsInIfStatement.ts, 6, 13)) >length : number, Symbol(String.length, Decl(lib.d.ts, 414, 19)) } else { return x++; // number >x++ : number >x : number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 6, 13)) } } function foo2(x: number | string) { >foo2 : (x: string | number) => string | number, Symbol(foo2, Decl(typeGuardsInIfStatement.ts, 13, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 14, 14)) // x is assigned in the if true branch, the type is not narrowed if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 14, 14)) >"string" : string x = 10; >x = 10 : number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 14, 14)) >10 : number return x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 14, 14)) } else { return x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 14, 14)) } } function foo3(x: number | string) { >foo3 : (x: string | number) => string | number, Symbol(foo3, Decl(typeGuardsInIfStatement.ts, 23, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 24, 14)) // x is assigned in the if true branch, the type is not narrowed if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 24, 14)) >"string" : string x = "Hello"; // even though assigned using same type as narrowed expression >x = "Hello" : string >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 24, 14)) >"Hello" : string return x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 24, 14)) } else { return x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 24, 14)) } } function foo4(x: number | string) { >foo4 : (x: string | number) => string | number, Symbol(foo4, Decl(typeGuardsInIfStatement.ts, 33, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 34, 14)) // false branch updates the variable - so here it is not number if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 34, 14)) >"string" : string return x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 34, 14)) } else { x = 10; // even though assigned number - this should result in x to be string | number >x = 10 : number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 34, 14)) >10 : number return x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 34, 14)) } } function foo5(x: number | string) { >foo5 : (x: string | number) => string | number, Symbol(foo5, Decl(typeGuardsInIfStatement.ts, 43, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 44, 14)) // false branch updates the variable - so here it is not number if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 44, 14)) >"string" : string return x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 44, 14)) } else { x = "hello"; >x = "hello" : string >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 44, 14)) >"hello" : string return x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 44, 14)) } } function foo6(x: number | string) { >foo6 : (x: string | number) => string | number, Symbol(foo6, Decl(typeGuardsInIfStatement.ts, 53, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14)) // Modify in both branches if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14)) >"string" : string x = 10; >x = 10 : number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14)) >10 : number return x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14)) } else { x = "hello"; >x = "hello" : string >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14)) >"hello" : string return x; // string | number >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 54, 14)) } } function foo7(x: number | string | boolean) { >foo7 : (x: string | number | boolean) => boolean, Symbol(foo7, Decl(typeGuardsInIfStatement.ts, 64, 1)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14)) if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14)) >"string" : string return x === "hello"; // string >x === "hello" : boolean >x : string, Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14)) >"hello" : string } else if (typeof x === "boolean") { >typeof x === "boolean" : boolean >typeof x : string >x : number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14)) >"boolean" : string return x; // boolean >x : boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14)) } else { return x == 10; // number >x == 10 : boolean >x : number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 65, 14)) >10 : number } } function foo8(x: number | string | boolean) { >foo8 : (x: string | number | boolean) => boolean, Symbol(foo8, Decl(typeGuardsInIfStatement.ts, 75, 1)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14)) if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14)) >"string" : string return x === "hello"; // string >x === "hello" : boolean >x : string, Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14)) >"hello" : string } else { var b: number | boolean = x; // number | boolean >b : number | boolean, Symbol(b, Decl(typeGuardsInIfStatement.ts, 81, 11)) >x : number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14)) if (typeof x === "boolean") { >typeof x === "boolean" : boolean >typeof x : string >x : number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14)) >"boolean" : string return x; // boolean >x : boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14)) } else { return x == 10; // number >x == 10 : boolean >x : number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 76, 14)) >10 : number } } } function foo9(x: number | string) { >foo9 : (x: string | number) => boolean, Symbol(foo9, Decl(typeGuardsInIfStatement.ts, 89, 1)) >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 90, 14)) var y = 10; >y : number, Symbol(y, Decl(typeGuardsInIfStatement.ts, 91, 7)) >10 : number if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 90, 14)) >"string" : string // usage of x or assignment to separate variable shouldn't cause narrowing of type to stop y = x.length; >y = x.length : number >y : number, Symbol(y, Decl(typeGuardsInIfStatement.ts, 91, 7)) >x.length : number, Symbol(String.length, Decl(lib.d.ts, 414, 19)) >x : string, Symbol(x, Decl(typeGuardsInIfStatement.ts, 90, 14)) >length : number, Symbol(String.length, Decl(lib.d.ts, 414, 19)) return x === "hello"; // string >x === "hello" : boolean >x : string, Symbol(x, Decl(typeGuardsInIfStatement.ts, 90, 14)) >"hello" : string } else { return x == 10; // number >x == 10 : boolean >x : number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 90, 14)) >10 : number } } function foo10(x: number | string | boolean) { >foo10 : (x: string | number | boolean) => boolean, Symbol(foo10, Decl(typeGuardsInIfStatement.ts, 100, 1)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15)) // Mixing typeguard narrowing in if statement with conditional expression typeguard if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15)) >"string" : string return x === "hello"; // string >x === "hello" : boolean >x : string, Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15)) >"hello" : string } else { var y: boolean | string; >y : string | boolean, Symbol(y, Decl(typeGuardsInIfStatement.ts, 107, 11)) var b = x; // number | boolean >b : number | boolean, Symbol(b, Decl(typeGuardsInIfStatement.ts, 108, 11)) >x : number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15)) return typeof x === "number" >typeof x === "number" ? x === 10 // number : x : boolean >typeof x === "number" : boolean >typeof x : string >x : number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15)) >"number" : string ? x === 10 // number >x === 10 : boolean >x : number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15)) >10 : number : x; // x should be boolean >x : boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 101, 15)) } } function foo11(x: number | string | boolean) { >foo11 : (x: string | number | boolean) => string | number | boolean, Symbol(foo11, Decl(typeGuardsInIfStatement.ts, 113, 1)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15)) // Mixing typeguard narrowing in if statement with conditional expression typeguard // Assigning value to x deep inside another guard stops narrowing of type too if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15)) >"string" : string return x; // string | number | boolean - x changed in else branch >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15)) } else { var y: number| boolean | string; >y : string | number | boolean, Symbol(y, Decl(typeGuardsInIfStatement.ts, 121, 11)) var b = x; // number | boolean | string - because below we are changing value of x in if statement >b : string | number | boolean, Symbol(b, Decl(typeGuardsInIfStatement.ts, 122, 11)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15)) return typeof x === "number" >typeof x === "number" ? ( // change value of x x = 10 && x.toString() // number | boolean | string ) : ( // do not change value y = x && x.toString() // number | boolean | string ) : string >typeof x === "number" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15)) >"number" : string ? ( >( // change value of x x = 10 && x.toString() // number | boolean | string ) : string // change value of x x = 10 && x.toString() // number | boolean | string >x = 10 && x.toString() : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15)) >10 && x.toString() : string >10 : number >x.toString() : string >x.toString : () => 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(typeGuardsInIfStatement.ts, 114, 15)) >toString : () => string, Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 96, 26)) ) : ( >( // do not change value y = x && x.toString() // number | boolean | string ) : string // do not change value y = x && x.toString() // number | boolean | string >y = x && x.toString() : string >y : string | number | boolean, Symbol(y, Decl(typeGuardsInIfStatement.ts, 121, 11)) >x && x.toString() : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 114, 15)) >x.toString() : string >x.toString : () => 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(typeGuardsInIfStatement.ts, 114, 15)) >toString : () => string, Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 96, 26)) ); } } function foo12(x: number | string | boolean) { >foo12 : (x: string | number | boolean) => string, Symbol(foo12, Decl(typeGuardsInIfStatement.ts, 133, 1)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15)) // Mixing typeguard narrowing in if statement with conditional expression typeguard // Assigning value to x in outer guard shouldn't stop narrowing in the inner expression if (typeof x === "string") { >typeof x === "string" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15)) >"string" : string return x.toString(); // string | number | boolean - x changed in else branch >x.toString() : string >x.toString : () => 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(typeGuardsInIfStatement.ts, 134, 15)) >toString : () => string, Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 96, 26)) } else { x = 10; >x = 10 : number >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15)) >10 : number var b = x; // number | boolean | string >b : string | number | boolean, Symbol(b, Decl(typeGuardsInIfStatement.ts, 142, 11)) >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15)) return typeof x === "number" >typeof x === "number" ? x.toString() // number : x.toString() : string >typeof x === "number" : boolean >typeof x : string >x : string | number | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15)) >"number" : string ? x.toString() // number >x.toString() : string >x.toString : (radix?: number) => string, Symbol(Number.toString, Decl(lib.d.ts, 458, 18)) >x : number, Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15)) >toString : (radix?: number) => string, Symbol(Number.toString, Decl(lib.d.ts, 458, 18)) : x.toString(); // boolean | string >x.toString() : string >x.toString : () => string, Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 96, 26)) >x : string | boolean, Symbol(x, Decl(typeGuardsInIfStatement.ts, 134, 15)) >toString : () => string, Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 96, 26)) } }