Skip to content

Commit 1f8aa05

Browse files
authored
Merge pull request microsoft#26483 from Microsoft/fix20594
Ensure for-in loop variable is checked
2 parents c667a98 + a901930 commit 1f8aa05

File tree

5 files changed

+92
-2
lines changed

5 files changed

+92
-2
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18382,7 +18382,7 @@ namespace ts {
1838218382
return errorType;
1838318383
}
1838418384

18385-
const indexType = isForInVariableForNumericPropertyNames(indexExpression) ? numberType : checkExpression(indexExpression);
18385+
const indexType = checkExpression(indexExpression);
1838618386

1838718387
if (objectType === errorType || objectType === silentNeverType) {
1838818388
return objectType;
@@ -18393,7 +18393,7 @@ namespace ts {
1839318393
return errorType;
1839418394
}
1839518395

18396-
return checkIndexedAccessIndexType(getIndexedAccessType(objectType, indexType, node), node);
18396+
return checkIndexedAccessIndexType(getIndexedAccessType(objectType, isForInVariableForNumericPropertyNames(indexExpression) ? numberType : indexType, node), node);
1839718397
}
1839818398

1839918399
function checkThatExpressionIsProperSymbolReference(expression: Expression, expressionType: Type, reportError: boolean): boolean {

tests/baselines/reference/capturedLetConstInLoop1.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,16 @@ for (const y = 0; y < 1;) {
111111
const x = 1;
112112
(function() { return x + y});
113113
(() => x + y);
114+
}
115+
116+
// https://github.com/Microsoft/TypeScript/issues/20594
117+
declare const sobj: { [x: string]: any };
118+
for (let sx in sobj) {
119+
(() => sobj[sx]);
120+
}
121+
declare const iobj: { [x: number]: any };
122+
for (let ix in iobj) {
123+
(() => iobj[ix]);
114124
}
115125

116126
//// [capturedLetConstInLoop1.js]
@@ -270,3 +280,15 @@ var _loop_20 = function (y) {
270280
for (var y = 0; y < 1;) {
271281
_loop_20(y);
272282
}
283+
var _loop_21 = function (sx) {
284+
(function () { return sobj[sx]; });
285+
};
286+
for (var sx in sobj) {
287+
_loop_21(sx);
288+
}
289+
var _loop_22 = function (ix) {
290+
(function () { return iobj[ix]; });
291+
};
292+
for (var ix in iobj) {
293+
_loop_22(ix);
294+
}

tests/baselines/reference/capturedLetConstInLoop1.symbols

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,29 @@ for (const y = 0; y < 1;) {
258258
>x : Symbol(x, Decl(capturedLetConstInLoop1.ts, 109, 9))
259259
>y : Symbol(y, Decl(capturedLetConstInLoop1.ts, 108, 10))
260260
}
261+
262+
// https://github.com/Microsoft/TypeScript/issues/20594
263+
declare const sobj: { [x: string]: any };
264+
>sobj : Symbol(sobj, Decl(capturedLetConstInLoop1.ts, 115, 13))
265+
>x : Symbol(x, Decl(capturedLetConstInLoop1.ts, 115, 23))
266+
267+
for (let sx in sobj) {
268+
>sx : Symbol(sx, Decl(capturedLetConstInLoop1.ts, 116, 8))
269+
>sobj : Symbol(sobj, Decl(capturedLetConstInLoop1.ts, 115, 13))
270+
271+
(() => sobj[sx]);
272+
>sobj : Symbol(sobj, Decl(capturedLetConstInLoop1.ts, 115, 13))
273+
>sx : Symbol(sx, Decl(capturedLetConstInLoop1.ts, 116, 8))
274+
}
275+
declare const iobj: { [x: number]: any };
276+
>iobj : Symbol(iobj, Decl(capturedLetConstInLoop1.ts, 119, 13))
277+
>x : Symbol(x, Decl(capturedLetConstInLoop1.ts, 119, 23))
278+
279+
for (let ix in iobj) {
280+
>ix : Symbol(ix, Decl(capturedLetConstInLoop1.ts, 120, 8))
281+
>iobj : Symbol(iobj, Decl(capturedLetConstInLoop1.ts, 119, 13))
282+
283+
(() => iobj[ix]);
284+
>iobj : Symbol(iobj, Decl(capturedLetConstInLoop1.ts, 119, 13))
285+
>ix : Symbol(ix, Decl(capturedLetConstInLoop1.ts, 120, 8))
286+
}

tests/baselines/reference/capturedLetConstInLoop1.types

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,3 +426,35 @@ for (const y = 0; y < 1;) {
426426
>x : 1
427427
>y : 0
428428
}
429+
430+
// https://github.com/Microsoft/TypeScript/issues/20594
431+
declare const sobj: { [x: string]: any };
432+
>sobj : { [x: string]: any; }
433+
>x : string
434+
435+
for (let sx in sobj) {
436+
>sx : string
437+
>sobj : { [x: string]: any; }
438+
439+
(() => sobj[sx]);
440+
>(() => sobj[sx]) : () => any
441+
>() => sobj[sx] : () => any
442+
>sobj[sx] : any
443+
>sobj : { [x: string]: any; }
444+
>sx : string
445+
}
446+
declare const iobj: { [x: number]: any };
447+
>iobj : { [x: number]: any; }
448+
>x : number
449+
450+
for (let ix in iobj) {
451+
>ix : string
452+
>iobj : { [x: number]: any; }
453+
454+
(() => iobj[ix]);
455+
>(() => iobj[ix]) : () => any
456+
>() => iobj[ix] : () => any
457+
>iobj[ix] : any
458+
>iobj : { [x: number]: any; }
459+
>ix : string
460+
}

tests/cases/compiler/capturedLetConstInLoop1.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,14 @@ for (const y = 0; y < 1;) {
110110
const x = 1;
111111
(function() { return x + y});
112112
(() => x + y);
113+
}
114+
115+
// https://github.com/Microsoft/TypeScript/issues/20594
116+
declare const sobj: { [x: string]: any };
117+
for (let sx in sobj) {
118+
(() => sobj[sx]);
119+
}
120+
declare const iobj: { [x: number]: any };
121+
for (let ix in iobj) {
122+
(() => iobj[ix]);
113123
}

0 commit comments

Comments
 (0)