mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-10 18:04:18 -05:00
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 <andrew@wheream.io>
This commit is contained in:
@@ -37933,7 +37933,7 @@ namespace ts {
|
||||
// For a binding pattern, validate the initializer and exit
|
||||
if (isBindingPattern(node.name)) {
|
||||
const needCheckInitializer = node.initializer && node.parent.parent.kind !== SyntaxKind.ForInStatement;
|
||||
const needCheckWidenedType = node.name.elements.length === 0;
|
||||
const needCheckWidenedType = !some(node.name.elements, not(isOmittedExpression));
|
||||
if (needCheckInitializer || needCheckWidenedType) {
|
||||
// Don't validate for-in initializer as it is already an error
|
||||
const widenedType = getWidenedTypeForVariableLikeDeclaration(node);
|
||||
|
||||
9
test.js
Normal file
9
test.js
Normal file
@@ -0,0 +1,9 @@
|
||||
"use strict";
|
||||
exports.__esModule = true;
|
||||
exports.main = void 0;
|
||||
function main() {
|
||||
for (var _i = 0, doesNotExist_1 = doesNotExist; _i < doesNotExist_1.length; _i++) {
|
||||
var _a = doesNotExist_1[_i];
|
||||
}
|
||||
}
|
||||
exports.main = main;
|
||||
4
test.ts
Normal file
4
test.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export function main() {
|
||||
for (const [,] of doesNotExist) {
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
tests/cases/compiler/omittedExpressionForOfLoop.ts(1,19): error TS2304: Cannot find name 'doesNotExist'.
|
||||
tests/cases/compiler/omittedExpressionForOfLoop.ts(4,19): error TS2532: Object is possibly 'undefined'.
|
||||
tests/cases/compiler/omittedExpressionForOfLoop.ts(7,12): error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
|
||||
tests/cases/compiler/omittedExpressionForOfLoop.ts(10,12): error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
|
||||
|
||||
|
||||
==== tests/cases/compiler/omittedExpressionForOfLoop.ts (4 errors) ====
|
||||
for (const [,] of doesNotExist) {
|
||||
~~~~~~~~~~~~
|
||||
!!! error TS2304: Cannot find name 'doesNotExist'.
|
||||
}
|
||||
|
||||
for (const [,] of undefined) {
|
||||
~~~~~~~~~
|
||||
!!! error TS2532: Object is possibly 'undefined'.
|
||||
}
|
||||
|
||||
for (const [,] of []) {
|
||||
~~~
|
||||
!!! error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
|
||||
}
|
||||
|
||||
for (const [] of []) {
|
||||
~~
|
||||
!!! error TS2488: Type 'never' must have a '[Symbol.iterator]()' method that returns an iterator.
|
||||
}
|
||||
23
tests/baselines/reference/omittedExpressionForOfLoop.js
Normal file
23
tests/baselines/reference/omittedExpressionForOfLoop.js
Normal file
@@ -0,0 +1,23 @@
|
||||
//// [omittedExpressionForOfLoop.ts]
|
||||
for (const [,] of doesNotExist) {
|
||||
}
|
||||
|
||||
for (const [,] of undefined) {
|
||||
}
|
||||
|
||||
for (const [,] of []) {
|
||||
}
|
||||
|
||||
for (const [] of []) {
|
||||
}
|
||||
|
||||
//// [omittedExpressionForOfLoop.js]
|
||||
"use strict";
|
||||
for (const [,] of doesNotExist) {
|
||||
}
|
||||
for (const [,] of undefined) {
|
||||
}
|
||||
for (const [,] of []) {
|
||||
}
|
||||
for (const [] of []) {
|
||||
}
|
||||
13
tests/baselines/reference/omittedExpressionForOfLoop.symbols
Normal file
13
tests/baselines/reference/omittedExpressionForOfLoop.symbols
Normal file
@@ -0,0 +1,13 @@
|
||||
=== tests/cases/compiler/omittedExpressionForOfLoop.ts ===
|
||||
for (const [,] of doesNotExist) {
|
||||
}
|
||||
|
||||
for (const [,] of undefined) {
|
||||
>undefined : Symbol(undefined)
|
||||
}
|
||||
|
||||
for (const [,] of []) {
|
||||
}
|
||||
|
||||
for (const [] of []) {
|
||||
}
|
||||
19
tests/baselines/reference/omittedExpressionForOfLoop.types
Normal file
19
tests/baselines/reference/omittedExpressionForOfLoop.types
Normal file
@@ -0,0 +1,19 @@
|
||||
=== tests/cases/compiler/omittedExpressionForOfLoop.ts ===
|
||||
for (const [,] of doesNotExist) {
|
||||
> : undefined
|
||||
>doesNotExist : any
|
||||
}
|
||||
|
||||
for (const [,] of undefined) {
|
||||
> : undefined
|
||||
>undefined : undefined
|
||||
}
|
||||
|
||||
for (const [,] of []) {
|
||||
> : undefined
|
||||
>[] : never[]
|
||||
}
|
||||
|
||||
for (const [] of []) {
|
||||
>[] : never[]
|
||||
}
|
||||
14
tests/cases/compiler/omittedExpressionForOfLoop.ts
Normal file
14
tests/cases/compiler/omittedExpressionForOfLoop.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
// @strict: true
|
||||
// @target: es2015
|
||||
|
||||
for (const [,] of doesNotExist) {
|
||||
}
|
||||
|
||||
for (const [,] of undefined) {
|
||||
}
|
||||
|
||||
for (const [,] of []) {
|
||||
}
|
||||
|
||||
for (const [] of []) {
|
||||
}
|
||||
Reference in New Issue
Block a user