Skip to content

Try not allowing string iteration #59683

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/lib/es2015.iterable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ interface StringIterator<T> extends IteratorObject<T, BuiltinIteratorReturn, unk

interface String {
/** Iterator */
[Symbol.iterator](): StringIterator<string>;
// [Symbol.iterator](): StringIterator<string>;
}

interface Int8Array {
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/ES5For-ofTypeCheck14.errors.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
ES5For-ofTypeCheck14.ts(2,17): error TS2802: Type 'Set<number>' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.
ES5For-ofTypeCheck14.ts(2,17): error TS2461: Type 'Set<number>' is not an array type.


==== ES5For-ofTypeCheck14.ts (1 errors) ====
var union: string | Set<number>
for (const e of union) { }
~~~~~
!!! error TS2802: Type 'Set<number>' can only be iterated through when using the '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.
!!! error TS2461: Type 'Set<number>' is not an array type.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
argumentsSpreadRestIterables.tsx(8,21): error TS2488: Type '"hello"' must have a '[Symbol.iterator]()' method that returns an iterator.
argumentsSpreadRestIterables.tsx(10,27): error TS2488: Type '"hello"' must have a '[Symbol.iterator]()' method that returns an iterator.
argumentsSpreadRestIterables.tsx(15,19): error TS2488: Type '"hello"' must have a '[Symbol.iterator]()' method that returns an iterator.
argumentsSpreadRestIterables.tsx(17,25): error TS2488: Type '"hello"' must have a '[Symbol.iterator]()' method that returns an iterator.


==== argumentsSpreadRestIterables.tsx (4 errors) ====
declare const itNum: Iterable<number>

;(function(...rest) {})(...itNum)
;(function(a, ...rest) {})('', ...itNum)
;(function(a, ...rest) {})('', true, ...itNum)

declare function fn1<const T extends readonly unknown[]>(...args: T): T;
const res1 = fn1(..."hello");
~~~~~~~
!!! error TS2488: Type '"hello"' must have a '[Symbol.iterator]()' method that returns an iterator.
const res2 = fn1(...itNum);
const res3 = fn1(true, ..."hello");
~~~~~~~
!!! error TS2488: Type '"hello"' must have a '[Symbol.iterator]()' method that returns an iterator.
const res4 = fn1(true, ...itNum);

// repro from #52781
declare function foo<T extends unknown[]>(...args: T): T;
const p1 = foo(..."hello");
~~~~~~~
!!! error TS2488: Type '"hello"' must have a '[Symbol.iterator]()' method that returns an iterator.
const p2 = foo(...itNum);
const p3 = foo(true, ..."hello");
~~~~~~~
!!! error TS2488: Type '"hello"' must have a '[Symbol.iterator]()' method that returns an iterator.
const p4 = foo(true, ...itNum);

Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ declare function fn1<const T extends readonly unknown[]>(...args: T): T;
> : ^

const res1 = fn1(..."hello");
>res1 : readonly string[]
> : ^^^^^^^^^^^^^^^^^
>fn1(..."hello") : readonly string[]
> : ^^^^^^^^^^^^^^^^^
>res1 : readonly any[]
> : ^^^^^^^^^^^^^^
>fn1(..."hello") : readonly any[]
> : ^^^^^^^^^^^^^^
>fn1 : <const T extends readonly unknown[]>(...args: T) => T
> : ^^^^^^^ ^^^^^^^^^ ^^^^^ ^^ ^^^^^
>..."hello" : string
> : ^^^^^^
>..."hello" : any
> : ^^^
>"hello" : "hello"
> : ^^^^^^^

Expand All @@ -88,16 +88,16 @@ const res2 = fn1(...itNum);
> : ^^^^^^^^^^^^^^^^

const res3 = fn1(true, ..."hello");
>res3 : readonly [true, ...string[]]
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>fn1(true, ..."hello") : readonly [true, ...string[]]
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>res3 : readonly [true, ...any[]]
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
>fn1(true, ..."hello") : readonly [true, ...any[]]
> : ^^^^^^^^^^^^^^^^^^^^^^^^^
>fn1 : <const T extends readonly unknown[]>(...args: T) => T
> : ^^^^^^^ ^^^^^^^^^ ^^^^^ ^^ ^^^^^
>true : true
> : ^^^^
>..."hello" : string
> : ^^^^^^
>..."hello" : any
> : ^^^
>"hello" : "hello"
> : ^^^^^^^

Expand All @@ -123,14 +123,14 @@ declare function foo<T extends unknown[]>(...args: T): T;
> : ^

const p1 = foo(..."hello");
>p1 : string[]
> : ^^^^^^^^
>foo(..."hello") : string[]
> : ^^^^^^^^
>p1 : any[]
> : ^^^^^
>foo(..."hello") : any[]
> : ^^^^^
>foo : <T extends unknown[]>(...args: T) => T
> : ^ ^^^^^^^^^ ^^^^^ ^^ ^^^^^
>..."hello" : string
> : ^^^^^^
>..."hello" : any
> : ^^^
>"hello" : "hello"
> : ^^^^^^^

Expand All @@ -147,16 +147,16 @@ const p2 = foo(...itNum);
> : ^^^^^^^^^^^^^^^^

const p3 = foo(true, ..."hello");
>p3 : [boolean, ...string[]]
> : ^^^^^^^^^^^^^^^^^^^^^^
>foo(true, ..."hello") : [boolean, ...string[]]
> : ^^^^^^^^^^^^^^^^^^^^^^
>p3 : [boolean, ...any[]]
> : ^^^^^^^^^^^^^^^^^^^
>foo(true, ..."hello") : [boolean, ...any[]]
> : ^^^^^^^^^^^^^^^^^^^
>foo : <T extends unknown[]>(...args: T) => T
> : ^ ^^^^^^^^^ ^^^^^ ^^ ^^^^^
>true : true
> : ^^^^
>..."hello" : string
> : ^^^^^^
>..."hello" : any
> : ^^^
>"hello" : "hello"
> : ^^^^^^^

Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/for-of9.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
for-of9.ts(3,11): error TS2488: Type '"hello"' must have a '[Symbol.iterator]()' method that returns an iterator.


==== for-of9.ts (1 errors) ====
var v: string;
for (v of ["hello"]) { }
for (v of "hello") { }
~~~~~~~
!!! error TS2488: Type '"hello"' must have a '[Symbol.iterator]()' method that returns an iterator.
4 changes: 2 additions & 2 deletions tests/baselines/reference/iterableArrayPattern8.types
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ var a: Bar, b: string;
> : ^^^^^^^^^^^^^^^^^^
>a : Bar
> : ^^^
>...b : string
> : ^^^^^^
>...b : any
> : ^^^
>b : string
> : ^^^^^^
>new FooIterator : FooIterator
Expand Down
16 changes: 16 additions & 0 deletions tests/baselines/reference/mapGroupBy.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
mapGroupBy.ts(3,27): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Iterable<unknown>'.


==== mapGroupBy.ts (1 errors) ====
const basic = Map.groupBy([0, 2, 8], x => x < 5 ? 'small' : 'large');

const chars = Map.groupBy('a string', c => c);
~~~~~~~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'Iterable<unknown>'.

type Employee = { name: string, role: 'ic' | 'manager' }
const employees: Set<Employee> = new Set();
const byRole = Map.groupBy(employees, x => x.role);

const byNonKey = Map.groupBy(employees, x => x);

20 changes: 10 additions & 10 deletions tests/baselines/reference/mapGroupBy.types
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ const basic = Map.groupBy([0, 2, 8], x => x < 5 ? 'small' : 'large');
> : ^^^^^^^

const chars = Map.groupBy('a string', c => c);
>chars : Map<string, string[]>
> : ^^^^^^^^^^^^^^^^^^^^^
>Map.groupBy('a string', c => c) : Map<string, string[]>
> : ^^^^^^^^^^^^^^^^^^^^^
>chars : Map<unknown, unknown[]>
> : ^^^^^^^^^^^^^^^^^^^^^^^
>Map.groupBy('a string', c => c) : Map<unknown, unknown[]>
> : ^^^^^^^^^^^^^^^^^^^^^^^
>Map.groupBy : <K, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K) => Map<K, T[]>
> : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^
>Map : MapConstructor
Expand All @@ -54,12 +54,12 @@ const chars = Map.groupBy('a string', c => c);
> : ^ ^^ ^^ ^^ ^^ ^^ ^^^^^
>'a string' : "a string"
> : ^^^^^^^^^^
>c => c : (c: string) => string
> : ^ ^^^^^^^^^^^^^^^^^^^
>c : string
> : ^^^^^^
>c : string
> : ^^^^^^
>c => c : (c: unknown) => unknown
> : ^ ^^^^^^^^^^^^^^^^^^^^^
>c : unknown
> : ^^^^^^^
>c : unknown
> : ^^^^^^^

type Employee = { name: string, role: 'ic' | 'manager' }
>Employee : Employee
Expand Down
36 changes: 18 additions & 18 deletions tests/baselines/reference/mappedTypeRecursiveInference.errors.txt

Large diffs are not rendered by default.

48 changes: 24 additions & 24 deletions tests/baselines/reference/mappedTypeRecursiveInference.types

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion tests/baselines/reference/objectGroupBy.errors.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
objectGroupBy.ts(3,30): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Iterable<unknown>'.
objectGroupBy.ts(9,49): error TS2322: Type 'Employee' is not assignable to type 'PropertyKey'.


==== objectGroupBy.ts (1 errors) ====
==== objectGroupBy.ts (2 errors) ====
const basic = Object.groupBy([0, 2, 8], x => x < 5 ? 'small' : 'large');

const chars = Object.groupBy('a string', c => c);
~~~~~~~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'Iterable<unknown>'.

type Employee = { name: string, role: 'ic' | 'manager' }
const employees: Set<Employee> = new Set();
Expand Down
20 changes: 10 additions & 10 deletions tests/baselines/reference/objectGroupBy.types
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ const basic = Object.groupBy([0, 2, 8], x => x < 5 ? 'small' : 'large');
> : ^^^^^^^

const chars = Object.groupBy('a string', c => c);
>chars : Partial<Record<string, string[]>>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>Object.groupBy('a string', c => c) : Partial<Record<string, string[]>>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>chars : Partial<Record<PropertyKey, unknown[]>>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>Object.groupBy('a string', c => c) : Partial<Record<PropertyKey, unknown[]>>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>Object.groupBy : <K extends PropertyKey, T>(items: Iterable<T>, keySelector: (item: T, index: number) => K) => Partial<Record<K, T[]>>
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^ ^^^^^
>Object : ObjectConstructor
Expand All @@ -54,12 +54,12 @@ const chars = Object.groupBy('a string', c => c);
> : ^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^ ^^^^^
>'a string' : "a string"
> : ^^^^^^^^^^
>c => c : (c: string) => string
> : ^ ^^^^^^^^^^^^^^^^^^^
>c : string
> : ^^^^^^
>c : string
> : ^^^^^^
>c => c : (c: unknown) => unknown
> : ^ ^^^^^^^^^^^^^^^^^^^^^
>c : unknown
> : ^^^^^^^
>c : unknown
> : ^^^^^^^

type Employee = { name: string, role: 'ic' | 'manager' }
>Employee : Employee
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
typeArgumentInferenceApparentType1.ts(5,26): error TS2345: Argument of type 'string' is not assignable to parameter of type 'Iterable<string>'.


==== typeArgumentInferenceApparentType1.ts (1 errors) ====
function method<T>(iterable: Iterable<T>): T {
return;
}

var res: string = method("test");
~~~~~~
!!! error TS2345: Argument of type 'string' is not assignable to parameter of type 'Iterable<string>'.
Loading