Skip to content

Commit db1fa5c

Browse files
authored
Be more lenient about iteration when lib=es5 / noLib (#63070)
1 parent 3fc1f26 commit db1fa5c

File tree

71 files changed

+1890
-3318
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+1890
-3318
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45619,7 +45619,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
4561945619
return undefined;
4562045620
}
4562145621

45622-
const uplevelIteration = languageVersion >= ScriptTarget.ES2015;
45622+
const iterableExists = getGlobalIterableType(/*reportErrors*/ false) !== emptyGenericType;
45623+
const uplevelIteration = languageVersion >= ScriptTarget.ES2015 && iterableExists;
4562345624
const downlevelIteration = !uplevelIteration && compilerOptions.downlevelIteration;
4562445625
const possibleOutOfBounds = compilerOptions.noUncheckedIndexedAccess && !!(use & IterationUse.PossiblyOutOfBounds);
4562545626

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
arrayIterationLibES5TargetDifferent.ts(15,17): error TS2495: Type 'number' is not an array type or a string type.
2+
arrayIterationLibES5TargetDifferent.ts(21,17): error TS2495: Type '{ foo: string; }' is not an array type or a string type.
3+
4+
5+
==== arrayIterationLibES5TargetDifferent.ts (2 errors) ====
6+
declare function log(message?: any): void;
7+
8+
for (const x of [1, 2, 3]) {
9+
log(x);
10+
}
11+
12+
declare const aString: string;
13+
14+
for (const x of aString) {
15+
log(x);
16+
}
17+
18+
declare const aNumber: number;
19+
20+
for (const x of aNumber) {
21+
~~~~~~~
22+
!!! error TS2495: Type 'number' is not an array type or a string type.
23+
log(x);
24+
}
25+
26+
declare const anObject: { foo: string };
27+
28+
for (const x of anObject) {
29+
~~~~~~~~
30+
!!! error TS2495: Type '{ foo: string; }' is not an array type or a string type.
31+
log(x);
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//// [tests/cases/compiler/arrayIterationLibES5TargetDifferent.ts] ////
2+
3+
//// [arrayIterationLibES5TargetDifferent.ts]
4+
declare function log(message?: any): void;
5+
6+
for (const x of [1, 2, 3]) {
7+
log(x);
8+
}
9+
10+
declare const aString: string;
11+
12+
for (const x of aString) {
13+
log(x);
14+
}
15+
16+
declare const aNumber: number;
17+
18+
for (const x of aNumber) {
19+
log(x);
20+
}
21+
22+
declare const anObject: { foo: string };
23+
24+
for (const x of anObject) {
25+
log(x);
26+
}
27+
28+
//// [arrayIterationLibES5TargetDifferent.js]
29+
"use strict";
30+
for (const x of [1, 2, 3]) {
31+
log(x);
32+
}
33+
for (const x of aString) {
34+
log(x);
35+
}
36+
for (const x of aNumber) {
37+
log(x);
38+
}
39+
for (const x of anObject) {
40+
log(x);
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//// [tests/cases/compiler/arrayIterationLibES5TargetDifferent.ts] ////
2+
3+
=== arrayIterationLibES5TargetDifferent.ts ===
4+
declare function log(message?: any): void;
5+
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
6+
>message : Symbol(message, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 21))
7+
8+
for (const x of [1, 2, 3]) {
9+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 2, 10))
10+
11+
log(x);
12+
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
13+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 2, 10))
14+
}
15+
16+
declare const aString: string;
17+
>aString : Symbol(aString, Decl(arrayIterationLibES5TargetDifferent.ts, 6, 13))
18+
19+
for (const x of aString) {
20+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 8, 10))
21+
>aString : Symbol(aString, Decl(arrayIterationLibES5TargetDifferent.ts, 6, 13))
22+
23+
log(x);
24+
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
25+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 8, 10))
26+
}
27+
28+
declare const aNumber: number;
29+
>aNumber : Symbol(aNumber, Decl(arrayIterationLibES5TargetDifferent.ts, 12, 13))
30+
31+
for (const x of aNumber) {
32+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 14, 10))
33+
>aNumber : Symbol(aNumber, Decl(arrayIterationLibES5TargetDifferent.ts, 12, 13))
34+
35+
log(x);
36+
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
37+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 14, 10))
38+
}
39+
40+
declare const anObject: { foo: string };
41+
>anObject : Symbol(anObject, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 13))
42+
>foo : Symbol(foo, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 25))
43+
44+
for (const x of anObject) {
45+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 20, 10))
46+
>anObject : Symbol(anObject, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 13))
47+
48+
log(x);
49+
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
50+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 20, 10))
51+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//// [tests/cases/compiler/arrayIterationLibES5TargetDifferent.ts] ////
2+
3+
=== arrayIterationLibES5TargetDifferent.ts ===
4+
declare function log(message?: any): void;
5+
>log : (message?: any) => void
6+
> : ^ ^^^ ^^^^^
7+
>message : any
8+
> : ^^^
9+
10+
for (const x of [1, 2, 3]) {
11+
>x : number
12+
> : ^^^^^^
13+
>[1, 2, 3] : number[]
14+
> : ^^^^^^^^
15+
>1 : 1
16+
> : ^
17+
>2 : 2
18+
> : ^
19+
>3 : 3
20+
> : ^
21+
22+
log(x);
23+
>log(x) : void
24+
> : ^^^^
25+
>log : (message?: any) => void
26+
> : ^ ^^^ ^^^^^
27+
>x : number
28+
> : ^^^^^^
29+
}
30+
31+
declare const aString: string;
32+
>aString : string
33+
> : ^^^^^^
34+
35+
for (const x of aString) {
36+
>x : string
37+
> : ^^^^^^
38+
>aString : string
39+
> : ^^^^^^
40+
41+
log(x);
42+
>log(x) : void
43+
> : ^^^^
44+
>log : (message?: any) => void
45+
> : ^ ^^^ ^^^^^
46+
>x : string
47+
> : ^^^^^^
48+
}
49+
50+
declare const aNumber: number;
51+
>aNumber : number
52+
> : ^^^^^^
53+
54+
for (const x of aNumber) {
55+
>x : any
56+
> : ^^^
57+
>aNumber : number
58+
> : ^^^^^^
59+
60+
log(x);
61+
>log(x) : void
62+
> : ^^^^
63+
>log : (message?: any) => void
64+
> : ^ ^^^ ^^^^^
65+
>x : any
66+
> : ^^^
67+
}
68+
69+
declare const anObject: { foo: string };
70+
>anObject : { foo: string; }
71+
> : ^^^^^^^ ^^^
72+
>foo : string
73+
> : ^^^^^^
74+
75+
for (const x of anObject) {
76+
>x : any
77+
> : ^^^
78+
>anObject : { foo: string; }
79+
> : ^^^^^^^ ^^^
80+
81+
log(x);
82+
>log(x) : void
83+
> : ^^^^
84+
>log : (message?: any) => void
85+
> : ^ ^^^ ^^^^^
86+
>x : any
87+
> : ^^^
88+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
arrayIterationLibES5TargetDifferent.ts(15,17): error TS2495: Type 'number' is not an array type or a string type.
2+
arrayIterationLibES5TargetDifferent.ts(21,17): error TS2495: Type '{ foo: string; }' is not an array type or a string type.
3+
4+
5+
==== arrayIterationLibES5TargetDifferent.ts (2 errors) ====
6+
declare function log(message?: any): void;
7+
8+
for (const x of [1, 2, 3]) {
9+
log(x);
10+
}
11+
12+
declare const aString: string;
13+
14+
for (const x of aString) {
15+
log(x);
16+
}
17+
18+
declare const aNumber: number;
19+
20+
for (const x of aNumber) {
21+
~~~~~~~
22+
!!! error TS2495: Type 'number' is not an array type or a string type.
23+
log(x);
24+
}
25+
26+
declare const anObject: { foo: string };
27+
28+
for (const x of anObject) {
29+
~~~~~~~~
30+
!!! error TS2495: Type '{ foo: string; }' is not an array type or a string type.
31+
log(x);
32+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//// [tests/cases/compiler/arrayIterationLibES5TargetDifferent.ts] ////
2+
3+
//// [arrayIterationLibES5TargetDifferent.ts]
4+
declare function log(message?: any): void;
5+
6+
for (const x of [1, 2, 3]) {
7+
log(x);
8+
}
9+
10+
declare const aString: string;
11+
12+
for (const x of aString) {
13+
log(x);
14+
}
15+
16+
declare const aNumber: number;
17+
18+
for (const x of aNumber) {
19+
log(x);
20+
}
21+
22+
declare const anObject: { foo: string };
23+
24+
for (const x of anObject) {
25+
log(x);
26+
}
27+
28+
//// [arrayIterationLibES5TargetDifferent.js]
29+
"use strict";
30+
for (var _i = 0, _a = [1, 2, 3]; _i < _a.length; _i++) {
31+
var x = _a[_i];
32+
log(x);
33+
}
34+
for (var _b = 0, aString_1 = aString; _b < aString_1.length; _b++) {
35+
var x = aString_1[_b];
36+
log(x);
37+
}
38+
for (var _c = 0, aNumber_1 = aNumber; _c < aNumber_1.length; _c++) {
39+
var x = aNumber_1[_c];
40+
log(x);
41+
}
42+
for (var _d = 0, anObject_1 = anObject; _d < anObject_1.length; _d++) {
43+
var x = anObject_1[_d];
44+
log(x);
45+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//// [tests/cases/compiler/arrayIterationLibES5TargetDifferent.ts] ////
2+
3+
=== arrayIterationLibES5TargetDifferent.ts ===
4+
declare function log(message?: any): void;
5+
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
6+
>message : Symbol(message, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 21))
7+
8+
for (const x of [1, 2, 3]) {
9+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 2, 10))
10+
11+
log(x);
12+
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
13+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 2, 10))
14+
}
15+
16+
declare const aString: string;
17+
>aString : Symbol(aString, Decl(arrayIterationLibES5TargetDifferent.ts, 6, 13))
18+
19+
for (const x of aString) {
20+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 8, 10))
21+
>aString : Symbol(aString, Decl(arrayIterationLibES5TargetDifferent.ts, 6, 13))
22+
23+
log(x);
24+
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
25+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 8, 10))
26+
}
27+
28+
declare const aNumber: number;
29+
>aNumber : Symbol(aNumber, Decl(arrayIterationLibES5TargetDifferent.ts, 12, 13))
30+
31+
for (const x of aNumber) {
32+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 14, 10))
33+
>aNumber : Symbol(aNumber, Decl(arrayIterationLibES5TargetDifferent.ts, 12, 13))
34+
35+
log(x);
36+
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
37+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 14, 10))
38+
}
39+
40+
declare const anObject: { foo: string };
41+
>anObject : Symbol(anObject, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 13))
42+
>foo : Symbol(foo, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 25))
43+
44+
for (const x of anObject) {
45+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 20, 10))
46+
>anObject : Symbol(anObject, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 13))
47+
48+
log(x);
49+
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
50+
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 20, 10))
51+
}

0 commit comments

Comments
 (0)