fix #14187, forIn should allow non primitive object as right hand side

This commit is contained in:
Herrington Darkholme 2017-02-21 11:52:12 +08:00
parent b4d2b1db0d
commit 51966076d4
12 changed files with 129 additions and 2 deletions

View File

@ -1,4 +1,4 @@
/// <reference path="moduleNameResolver.ts"/>
/// <reference path="moduleNameResolver.ts"/>
/// <reference path="binder.ts"/>
/* @internal */
@ -18606,7 +18606,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);
}

View File

@ -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];
}

View File

@ -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))
}

View File

@ -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
}

View File

@ -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.
}

View File

@ -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
}

View File

@ -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];
}

View File

@ -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))
}

View File

@ -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
}

View File

@ -0,0 +1,5 @@
var a: object;
for (var key in a) {
var value = a[key];
}

View File

@ -0,0 +1,6 @@
// @noImplicitAny: true
var a: object;
for (var key in a) {
var value = a[key]; // error
}

View File

@ -0,0 +1,7 @@
// @noImplicitAny: true
// @suppressImplicitAnyIndexErrors: true
var a: object;
for (var key in a) {
var value = a[key];
}