File tree Expand file tree Collapse file tree 3 files changed +50
-0
lines changed
cases/conformance/expressions/typeGuards Expand file tree Collapse file tree 3 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -41,6 +41,15 @@ function foo4(x: number | string | boolean) {
4141 : x . toString ( ) ; // number
4242 } ) ( x ) ; // x here is narrowed to number | boolean
4343}
44+ // Type guards affect nested function expressions, but not nested function declarations
45+ function foo5 ( x : number | string | boolean ) {
46+ if ( typeof x === "string" ) {
47+ var y = x ; // string;
48+ function foo ( ) {
49+ var z = x ; // number | string | boolean, type guard has no effect
50+ }
51+ }
52+ }
4453module m {
4554 var x : number | string | boolean ;
4655 module m2 {
@@ -96,6 +105,15 @@ function foo4(x) {
96105 return typeof x === "boolean" ? x . toString ( ) : x . toString ( ) ; // number
97106 } ) ( x ) ; // x here is narrowed to number | boolean
98107}
108+ // Type guards affect nested function expressions, but not nested function declarations
109+ function foo5 ( x ) {
110+ if ( typeof x === "string" ) {
111+ var y = x ; // string;
112+ function foo ( ) {
113+ var z = x ; // number | string | boolean, type guard has no effect
114+ }
115+ }
116+ }
99117var m ;
100118( function ( m ) {
101119 var x ;
Original file line number Diff line number Diff line change @@ -173,6 +173,29 @@ function foo4(x: number | string | boolean) {
173173 })(x); // x here is narrowed to number | boolean
174174>x : number | boolean
175175}
176+ // Type guards affect nested function expressions, but not nested function declarations
177+ function foo5(x: number | string | boolean) {
178+ >foo5 : (x: string | number | boolean) => void
179+ >x : string | number | boolean
180+
181+ if (typeof x === "string") {
182+ >typeof x === "string" : boolean
183+ >typeof x : string
184+ >x : string | number | boolean
185+
186+ var y = x; // string;
187+ >y : string
188+ >x : string
189+
190+ function foo() {
191+ >foo : () => void
192+
193+ var z = x; // number | string | boolean, type guard has no effect
194+ >z : string | number | boolean
195+ >x : string | number | boolean
196+ }
197+ }
198+ }
176199module m {
177200>m : typeof m
178201
Original file line number Diff line number Diff line change @@ -40,6 +40,15 @@ function foo4(x: number | string | boolean) {
4040 : x . toString ( ) ; // number
4141 } ) ( x ) ; // x here is narrowed to number | boolean
4242}
43+ // Type guards affect nested function expressions, but not nested function declarations
44+ function foo5 ( x : number | string | boolean ) {
45+ if ( typeof x === "string" ) {
46+ var y = x ; // string;
47+ function foo ( ) {
48+ var z = x ; // number | string | boolean, type guard has no effect
49+ }
50+ }
51+ }
4352module m {
4453 var x : number | string | boolean ;
4554 module m2 {
You can’t perform that action at this time.
0 commit comments