Merge pull request #14195 from HerringtonDarkholme/object-iteration

fix #14187, forIn should allow non primitive object as right hand side
This commit is contained in:
Mohamed Hegazy
2017-03-23 10:11:50 -07:00
committed by GitHub
12 changed files with 128 additions and 1 deletions
+1 -1
View File
@@ -18788,7 +18788,7 @@ namespace ts {
// unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved
// in this case error about missing name is already reported - do not report extra one
if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, TypeFlags.Object | TypeFlags.TypeVariable)) {
if (!isTypeAnyOrAllConstituentTypesHaveKind(rightType, TypeFlags.Object | TypeFlags.TypeVariable | TypeFlags.NonPrimitive)) {
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);
}
@@ -0,0 +1,13 @@
//// [nonPrimitiveIndexingWithForIn.ts]
var a: object;
for (var key in a) {
var value = a[key];
}
//// [nonPrimitiveIndexingWithForIn.js]
var a;
for (var key in a) {
var value = a[key];
}
@@ -0,0 +1,14 @@
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForIn.ts ===
var a: object;
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForIn.ts, 0, 3))
for (var key in a) {
>key : Symbol(key, Decl(nonPrimitiveIndexingWithForIn.ts, 2, 8))
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForIn.ts, 0, 3))
var value = a[key];
>value : Symbol(value, Decl(nonPrimitiveIndexingWithForIn.ts, 3, 7))
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForIn.ts, 0, 3))
>key : Symbol(key, Decl(nonPrimitiveIndexingWithForIn.ts, 2, 8))
}
@@ -0,0 +1,15 @@
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForIn.ts ===
var a: object;
>a : object
for (var key in a) {
>key : string
>a : object
var value = a[key];
>value : any
>a[key] : any
>a : object
>key : string
}
@@ -0,0 +1,12 @@
tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForInNoImplicitAny.ts(4,17): error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForInNoImplicitAny.ts (1 errors) ====
var a: object;
for (var key in a) {
var value = a[key]; // error
~~~~~~
!!! error TS7017: Element implicitly has an 'any' type because type '{}' has no index signature.
}
@@ -0,0 +1,13 @@
//// [nonPrimitiveIndexingWithForInNoImplicitAny.ts]
var a: object;
for (var key in a) {
var value = a[key]; // error
}
//// [nonPrimitiveIndexingWithForInNoImplicitAny.js]
var a;
for (var key in a) {
var value = a[key]; // error
}
@@ -0,0 +1,13 @@
//// [nonPrimitiveIndexingWithForInSupressError.ts]
var a: object;
for (var key in a) {
var value = a[key];
}
//// [nonPrimitiveIndexingWithForInSupressError.js]
var a;
for (var key in a) {
var value = a[key];
}
@@ -0,0 +1,14 @@
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForInSupressError.ts ===
var a: object;
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 0, 3))
for (var key in a) {
>key : Symbol(key, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 2, 8))
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 0, 3))
var value = a[key];
>value : Symbol(value, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 3, 7))
>a : Symbol(a, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 0, 3))
>key : Symbol(key, Decl(nonPrimitiveIndexingWithForInSupressError.ts, 2, 8))
}
@@ -0,0 +1,15 @@
=== tests/cases/conformance/types/nonPrimitive/nonPrimitiveIndexingWithForInSupressError.ts ===
var a: object;
>a : object
for (var key in a) {
>key : string
>a : object
var value = a[key];
>value : any
>a[key] : any
>a : object
>key : string
}
@@ -0,0 +1,5 @@
var a: object;
for (var key in a) {
var value = a[key];
}
@@ -0,0 +1,6 @@
// @noImplicitAny: true
var a: object;
for (var key in a) {
var value = a[key]; // error
}
@@ -0,0 +1,7 @@
// @noImplicitAny: true
// @suppressImplicitAnyIndexErrors: true
var a: object;
for (var key in a) {
var value = a[key];
}