TypeScript/tests/baselines/reference/typeGuardsInRightOperandOfAndAndOperator.symbols
2015-04-15 16:44:20 -07:00

144 lines
7.1 KiB
Plaintext

=== 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,
// provided the right operand contains no assignments to the variable or parameter.
function foo(x: number | string) {
>foo : Symbol(foo, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 0, 0))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 3, 13))
return typeof x === "string" && x.length === 10; // string
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 3, 13))
>x.length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 3, 13))
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
}
function foo2(x: number | string) {
>foo2 : Symbol(foo2, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 5, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 6, 14))
// modify x in right hand operand
return typeof x === "string" && ((x = 10) && x); // string | number
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 6, 14))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 6, 14))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 6, 14))
}
function foo3(x: number | string) {
>foo3 : Symbol(foo3, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 9, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 10, 14))
// modify x in right hand operand with string type itself
return typeof x === "string" && ((x = "hello") && x); // string | number
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 10, 14))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 10, 14))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 10, 14))
}
function foo4(x: number | string | boolean) {
>foo4 : Symbol(foo4, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 13, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 14, 14))
return typeof x !== "string" // string | number | boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 14, 14))
&& typeof x !== "number" // number | boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 14, 14))
&& x; // boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 14, 14))
}
function foo5(x: number | string | boolean) {
>foo5 : Symbol(foo5, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 18, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 19, 14))
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
var b: number | boolean;
>b : Symbol(b, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 21, 7))
return typeof x !== "string" // string | number | boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 19, 14))
&& ((b = x) && (typeof x !== "number" // number | boolean
>b : Symbol(b, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 21, 7))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 19, 14))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 19, 14))
&& x)); // boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 19, 14))
}
function foo6(x: number | string | boolean) {
>foo6 : Symbol(foo6, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 25, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 26, 14))
// Mixing typeguard narrowing in if statement with conditional expression typeguard
return typeof x !== "string" // string | number | boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 26, 14))
&& (typeof x !== "number" // number | boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 26, 14))
? x // boolean
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 26, 14))
: x === 10) // number
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 26, 14))
}
function foo7(x: number | string | boolean) {
>foo7 : Symbol(foo7, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 32, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 33, 14))
var y: number| boolean | string;
>y : Symbol(y, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 34, 7))
var z: number| boolean | string;
>z : Symbol(z, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 35, 7))
// Mixing typeguard narrowing
// Assigning value to x deep inside another guard stops narrowing of type too
return typeof x !== "string"
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 33, 14))
&& ((z = x) // string | number | boolean - x changed deeper in conditional expression
>z : Symbol(z, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 35, 7))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 33, 14))
&& (typeof x === "number"
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 33, 14))
// change value of x
? (x = 10 && x.toString()) // number | boolean | string
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 33, 14))
>x.toString : Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 96, 26))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 33, 14))
>toString : 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
>y : Symbol(y, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 34, 7))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 33, 14))
>x.toString : Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 96, 26))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 33, 14))
>toString : Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 96, 26))
}
function foo8(x: number | string) {
>foo8 : Symbol(foo8, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 45, 1))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 46, 14))
// Mixing typeguard
// Assigning value to x in outer guard shouldn't stop narrowing in the inner expression
return typeof x !== "string"
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 46, 14))
&& (x = 10) // change x - number| string
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 46, 14))
&& (typeof x === "number"
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 46, 14))
? x // number
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 46, 14))
: x.length); // string
>x.length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
>x : Symbol(x, Decl(typeGuardsInRightOperandOfAndAndOperator.ts, 46, 14))
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
}