Skip to content

Commit 3939b38

Browse files
fixes #48630 array binding pattern with only OmittedExpressions does not check RHS of for-of loop (#49008)
* fix RHS of for..of loop not evaluated when LHS is array binding element with OmittedExpression * expand widened type check * add more test cases * update code with suggestions * Make test target es2015 Co-authored-by: Andrew Branch <[email protected]>
1 parent 7baaf7b commit 3939b38

File tree

8 files changed

+109
-1
lines changed

8 files changed

+109
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37933,7 +37933,7 @@ namespace ts {
3793337933
// For a binding pattern, validate the initializer and exit
3793437934
if (isBindingPattern(node.name)) {
3793537935
const needCheckInitializer = node.initializer && node.parent.parent.kind !== SyntaxKind.ForInStatement;
37936-
const needCheckWidenedType = node.name.elements.length === 0;
37936+
const needCheckWidenedType = !some(node.name.elements, not(isOmittedExpression));
3793737937
if (needCheckInitializer || needCheckWidenedType) {
3793837938
// Don't validate for-in initializer as it is already an error
3793937939
const widenedType = getWidenedTypeForVariableLikeDeclaration(node);

test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
exports.main = void 0;
4+
function main() {
5+
for (var _i = 0, doesNotExist_1 = doesNotExist; _i < doesNotExist_1.length; _i++) {
6+
var _a = doesNotExist_1[_i];
7+
}
8+
}
9+
exports.main = main;

test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function main() {
2+
for (const [,] of doesNotExist) {
3+
}
4+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
tests/cases/compiler/omittedExpressionForOfLoop.ts(1,19): error TS2304: Cannot find name 'doesNotExist'.
2+
tests/cases/compiler/omittedExpressionForOfLoop.ts(4,19): error TS2532: Object is possibly 'undefined'.
3+
tests/cases/compiler/omittedExpressionForOfLoop.ts(7,12): error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
4+
tests/cases/compiler/omittedExpressionForOfLoop.ts(10,12): error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
5+
6+
7+
==== tests/cases/compiler/omittedExpressionForOfLoop.ts (4 errors) ====
8+
for (const [,] of doesNotExist) {
9+
~~~~~~~~~~~~
10+
!!! error TS2304: Cannot find name 'doesNotExist'.
11+
}
12+
13+
for (const [,] of undefined) {
14+
~~~~~~~~~
15+
!!! error TS2532: Object is possibly 'undefined'.
16+
}
17+
18+
for (const [,] of []) {
19+
~~~
20+
!!! error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
21+
}
22+
23+
for (const [] of []) {
24+
~~
25+
!!! error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
26+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [omittedExpressionForOfLoop.ts]
2+
for (const [,] of doesNotExist) {
3+
}
4+
5+
for (const [,] of undefined) {
6+
}
7+
8+
for (const [,] of []) {
9+
}
10+
11+
for (const [] of []) {
12+
}
13+
14+
//// [omittedExpressionForOfLoop.js]
15+
"use strict";
16+
for (const [,] of doesNotExist) {
17+
}
18+
for (const [,] of undefined) {
19+
}
20+
for (const [,] of []) {
21+
}
22+
for (const [] of []) {
23+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
=== tests/cases/compiler/omittedExpressionForOfLoop.ts ===
2+
for (const [,] of doesNotExist) {
3+
}
4+
5+
for (const [,] of undefined) {
6+
>undefined : Symbol(undefined)
7+
}
8+
9+
for (const [,] of []) {
10+
}
11+
12+
for (const [] of []) {
13+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/omittedExpressionForOfLoop.ts ===
2+
for (const [,] of doesNotExist) {
3+
> : undefined
4+
>doesNotExist : any
5+
}
6+
7+
for (const [,] of undefined) {
8+
> : undefined
9+
>undefined : undefined
10+
}
11+
12+
for (const [,] of []) {
13+
> : undefined
14+
>[] : never[]
15+
}
16+
17+
for (const [] of []) {
18+
>[] : never[]
19+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @strict: true
2+
// @target: es2015
3+
4+
for (const [,] of doesNotExist) {
5+
}
6+
7+
for (const [,] of undefined) {
8+
}
9+
10+
for (const [,] of []) {
11+
}
12+
13+
for (const [] of []) {
14+
}

0 commit comments

Comments
 (0)