Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25480,7 +25480,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {

function isArrayLikeType(type: Type): boolean {
// A type is array-like if it is a reference to the global Array or global ReadonlyArray type,
// or if it is not the undefined or null type and if it is assignable to ReadonlyArray<any>
// or if it is not the undefined or null type and if it is assignable to ReadonlyArray<any>.
// When Array doesn't exist (noLib), we can't determine array-likeness.
if (anyReadonlyArrayType === emptyObjectType) {
return false;
}
Comment thread
jakebailey marked this conversation as resolved.
Outdated
return isArrayType(type) || !(type.flags & TypeFlags.Nullable) && isTypeAssignableTo(type, anyReadonlyArrayType);
}

Expand Down Expand Up @@ -45619,7 +45623,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return undefined;
}

const uplevelIteration = languageVersion >= ScriptTarget.ES2015;
const iterableExists = getGlobalIterableType(/*reportErrors*/ false) !== emptyGenericType;
// Only use iteration semantics when Iterable exists; otherwise fall through to array-like handling.
const uplevelIteration = languageVersion >= ScriptTarget.ES2015 && iterableExists;
Comment thread
jakebailey marked this conversation as resolved.
const downlevelIteration = !uplevelIteration && compilerOptions.downlevelIteration;
const possibleOutOfBounds = compilerOptions.noUncheckedIndexedAccess && !!(use & IterationUse.PossiblyOutOfBounds);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
arrayIterationLibES5TargetDifferent.ts(15,17): error TS2495: Type 'number' is not an array type or a string type.
arrayIterationLibES5TargetDifferent.ts(21,17): error TS2495: Type '{ foo: string; }' is not an array type or a string type.


==== arrayIterationLibES5TargetDifferent.ts (2 errors) ====
declare function log(message?: any): void;

for (const x of [1, 2, 3]) {
log(x);
}

declare const aString: string;

for (const x of aString) {
log(x);
}

declare const aNumber: number;

for (const x of aNumber) {
~~~~~~~
!!! error TS2495: Type 'number' is not an array type or a string type.
log(x);
}

declare const anObject: { foo: string };

for (const x of anObject) {
~~~~~~~~
!!! error TS2495: Type '{ foo: string; }' is not an array type or a string type.
log(x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [tests/cases/compiler/arrayIterationLibES5TargetDifferent.ts] ////

//// [arrayIterationLibES5TargetDifferent.ts]
declare function log(message?: any): void;

for (const x of [1, 2, 3]) {
log(x);
}

declare const aString: string;

for (const x of aString) {
log(x);
}

declare const aNumber: number;

for (const x of aNumber) {
log(x);
}

declare const anObject: { foo: string };

for (const x of anObject) {
log(x);
}

//// [arrayIterationLibES5TargetDifferent.js]
"use strict";
for (const x of [1, 2, 3]) {
log(x);
}
for (const x of aString) {
log(x);
}
for (const x of aNumber) {
log(x);
}
for (const x of anObject) {
log(x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//// [tests/cases/compiler/arrayIterationLibES5TargetDifferent.ts] ////

=== arrayIterationLibES5TargetDifferent.ts ===
declare function log(message?: any): void;
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
>message : Symbol(message, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 21))

for (const x of [1, 2, 3]) {
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 2, 10))

log(x);
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 2, 10))
}

declare const aString: string;
>aString : Symbol(aString, Decl(arrayIterationLibES5TargetDifferent.ts, 6, 13))

for (const x of aString) {
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 8, 10))
>aString : Symbol(aString, Decl(arrayIterationLibES5TargetDifferent.ts, 6, 13))

log(x);
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 8, 10))
}

declare const aNumber: number;
>aNumber : Symbol(aNumber, Decl(arrayIterationLibES5TargetDifferent.ts, 12, 13))

for (const x of aNumber) {
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 14, 10))
>aNumber : Symbol(aNumber, Decl(arrayIterationLibES5TargetDifferent.ts, 12, 13))

log(x);
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 14, 10))
}

declare const anObject: { foo: string };
>anObject : Symbol(anObject, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 13))
>foo : Symbol(foo, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 25))

for (const x of anObject) {
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 20, 10))
>anObject : Symbol(anObject, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 13))

log(x);
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 20, 10))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//// [tests/cases/compiler/arrayIterationLibES5TargetDifferent.ts] ////

=== arrayIterationLibES5TargetDifferent.ts ===
declare function log(message?: any): void;
>log : (message?: any) => void
> : ^ ^^^ ^^^^^
>message : any
> : ^^^

for (const x of [1, 2, 3]) {
>x : number
> : ^^^^^^
>[1, 2, 3] : number[]
> : ^^^^^^^^
>1 : 1
> : ^
>2 : 2
> : ^
>3 : 3
> : ^

log(x);
>log(x) : void
> : ^^^^
>log : (message?: any) => void
> : ^ ^^^ ^^^^^
>x : number
> : ^^^^^^
}

declare const aString: string;
>aString : string
> : ^^^^^^

for (const x of aString) {
>x : string
> : ^^^^^^
>aString : string
> : ^^^^^^

log(x);
>log(x) : void
> : ^^^^
>log : (message?: any) => void
> : ^ ^^^ ^^^^^
>x : string
> : ^^^^^^
}

declare const aNumber: number;
>aNumber : number
> : ^^^^^^

for (const x of aNumber) {
>x : any
> : ^^^
>aNumber : number
> : ^^^^^^

log(x);
>log(x) : void
> : ^^^^
>log : (message?: any) => void
> : ^ ^^^ ^^^^^
>x : any
> : ^^^
}

declare const anObject: { foo: string };
>anObject : { foo: string; }
> : ^^^^^^^ ^^^
>foo : string
> : ^^^^^^

for (const x of anObject) {
>x : any
> : ^^^
>anObject : { foo: string; }
> : ^^^^^^^ ^^^

log(x);
>log(x) : void
> : ^^^^
>log : (message?: any) => void
> : ^ ^^^ ^^^^^
>x : any
> : ^^^
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
arrayIterationLibES5TargetDifferent.ts(15,17): error TS2495: Type 'number' is not an array type or a string type.
arrayIterationLibES5TargetDifferent.ts(21,17): error TS2495: Type '{ foo: string; }' is not an array type or a string type.


==== arrayIterationLibES5TargetDifferent.ts (2 errors) ====
declare function log(message?: any): void;

for (const x of [1, 2, 3]) {
log(x);
}

declare const aString: string;

for (const x of aString) {
log(x);
}

declare const aNumber: number;

for (const x of aNumber) {
~~~~~~~
!!! error TS2495: Type 'number' is not an array type or a string type.
log(x);
}

declare const anObject: { foo: string };

for (const x of anObject) {
~~~~~~~~
!!! error TS2495: Type '{ foo: string; }' is not an array type or a string type.
log(x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//// [tests/cases/compiler/arrayIterationLibES5TargetDifferent.ts] ////

//// [arrayIterationLibES5TargetDifferent.ts]
declare function log(message?: any): void;

for (const x of [1, 2, 3]) {
log(x);
}

declare const aString: string;

for (const x of aString) {
log(x);
}

declare const aNumber: number;

for (const x of aNumber) {
log(x);
}

declare const anObject: { foo: string };

for (const x of anObject) {
log(x);
}

//// [arrayIterationLibES5TargetDifferent.js]
"use strict";
for (var _i = 0, _a = [1, 2, 3]; _i < _a.length; _i++) {
var x = _a[_i];
log(x);
}
for (var _b = 0, aString_1 = aString; _b < aString_1.length; _b++) {
var x = aString_1[_b];
log(x);
}
for (var _c = 0, aNumber_1 = aNumber; _c < aNumber_1.length; _c++) {
var x = aNumber_1[_c];
log(x);
}
for (var _d = 0, anObject_1 = anObject; _d < anObject_1.length; _d++) {
var x = anObject_1[_d];
log(x);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//// [tests/cases/compiler/arrayIterationLibES5TargetDifferent.ts] ////

=== arrayIterationLibES5TargetDifferent.ts ===
declare function log(message?: any): void;
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
>message : Symbol(message, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 21))

for (const x of [1, 2, 3]) {
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 2, 10))

log(x);
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 2, 10))
}

declare const aString: string;
>aString : Symbol(aString, Decl(arrayIterationLibES5TargetDifferent.ts, 6, 13))

for (const x of aString) {
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 8, 10))
>aString : Symbol(aString, Decl(arrayIterationLibES5TargetDifferent.ts, 6, 13))

log(x);
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 8, 10))
}

declare const aNumber: number;
>aNumber : Symbol(aNumber, Decl(arrayIterationLibES5TargetDifferent.ts, 12, 13))

for (const x of aNumber) {
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 14, 10))
>aNumber : Symbol(aNumber, Decl(arrayIterationLibES5TargetDifferent.ts, 12, 13))

log(x);
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 14, 10))
}

declare const anObject: { foo: string };
>anObject : Symbol(anObject, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 13))
>foo : Symbol(foo, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 25))

for (const x of anObject) {
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 20, 10))
>anObject : Symbol(anObject, Decl(arrayIterationLibES5TargetDifferent.ts, 18, 13))

log(x);
>log : Symbol(log, Decl(arrayIterationLibES5TargetDifferent.ts, 0, 0))
>x : Symbol(x, Decl(arrayIterationLibES5TargetDifferent.ts, 20, 10))
}
Loading
Loading