2014-11-05 23:21:32 +01:00
=== tests/cases/conformance/expressions/typeGuards/typeGuardsInFunctionAndModuleBlock.ts ===
// typeguards are scoped in function/module block
function foo(x: number | string | boolean) {
>foo : (x: string | number | boolean) => string
>x : string | number | boolean
return typeof x === "string"
2014-11-14 19:42:31 +01:00
>typeof x === "string" ? x : function f() { var b = x; // number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } () : string
2014-11-05 23:21:32 +01:00
>typeof x === "string" : boolean
>typeof x : string
>x : string | number | boolean
? x
>x : string
: function f() {
2014-11-14 19:42:31 +01:00
>function f() { var b = x; // number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } () : string
>function f() { var b = x; // number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } : () => string
2014-11-05 23:21:32 +01:00
>f : () => string
2014-11-14 19:42:31 +01:00
var b = x; // number | boolean
>b : number | boolean
>x : number | boolean
2014-11-05 23:21:32 +01:00
return typeof x === "boolean"
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
>typeof x === "boolean" : boolean
>typeof x : string
2014-11-14 19:42:31 +01:00
>x : number | boolean
2014-11-05 23:21:32 +01:00
? x.toString() // boolean
>x.toString() : string
>x.toString : () => string
>x : boolean
>toString : () => string
2014-11-14 19:42:31 +01:00
: x.toString(); // number
2014-11-05 23:21:32 +01:00
>x.toString() : string
2014-11-14 19:42:31 +01:00
>x.toString : (radix?: number) => string
>x : number
>toString : (radix?: number) => string
2014-11-05 23:21:32 +01:00
} ();
}
function foo2(x: number | string | boolean) {
>foo2 : (x: string | number | boolean) => string
>x : string | number | boolean
return typeof x === "string"
2014-11-14 19:42:31 +01:00
>typeof x === "string" ? x : function f(a: number | boolean) { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } (x) : string
2014-11-05 23:21:32 +01:00
>typeof x === "string" : boolean
>typeof x : string
>x : string | number | boolean
? x
>x : string
: function f(a: number | boolean) {
2014-11-14 19:42:31 +01:00
>function f(a: number | boolean) { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } (x) : string
>function f(a: number | boolean) { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } : (a: number | boolean) => string
2014-11-05 23:21:32 +01:00
>f : (a: number | boolean) => string
>a : number | boolean
2014-11-14 19:42:31 +01:00
var b = x; // new scope - number | boolean
>b : number | boolean
>x : number | boolean
2014-11-05 23:21:32 +01:00
return typeof x === "boolean"
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
>typeof x === "boolean" : boolean
>typeof x : string
2014-11-14 19:42:31 +01:00
>x : number | boolean
2014-11-05 23:21:32 +01:00
? x.toString() // boolean
>x.toString() : string
>x.toString : () => string
>x : boolean
>toString : () => string
2014-11-14 19:42:31 +01:00
: x.toString(); // number
2014-11-05 23:21:32 +01:00
>x.toString() : string
2014-11-14 19:42:31 +01:00
>x.toString : (radix?: number) => string
>x : number
>toString : (radix?: number) => string
2014-11-05 23:21:32 +01:00
} (x); // x here is narrowed to number | boolean
>x : number | boolean
}
function foo3(x: number | string | boolean) {
>foo3 : (x: string | number | boolean) => string
>x : string | number | boolean
return typeof x === "string"
2014-11-14 19:42:31 +01:00
>typeof x === "string" ? x : (() => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number })() : string
2014-11-05 23:21:32 +01:00
>typeof x === "string" : boolean
>typeof x : string
>x : string | number | boolean
? x
>x : string
: (() => {
2014-11-14 19:42:31 +01:00
>(() => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number })() : string
>(() => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number }) : () => string
>() => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } : () => string
2014-11-05 23:21:32 +01:00
2014-11-14 19:42:31 +01:00
var b = x; // new scope - number | boolean
>b : number | boolean
>x : number | boolean
2014-11-05 23:21:32 +01:00
return typeof x === "boolean"
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
>typeof x === "boolean" : boolean
>typeof x : string
2014-11-14 19:42:31 +01:00
>x : number | boolean
2014-11-05 23:21:32 +01:00
? x.toString() // boolean
>x.toString() : string
>x.toString : () => string
>x : boolean
>toString : () => string
2014-11-14 19:42:31 +01:00
: x.toString(); // number
2014-11-05 23:21:32 +01:00
>x.toString() : string
2014-11-14 19:42:31 +01:00
>x.toString : (radix?: number) => string
>x : number
>toString : (radix?: number) => string
2014-11-05 23:21:32 +01:00
})();
}
function foo4(x: number | string | boolean) {
>foo4 : (x: string | number | boolean) => string
>x : string | number | boolean
return typeof x === "string"
2014-11-14 19:42:31 +01:00
>typeof x === "string" ? x : ((a: number | boolean) => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number })(x) : string
2014-11-05 23:21:32 +01:00
>typeof x === "string" : boolean
>typeof x : string
>x : string | number | boolean
? x
>x : string
: ((a: number | boolean) => {
2014-11-14 19:42:31 +01:00
>((a: number | boolean) => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number })(x) : string
>((a: number | boolean) => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number }) : (a: number | boolean) => string
>(a: number | boolean) => { var b = x; // new scope - number | boolean return typeof x === "boolean" ? x.toString() // boolean : x.toString(); // number } : (a: number | boolean) => string
2014-11-05 23:21:32 +01:00
>a : number | boolean
2014-11-14 19:42:31 +01:00
var b = x; // new scope - number | boolean
>b : number | boolean
>x : number | boolean
2014-11-05 23:21:32 +01:00
return typeof x === "boolean"
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
>typeof x === "boolean" : boolean
>typeof x : string
2014-11-14 19:42:31 +01:00
>x : number | boolean
2014-11-05 23:21:32 +01:00
? x.toString() // boolean
>x.toString() : string
>x.toString : () => string
>x : boolean
>toString : () => string
2014-11-14 19:42:31 +01:00
: x.toString(); // number
2014-11-05 23:21:32 +01:00
>x.toString() : string
2014-11-14 19:42:31 +01:00
>x.toString : (radix?: number) => string
>x : number
>toString : (radix?: number) => string
2014-11-05 23:21:32 +01:00
})(x); // x here is narrowed to number | boolean
>x : number | boolean
}
2014-11-14 22:36:09 +01:00
// Type guards affect nested function expressions, but not nested function declarations
function foo5(x: number | string | boolean) {
>foo5 : (x: string | number | boolean) => void
>x : string | number | boolean
if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>x : string | number | boolean
var y = x; // string;
>y : string
>x : string
function foo() {
>foo : () => void
var z = x; // number | string | boolean, type guard has no effect
>z : string | number | boolean
>x : string | number | boolean
}
}
}
2014-11-05 23:21:32 +01:00
module m {
>m : typeof m
var x: number | string | boolean;
>x : string | number | boolean
module m2 {
>m2 : typeof m2
var b = x; // new scope - number | boolean | string
>b : string | number | boolean
>x : string | number | boolean
var y: string;
>y : string
if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>x : string | number | boolean
y = x // string;
>y = x : string
>y : string
>x : string
} else {
y = typeof x === "boolean"
>y = typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
>y : string
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
>typeof x === "boolean" : boolean
>typeof x : string
>x : number | boolean
? x.toString() // boolean
>x.toString() : string
>x.toString : () => string
>x : boolean
>toString : () => string
: x.toString(); // number
>x.toString() : string
>x.toString : (radix?: number) => string
>x : number
>toString : (radix?: number) => string
}
}
}
module m1 {
>m1 : typeof m1
var x: number | string | boolean;
>x : string | number | boolean
module m2.m3 {
>m2 : typeof m2
>m3 : typeof m3
var b = x; // new scope - number | boolean | string
>b : string | number | boolean
>x : string | number | boolean
var y: string;
>y : string
if (typeof x === "string") {
>typeof x === "string" : boolean
>typeof x : string
>x : string | number | boolean
y = x // string;
>y = x : string
>y : string
>x : string
} else {
y = typeof x === "boolean"
>y = typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
>y : string
>typeof x === "boolean" ? x.toString() // boolean : x.toString() : string
>typeof x === "boolean" : boolean
>typeof x : string
>x : number | boolean
? x.toString() // boolean
>x.toString() : string
>x.toString : () => string
>x : boolean
>toString : () => string
: x.toString(); // number
>x.toString() : string
>x.toString : (radix?: number) => string
>x : number
>toString : (radix?: number) => string
}
}
}