Error on for (const x in never) (#22988)

* Error on `for (const x in never)`

* Update diagnostic

* Provide argument to diagnostic
This commit is contained in:
Andy
2018-03-29 11:39:30 -07:00
committed by GitHub
parent 6c517ef276
commit a9aca81601
17 changed files with 69 additions and 37 deletions

View File

@@ -22590,8 +22590,8 @@ 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 (!isTypeAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.InstantiableNonPrimitive)) {
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);
if (rightType === neverType || !isTypeAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.InstantiableNonPrimitive)) {
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_but_here_has_type_0, typeToString(rightType));
}
checkSourceElement(node.statement);

View File

@@ -1372,7 +1372,7 @@
"category": "Error",
"code": 2406
},
"The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.": {
"The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type '{0}'.": {
"category": "Error",
"code": 2407
},

View File

@@ -1,6 +1,6 @@
tests/cases/conformance/statements/for-inStatements/for-inStatements.ts(33,18): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'string', but here has type 'keyof this'.
tests/cases/conformance/statements/for-inStatements/for-inStatements.ts(50,18): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'string', but here has type 'keyof this'.
tests/cases/conformance/statements/for-inStatements/for-inStatements.ts(79,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatements.ts(79,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'Color.Blue'.
==== tests/cases/conformance/statements/for-inStatements/for-inStatements.ts (3 errors) ====
@@ -88,5 +88,5 @@ tests/cases/conformance/statements/for-inStatements/for-inStatements.ts(79,15):
for (var x in Color) { }
for (var x in Color.Blue) { }
~~~~~~~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'Color.Blue'.

View File

@@ -2,19 +2,19 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(2
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(5,6): error TS2405: The left-hand side of a 'for...in' statement must be of type 'string' or 'any'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(8,6): error TS2405: The left-hand side of a 'for...in' statement must be of type 'string' or 'any'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(10,10): error TS2404: The left-hand side of a 'for...in' statement cannot use a type annotation.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(13,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(17,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(18,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(19,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(20,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(22,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(29,23): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(13,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'void'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(17,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'string'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(18,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'string'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(19,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'string'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(20,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'string'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(22,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'string'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(29,23): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(31,18): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'string', but here has type 'keyof this'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(38,23): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(46,23): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(38,23): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(46,23): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(48,18): error TS2403: Subsequent variable declarations must have the same type. Variable 'x' must be of type 'string', but here has type 'keyof this'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(51,23): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(62,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(51,23): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.
tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(62,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.
==== tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts (17 errors) ====
@@ -40,26 +40,26 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(6
function fn(): void { }
for (var x in fn()) { }
~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'void'.
var c : string, d:string, e;
for (var x in c || d) { }
~~~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'string'.
for (var x in e ? c : d) { }
~~~~~~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'string'.
for (var x in 42 ? c : d) { }
~~~~~~~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'string'.
for (var x in '' ? c : d) { }
~~~~~~~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'string'.
for (var x in 42 ? d[x] : c[x]) { }
for (var x in c[23]) { }
~~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'string'.
for (var x in (<T>(x: T) => x)) { }
for (var x in function (x: string, y: number) { return x + y }) { }
@@ -68,7 +68,7 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(6
biz() : number{
for (var x in this.biz()) { }
~~~~~~~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.
for (var x in this.biz) { }
for (var x in this) { }
~
@@ -81,7 +81,7 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(6
for (var x in this.baz) { }
for (var x in this.baz()) { }
~~~~~~~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.
return null;
}
@@ -91,7 +91,7 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(6
boz() {
for (var x in this.biz()) { }
~~~~~~~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.
for (var x in this.biz) { }
for (var x in this) { }
~
@@ -100,7 +100,7 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(6
for (var x in super.biz) { }
for (var x in super.biz()) { }
~~~~~~~~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.
return null;
}
}
@@ -113,5 +113,5 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(6
for (var x in i[42]) { }
~~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.

View File

@@ -1,8 +1,8 @@
tests/cases/compiler/forIn2.ts(1,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/compiler/forIn2.ts(1,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type '1'.
==== tests/cases/compiler/forIn2.ts (1 errors) ====
for (var i in 1) {
~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type '1'.
}

View File

@@ -1,9 +1,9 @@
tests/cases/compiler/forInStatement2.ts(2,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/compiler/forInStatement2.ts(2,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.
==== tests/cases/compiler/forInStatement2.ts (1 errors) ====
var expr: number;
for (var a in expr) {
~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'number'.
}

View File

@@ -9,9 +9,10 @@ tests/cases/conformance/types/never/neverTypeErrors1.ts(13,5): error TS2322: Typ
tests/cases/conformance/types/never/neverTypeErrors1.ts(17,5): error TS2322: Type '1' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors1.ts(20,16): error TS2534: A function returning 'never' cannot have a reachable end point.
tests/cases/conformance/types/never/neverTypeErrors1.ts(23,17): error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
tests/cases/conformance/types/never/neverTypeErrors1.ts(24,17): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'never'.
==== tests/cases/conformance/types/never/neverTypeErrors1.ts (11 errors) ====
==== tests/cases/conformance/types/never/neverTypeErrors1.ts (12 errors) ====
function f1() {
let x: never;
x = 1;
@@ -57,4 +58,7 @@ tests/cases/conformance/types/never/neverTypeErrors1.ts(23,17): error TS2488: Ty
for (const n of f4()) {}
~~~~
!!! error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
for (const n in f4()) {}
~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'never'.

View File

@@ -22,6 +22,7 @@ function f4(): never {
}
for (const n of f4()) {}
for (const n in f4()) {}
//// [neverTypeErrors1.js]
@@ -46,3 +47,4 @@ function f4() {
for (var _i = 0, _a = f4(); _i < _a.length; _i++) {
var n = _a[_i];
}
for (var n in f4()) { }

View File

@@ -48,3 +48,7 @@ for (const n of f4()) {}
>n : Symbol(n, Decl(neverTypeErrors1.ts, 22, 10))
>f4 : Symbol(f4, Decl(neverTypeErrors1.ts, 17, 1))
for (const n in f4()) {}
>n : Symbol(n, Decl(neverTypeErrors1.ts, 23, 10))
>f4 : Symbol(f4, Decl(neverTypeErrors1.ts, 17, 1))

View File

@@ -62,3 +62,8 @@ for (const n of f4()) {}
>f4() : never
>f4 : () => never
for (const n in f4()) {}
>n : string
>f4() : never
>f4 : () => never

View File

@@ -9,9 +9,10 @@ tests/cases/conformance/types/never/neverTypeErrors2.ts(13,5): error TS2322: Typ
tests/cases/conformance/types/never/neverTypeErrors2.ts(17,5): error TS2322: Type '1' is not assignable to type 'never'.
tests/cases/conformance/types/never/neverTypeErrors2.ts(20,16): error TS2534: A function returning 'never' cannot have a reachable end point.
tests/cases/conformance/types/never/neverTypeErrors2.ts(23,17): error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
tests/cases/conformance/types/never/neverTypeErrors2.ts(24,17): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'never'.
==== tests/cases/conformance/types/never/neverTypeErrors2.ts (11 errors) ====
==== tests/cases/conformance/types/never/neverTypeErrors2.ts (12 errors) ====
function f1() {
let x: never;
x = 1;
@@ -57,4 +58,7 @@ tests/cases/conformance/types/never/neverTypeErrors2.ts(23,17): error TS2488: Ty
for (const n of f4()) {}
~~~~
!!! error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
for (const n in f4()) {}
~~~~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'never'.

View File

@@ -22,6 +22,7 @@ function f4(): never {
}
for (const n of f4()) {}
for (const n in f4()) {}
//// [neverTypeErrors2.js]
@@ -46,3 +47,4 @@ function f4() {
for (var _i = 0, _a = f4(); _i < _a.length; _i++) {
var n = _a[_i];
}
for (var n in f4()) { }

View File

@@ -48,3 +48,7 @@ for (const n of f4()) {}
>n : Symbol(n, Decl(neverTypeErrors2.ts, 22, 10))
>f4 : Symbol(f4, Decl(neverTypeErrors2.ts, 17, 1))
for (const n in f4()) {}
>n : Symbol(n, Decl(neverTypeErrors2.ts, 23, 10))
>f4 : Symbol(f4, Decl(neverTypeErrors2.ts, 17, 1))

View File

@@ -62,3 +62,8 @@ for (const n of f4()) {}
>f4() : never
>f4 : () => never
for (const n in f4()) {}
>n : string
>f4() : never
>f4 : () => never

View File

@@ -1,6 +1,6 @@
tests/cases/conformance/es6/Symbols/symbolType13.ts(4,6): error TS2405: The left-hand side of a 'for...in' statement must be of type 'string' or 'any'.
tests/cases/conformance/es6/Symbols/symbolType13.ts(5,11): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/es6/Symbols/symbolType13.ts(6,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
tests/cases/conformance/es6/Symbols/symbolType13.ts(5,11): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'symbol'.
tests/cases/conformance/es6/Symbols/symbolType13.ts(6,15): error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'symbol'.
==== tests/cases/conformance/es6/Symbols/symbolType13.ts (3 errors) ====
@@ -12,7 +12,7 @@ tests/cases/conformance/es6/Symbols/symbolType13.ts(6,15): error TS2407: The rig
!!! error TS2405: The left-hand side of a 'for...in' statement must be of type 'string' or 'any'.
for (x in s) { }
~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'symbol'.
for (var y in s) { }
~
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'symbol'.

View File

@@ -21,3 +21,4 @@ function f4(): never {
}
for (const n of f4()) {}
for (const n in f4()) {}

View File

@@ -23,3 +23,4 @@ function f4(): never {
}
for (const n of f4()) {}
for (const n in f4()) {}