From df544a687c0333387423f9a58be346ecd6ca9c84 Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Fri, 14 Nov 2014 13:36:09 -0800 Subject: [PATCH] Addressing CR feedback --- .../typeGuardsInFunctionAndModuleBlock.js | 18 +++++++++++++++ .../typeGuardsInFunctionAndModuleBlock.types | 23 +++++++++++++++++++ .../typeGuardsInFunctionAndModuleBlock.ts | 9 ++++++++ 3 files changed, 50 insertions(+) diff --git a/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.js b/tests/baselines/reference/typeGuardsInFunctionAndModuleBlock.js index 01bd6f267e..317017c9ac 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 ec844960f5..3d914b5c80 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 2727375294..ec23b75b59 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 {