Skip to content

Commit a9aca81

Browse files
author
Andy
authored
Error on for (const x in never) (#22988)
* Error on `for (const x in never)` * Update diagnostic * Provide argument to diagnostic
1 parent 6c517ef commit a9aca81

17 files changed

+69
-37
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22590,8 +22590,8 @@ namespace ts {
2259022590

2259122591
// unknownType is returned i.e. if node.expression is identifier whose name cannot be resolved
2259222592
// in this case error about missing name is already reported - do not report extra one
22593-
if (!isTypeAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.InstantiableNonPrimitive)) {
22594-
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);
22593+
if (rightType === neverType || !isTypeAssignableToKind(rightType, TypeFlags.NonPrimitive | TypeFlags.InstantiableNonPrimitive)) {
22594+
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));
2259522595
}
2259622596

2259722597
checkSourceElement(node.statement);

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@
13721372
"category": "Error",
13731373
"code": 2406
13741374
},
1375-
"The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.": {
1375+
"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}'.": {
13761376
"category": "Error",
13771377
"code": 2407
13781378
},

tests/baselines/reference/for-inStatements.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
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'.
22
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'.
3-
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.
3+
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'.
44

55

66
==== 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):
8888
for (var x in Color) { }
8989
for (var x in Color.Blue) { }
9090
~~~~~~~~~~
91-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
91+
!!! 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'.
9292

tests/baselines/reference/for-inStatementsInvalid.errors.txt

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(2
22
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'.
33
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'.
44
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.
5-
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.
6-
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.
7-
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.
8-
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.
9-
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.
10-
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.
11-
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.
5+
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'.
6+
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'.
7+
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'.
8+
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'.
9+
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'.
10+
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'.
11+
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'.
1212
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'.
13-
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.
14-
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.
13+
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'.
14+
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'.
1515
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'.
16-
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.
17-
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.
16+
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'.
17+
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'.
1818

1919

2020
==== 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
4040
function fn(): void { }
4141
for (var x in fn()) { }
4242
~~~~
43-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
43+
!!! 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'.
4444

4545
var c : string, d:string, e;
4646

4747
for (var x in c || d) { }
4848
~~~~~~
49-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
49+
!!! 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'.
5050
for (var x in e ? c : d) { }
5151
~~~~~~~~~
52-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
52+
!!! 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'.
5353
for (var x in 42 ? c : d) { }
5454
~~~~~~~~~~
55-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
55+
!!! 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'.
5656
for (var x in '' ? c : d) { }
5757
~~~~~~~~~~
58-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
58+
!!! 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'.
5959
for (var x in 42 ? d[x] : c[x]) { }
6060
for (var x in c[23]) { }
6161
~~~~~
62-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
62+
!!! 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'.
6363

6464
for (var x in (<T>(x: T) => x)) { }
6565
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
6868
biz() : number{
6969
for (var x in this.biz()) { }
7070
~~~~~~~~~~
71-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
71+
!!! 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'.
7272
for (var x in this.biz) { }
7373
for (var x in this) { }
7474
~
@@ -81,7 +81,7 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(6
8181
for (var x in this.baz) { }
8282
for (var x in this.baz()) { }
8383
~~~~~~~~~~
84-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
84+
!!! 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'.
8585

8686
return null;
8787
}
@@ -91,7 +91,7 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(6
9191
boz() {
9292
for (var x in this.biz()) { }
9393
~~~~~~~~~~
94-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
94+
!!! 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'.
9595
for (var x in this.biz) { }
9696
for (var x in this) { }
9797
~
@@ -100,7 +100,7 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(6
100100
for (var x in super.biz) { }
101101
for (var x in super.biz()) { }
102102
~~~~~~~~~~~
103-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
103+
!!! 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'.
104104
return null;
105105
}
106106
}
@@ -113,5 +113,5 @@ tests/cases/conformance/statements/for-inStatements/for-inStatementsInvalid.ts(6
113113

114114
for (var x in i[42]) { }
115115
~~~~~
116-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
116+
!!! 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'.
117117

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
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.
1+
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'.
22

33

44
==== tests/cases/compiler/forIn2.ts (1 errors) ====
55
for (var i in 1) {
66
~
7-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
7+
!!! 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'.
88
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
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.
1+
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'.
22

33

44
==== tests/cases/compiler/forInStatement2.ts (1 errors) ====
55
var expr: number;
66
for (var a in expr) {
77
~~~~
8-
!!! error TS2407: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter.
8+
!!! 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'.
99
}

tests/baselines/reference/neverTypeErrors1.errors.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ tests/cases/conformance/types/never/neverTypeErrors1.ts(13,5): error TS2322: Typ
99
tests/cases/conformance/types/never/neverTypeErrors1.ts(17,5): error TS2322: Type '1' is not assignable to type 'never'.
1010
tests/cases/conformance/types/never/neverTypeErrors1.ts(20,16): error TS2534: A function returning 'never' cannot have a reachable end point.
1111
tests/cases/conformance/types/never/neverTypeErrors1.ts(23,17): error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
12+
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'.
1213

1314

14-
==== tests/cases/conformance/types/never/neverTypeErrors1.ts (11 errors) ====
15+
==== tests/cases/conformance/types/never/neverTypeErrors1.ts (12 errors) ====
1516
function f1() {
1617
let x: never;
1718
x = 1;
@@ -57,4 +58,7 @@ tests/cases/conformance/types/never/neverTypeErrors1.ts(23,17): error TS2488: Ty
5758
for (const n of f4()) {}
5859
~~~~
5960
!!! error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
61+
for (const n in f4()) {}
62+
~~~~
63+
!!! 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'.
6064

tests/baselines/reference/neverTypeErrors1.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ function f4(): never {
2222
}
2323

2424
for (const n of f4()) {}
25+
for (const n in f4()) {}
2526

2627

2728
//// [neverTypeErrors1.js]
@@ -46,3 +47,4 @@ function f4() {
4647
for (var _i = 0, _a = f4(); _i < _a.length; _i++) {
4748
var n = _a[_i];
4849
}
50+
for (var n in f4()) { }

tests/baselines/reference/neverTypeErrors1.symbols

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,7 @@ for (const n of f4()) {}
4848
>n : Symbol(n, Decl(neverTypeErrors1.ts, 22, 10))
4949
>f4 : Symbol(f4, Decl(neverTypeErrors1.ts, 17, 1))
5050

51+
for (const n in f4()) {}
52+
>n : Symbol(n, Decl(neverTypeErrors1.ts, 23, 10))
53+
>f4 : Symbol(f4, Decl(neverTypeErrors1.ts, 17, 1))
54+

tests/baselines/reference/neverTypeErrors1.types

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,8 @@ for (const n of f4()) {}
6262
>f4() : never
6363
>f4 : () => never
6464

65+
for (const n in f4()) {}
66+
>n : string
67+
>f4() : never
68+
>f4 : () => never
69+

0 commit comments

Comments
 (0)