Skip to content

Commit 65bb78d

Browse files
authored
Merge pull request #12808 from Microsoft/self-referencing-spread-recursive-loop
Self-referencing spread recursive loop
2 parents 496a14a + 809706b commit 65bb78d

File tree

4 files changed

+33
-7
lines changed

4 files changed

+33
-7
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11320,13 +11320,7 @@ namespace ts {
1132011320
}
1132111321

1132211322
function checkSpreadExpression(node: SpreadElement, contextualMapper?: TypeMapper): Type {
11323-
// It is usually not safe to call checkExpressionCached if we can be contextually typing.
11324-
// You can tell that we are contextually typing because of the contextualMapper parameter.
11325-
// While it is true that a spread element can have a contextual type, it does not do anything
11326-
// with this type. It is neither affected by it, nor does it propagate it to its operand.
11327-
// So the fact that contextualMapper is passed is not important, because the operand of a spread
11328-
// element is not contextually typed.
11329-
const arrayOrIterableType = checkExpressionCached(node.expression, contextualMapper);
11323+
const arrayOrIterableType = checkExpression(node.expression, contextualMapper);
1133011324
return checkIteratedTypeOrElementType(arrayOrIterableType, node.expression, /*allowStringInput*/ false);
1133111325
}
1133211326

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/compiler/selfReferencingSpreadInLoop.ts(1,5): error TS7034: Variable 'additional' implicitly has type 'any[]' in some locations where its type cannot be determined.
2+
tests/cases/compiler/selfReferencingSpreadInLoop.ts(3,22): error TS7005: Variable 'additional' implicitly has an 'any[]' type.
3+
4+
5+
==== tests/cases/compiler/selfReferencingSpreadInLoop.ts (2 errors) ====
6+
let additional = [];
7+
~~~~~~~~~~
8+
!!! error TS7034: Variable 'additional' implicitly has type 'any[]' in some locations where its type cannot be determined.
9+
for (const subcomponent of [1, 2, 3]) {
10+
additional = [...additional, subcomponent];
11+
~~~~~~~~~~
12+
!!! error TS7005: Variable 'additional' implicitly has an 'any[]' type.
13+
}
14+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//// [selfReferencingSpreadInLoop.ts]
2+
let additional = [];
3+
for (const subcomponent of [1, 2, 3]) {
4+
additional = [...additional, subcomponent];
5+
}
6+
7+
8+
//// [selfReferencingSpreadInLoop.js]
9+
var additional = [];
10+
for (var _i = 0, _a = [1, 2, 3]; _i < _a.length; _i++) {
11+
var subcomponent = _a[_i];
12+
additional = additional.concat([subcomponent]);
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// @noImplicitAny: true
2+
let additional = [];
3+
for (const subcomponent of [1, 2, 3]) {
4+
additional = [...additional, subcomponent];
5+
}

0 commit comments

Comments
 (0)