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

251 lines
11 KiB
Text

=== 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 : Symbol(foo, Decl(typeGuardsInConditionalExpression.ts, 0, 0))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 7, 13))
return typeof x === "string"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 7, 13))
? x.length // string
>x.length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 7, 13))
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
: x++; // number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 7, 13))
}
function foo2(x: number | string) {
>foo2 : Symbol(foo2, Decl(typeGuardsInConditionalExpression.ts, 11, 1))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 12, 14))
// x is assigned in the if true branch, the type is not narrowed
return typeof x === "string"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 12, 14))
? (x = 10 && x)// string | number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 12, 14))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 12, 14))
: x; // string | number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 12, 14))
}
function foo3(x: number | string) {
>foo3 : Symbol(foo3, Decl(typeGuardsInConditionalExpression.ts, 17, 1))
>x : 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"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 18, 14))
? (x = "Hello" && x) // string | number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 18, 14))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 18, 14))
: x; // string | number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 18, 14))
}
function foo4(x: number | string) {
>foo4 : Symbol(foo4, Decl(typeGuardsInConditionalExpression.ts, 24, 1))
>x : 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"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 25, 14))
? x // string | number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 25, 14))
: (x = 10 && x); // string | number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 25, 14))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 25, 14))
}
function foo5(x: number | string) {
>foo5 : Symbol(foo5, Decl(typeGuardsInConditionalExpression.ts, 31, 1))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 32, 14))
// false branch updates the variable - so here it is not number
return typeof x === "string"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 32, 14))
? x // string | number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 32, 14))
: (x = "hello" && x); // string | number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 32, 14))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 32, 14))
}
function foo6(x: number | string) {
>foo6 : Symbol(foo6, Decl(typeGuardsInConditionalExpression.ts, 37, 1))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14))
// Modify in both branches
return typeof x === "string"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14))
? (x = 10 && x) // string | number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14))
: (x = "hello" && x); // string | number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 38, 14))
}
function foo7(x: number | string | boolean) {
>foo7 : Symbol(foo7, Decl(typeGuardsInConditionalExpression.ts, 43, 1))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14))
return typeof x === "string"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14))
? x === "hello" // string
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14))
: typeof x === "boolean"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14))
? x // boolean
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14))
: x == 10; // number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 44, 14))
}
function foo8(x: number | string | boolean) {
>foo8 : Symbol(foo8, Decl(typeGuardsInConditionalExpression.ts, 50, 1))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14))
var b: number | boolean;
>b : Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 52, 7))
return typeof x === "string"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14))
? x === "hello"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14))
: ((b = x) && // number | boolean
>b : Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 52, 7))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14))
(typeof x === "boolean"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14))
? x // boolean
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14))
: x == 10)); // number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 51, 14))
}
function foo9(x: number | string) {
>foo9 : Symbol(foo9, Decl(typeGuardsInConditionalExpression.ts, 59, 1))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 60, 14))
var y = 10;
>y : Symbol(y, Decl(typeGuardsInConditionalExpression.ts, 61, 7))
// usage of x or assignment to separate variable shouldn't cause narrowing of type to stop
return typeof x === "string"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 60, 14))
? ((y = x.length) && x === "hello") // string
>y : Symbol(y, Decl(typeGuardsInConditionalExpression.ts, 61, 7))
>x.length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 60, 14))
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 60, 14))
: x === 10; // number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 60, 14))
}
function foo10(x: number | string | boolean) {
>foo10 : Symbol(foo10, Decl(typeGuardsInConditionalExpression.ts, 66, 1))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15))
// Mixing typeguards
var b: boolean | number;
>b : Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 69, 7))
return typeof x === "string"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15))
? x // string
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15))
: ((b = x) // x is number | boolean
>b : Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 69, 7))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15))
&& typeof x === "number"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15))
&& x.toString()); // x is number
>x.toString : Symbol(Number.toString, Decl(lib.d.ts, 458, 18))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 67, 15))
>toString : Symbol(Number.toString, Decl(lib.d.ts, 458, 18))
}
function foo11(x: number | string | boolean) {
>foo11 : Symbol(foo11, Decl(typeGuardsInConditionalExpression.ts, 75, 1))
>x : 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 : Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 79, 7))
return typeof x === "string"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15))
? x // number | boolean | string - changed in the false branch
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15))
: ((b = x) // x is number | boolean | string - because the assignment changed it
>b : Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 79, 7))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15))
&& typeof x === "number"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15))
&& (x = 10) // assignment to x
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15))
&& x); // x is number | boolean | string
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 76, 15))
}
function foo12(x: number | string | boolean) {
>foo12 : Symbol(foo12, Decl(typeGuardsInConditionalExpression.ts, 86, 1))
>x : 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 : Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 90, 7))
return typeof x === "string"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15))
? (x = 10 && x.toString().length) // number | boolean | string - changed here
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15))
>x.toString().length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
>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(typeGuardsInConditionalExpression.ts, 87, 15))
>toString : Symbol(toString, Decl(lib.d.ts, 277, 18), Decl(lib.d.ts, 458, 18), Decl(lib.d.ts, 96, 26))
>length : Symbol(String.length, Decl(lib.d.ts, 414, 19))
: ((b = x) // x is number | boolean | string - changed in true branch
>b : Symbol(b, Decl(typeGuardsInConditionalExpression.ts, 90, 7))
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15))
&& typeof x === "number"
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15))
&& x); // x is number
>x : Symbol(x, Decl(typeGuardsInConditionalExpression.ts, 87, 15))
}