Skip to content

Commit 5196607

Browse files
fix #14187, forIn should allow non primitive object as right hand side
1 parent b4d2b1d commit 5196607

12 files changed

+129
-2
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/// <reference path="moduleNameResolver.ts"/>
1+
/// <reference path="moduleNameResolver.ts"/>
22
/// <reference path="binder.ts"/>
33

44
/* @internal */
@@ -18606,7 +18606,7 @@ namespace ts {
1860618606

1860718607
// unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved
1860818608
// in this case error about missing name is already reported - do not report extra one
18609-
if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, TypeFlags.Object | TypeFlags.TypeVariable)) {
18609+
if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, TypeFlags.Object | TypeFlags.TypeVariable | TypeFlags.NonPrimitive)) {
1861018610
error(node.expression, Diagnostics.The_right_hand_side_of_a_for_in_statement_must_be_of_type_any_an_object_type_or_a_type_parameter);
1861118611
}
1861218612

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [nonPrimitiveIndexingWithForIn.ts]
2+
var a: object;
3+
4+
for (var key in a) {
5+
var value = a[key];
6+
}
7+
8+
9+
//// [nonPrimitiveIndexingWithForIn.js]
10+
var a;
11+
for (var key in a) {
12+
var value = a[key];
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForIn.ts ===
2+
var a: object;
3+
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForIn.ts, 0, 3))
4+
5+
for (var key in a) {
6+
>key : Symbol(key, Decl(nonPrimitiveIndexingWithForIn.ts, 2, 8))
7+
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForIn.ts, 0, 3))
8+
9+
var value = a[key];
10+
>value : Symbol(value, Decl(nonPrimitiveIndexingWithForIn.ts, 3, 7))
11+
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForIn.ts, 0, 3))
12+
>key : Symbol(key, Decl(nonPrimitiveIndexingWithForIn.ts, 2, 8))
13+
}
14+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForIn.ts ===
2+
var a: object;
3+
>a : object
4+
5+
for (var key in a) {
6+
>key : string
7+
>a : object
8+
9+
var value = a[key];
10+
>value : any
11+
>a[key] : any
12+
>a : object
13+
>key : string
14+
}
15+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForInNoImplicitAny.ts(4,17): error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
2+
3+
4+
==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForInNoImplicitAny.ts (1 errors) ====
5+
var a: object;
6+
7+
for (var key in a) {
8+
var value = a[key]; // error
9+
~~~~~~
10+
!!! error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
11+
}
12+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [nonPrimitiveIndexingWithForInNoImplicitAny.ts]
2+
var a: object;
3+
4+
for (var key in a) {
5+
var value = a[key]; // error
6+
}
7+
8+
9+
//// [nonPrimitiveIndexingWithForInNoImplicitAny.js]
10+
var a;
11+
for (var key in a) {
12+
var value = a[key]; // error
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [nonPrimitiveIndexingWithForInSupressError.ts]
2+
var a: object;
3+
4+
for (var key in a) {
5+
var value = a[key];
6+
}
7+
8+
9+
//// [nonPrimitiveIndexingWithForInSupressError.js]
10+
var a;
11+
for (var key in a) {
12+
var value = a[key];
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForInSupressError.ts ===
2+
var a: object;
3+
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 0, 3))
4+
5+
for (var key in a) {
6+
>key : Symbol(key, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 2, 8))
7+
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 0, 3))
8+
9+
var value = a[key];
10+
>value : Symbol(value, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 3, 7))
11+
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 0, 3))
12+
>key : Symbol(key, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 2, 8))
13+
}
14+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForInSupressError.ts ===
2+
var a: object;
3+
>a : object
4+
5+
for (var key in a) {
6+
>key : string
7+
>a : object
8+
9+
var value = a[key];
10+
>value : any
11+
>a[key] : any
12+
>a : object
13+
>key : string
14+
}
15+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var a: object;
2+
3+
for (var key in a) {
4+
var value = a[key];
5+
}

0 commit comments

Comments
 (0)