diff --git a/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.js b/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.js index 01bd6f267ed..317017c9acc 100644 --- a/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.js +++ b/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.js @@ -41,6 +41,15 @@ function foo4(x: number | string | boolean) { : x.toString(); // number })(x); // x here is narrowed to number | boolean } +// Type guards affect nested function expressions, but not nested function declarations +function foo5(x: number | string | boolean) { + if (typeof x === "string") { + var y = x; // string; + function foo() { + var z = x; // number | string | boolean, type guard has no effect + } + } +} module m { var x: number | string | boolean; module m2 { @@ -96,6 +105,15 @@ function foo4(x) { return typeof x === "boolean" ? x.toString() : x.toString(); // number })(x); // x here is narrowed to number | boolean } +// Type guards affect nested function expressions, but not nested function declarations +function foo5(x) { + if (typeof x === "string") { + var y = x; // string; + function foo() { + var z = x; // number | string | boolean, type guard has no effect + } + } +} var m; (function (m) { var x; diff --git a/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.types b/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.types index ec844960f5d..3d914b5c80b 100644 --- a/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.types +++ b/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.types @@ -173,6 +173,29 @@ function foo4(x: number | string | boolean) { })(x); // x here is narrowed to number | boolean >x : number | boolean } +// 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 + } + } +} module m { >m : typeof m diff --git a/tests/cases/conformance/expressions/typeGuards/typeGuardsInFunctionAndModuleBlock.ts b/tests/cases/conformance/expressions/typeGuards/typeGuardsInFunctionAndModuleBlock.ts index 2727375294c..ec23b75b590 100644 --- a/tests/cases/conformance/expressions/typeGuards/typeGuardsInFunctionAndModuleBlock.ts +++ b/tests/cases/conformance/expressions/typeGuards/typeGuardsInFunctionAndModuleBlock.ts @@ -40,6 +40,15 @@ function foo4(x: number | string | boolean) { : x.toString(); // number })(x); // x here is narrowed to number | boolean } +// Type guards affect nested function expressions, but not nested function declarations +function foo5(x: number | string | boolean) { + if (typeof x === "string") { + var y = x; // string; + function foo() { + var z = x; // number | string | boolean, type guard has no effect + } + } +} module m { var x: number | string | boolean; module m2 {