-
Notifications
You must be signed in to change notification settings - Fork 13.4k
add types for iterator helpers proposal #58222
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
bd51b00
add types for iterator helpers proposal
bakkot 075ce43
run baselines
bakkot 4449c82
format
bakkot 30d9bc2
Revert "add types for iterator helpers proposal"
bakkot f5393db
add BuiltinIterator and switch existing IterableIterator uses to that
bakkot ebc4e71
add AsyncBuiltinIterator and switch existing AsyncIterableIterator us…
bakkot e748158
rebaseline
bakkot c4a8a08
add types for iterator helpers
bakkot 51124ae
rebaseline
bakkot 48c5ac9
Merge branch 'main' into iterator-helpers
bakkot d8282a4
Merge branch 'main' into iterator-helpers
rbuckton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add types for iterator helpers
- Loading branch information
commit c4a8a08e5b96365f63dcca1a576ddc9292cabe7b
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /// <reference lib="es2015.iterable" /> | ||
| export {}; | ||
|
|
||
| // Abstract type that allows us to mark `next` as `abstract` | ||
| declare abstract class Iterator<T> { | ||
| abstract next(value?: undefined): IteratorResult<T, void>; | ||
| } | ||
|
|
||
| // Merge all members of `BuiltinIterator<T>` into `Iterator<T>` | ||
| interface Iterator<T> extends globalThis.BuiltinIterator<T, void, undefined> {} | ||
|
|
||
| // Capture the `Iterator` constructor in a type we can use in the `extends` clause of `IteratorConstructor`. | ||
| type BuiltinIteratorConstructor = typeof Iterator; | ||
|
|
||
| declare global { | ||
| // Global `BuiltinIterator<T>` interface that can be augmented by polyfills | ||
| interface BuiltinIterator<T, TReturn, TNext> { | ||
| /** | ||
| * Returns this iterator. | ||
| */ | ||
| [Symbol.iterator](): BuiltinIterator<T, TReturn, TNext>; | ||
|
|
||
| /** | ||
| * Creates an iterator whose values are the result of applying the callback to the values from this iterator. | ||
| * @param callbackfn A function that accepts up to two arguments to be used to transform values from the underlying iterator. | ||
| */ | ||
| map<U>(callbackfn: (value: T, index: number) => U): BuiltinIterator<U>; | ||
|
|
||
| /** | ||
| * Creates an iterator whose values are those from this iterator for which the provided predicate returns true. | ||
| * @param predicate A function that accepts up to two arguments to be used to test values from the underlying iterator. | ||
| */ | ||
| filter<S extends T>(predicate: (value: T, index: number) => value is S): BuiltinIterator<S>; | ||
|
|
||
| /** | ||
| * Creates an iterator whose values are those from this iterator for which the provided predicate returns true. | ||
| * @param predicate A function that accepts up to two arguments to be used to test values from the underlying iterator. | ||
| */ | ||
| filter(predicate: (value: T, index: number) => unknown): BuiltinIterator<T>; | ||
|
|
||
| /** | ||
| * Creates an iterator whose values are the values from this iterator, stopping once the provided limit is reached. | ||
| * @param limit The maximum number of values to yield. | ||
| */ | ||
| take(limit: number): BuiltinIterator<T>; | ||
|
|
||
| /** | ||
| * Creates an iterator whose values are the values from this iterator after skipping the provided count. | ||
| * @param count The number of values to drop. | ||
| */ | ||
| drop(count: number): BuiltinIterator<T>; | ||
|
|
||
| /** | ||
| * Creates an iterator whose values are the result of applying the callback to the values from this iterator and then flattening the resulting iterators or iterables. | ||
| * @param callback A function that accepts up to two arguments to be used to transform values from the underlying iterator into new iterators or iterables to be flattened into the result. | ||
| */ | ||
| flatMap<U>(callback: (value: T, index: number) => Iterator<U> | Iterable<U>): BuiltinIterator<U>; | ||
|
rbuckton marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** | ||
| * Calls the specified callback function for all the elements in this iterator. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. | ||
| * @param callbackfn A function that accepts up to three arguments. The reduce method calls the callbackfn function one time for each element in the iterator. | ||
| * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of a value from the iterator. | ||
| */ | ||
| reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number) => T): T; | ||
| reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number) => T, initialValue: T): T; | ||
|
|
||
| /** | ||
| * Calls the specified callback function for all the elements in this iterator. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. | ||
| * @param callbackfn A function that accepts up to three arguments. The reduce method calls the callbackfn function one time for each element in the iterator. | ||
| * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of a value from the iterator. | ||
| */ | ||
| reduce<U>(callbackfn: (previousValue: U, currentValue: T, currentIndex: number) => U, initialValue: U): U; | ||
|
|
||
| /** | ||
| * Creates a new array from the values yielded by this iterator. | ||
| */ | ||
| toArray(): Array<T>; | ||
|
|
||
| /** | ||
| * Performs the specified action for each element in the iterator. | ||
| * @param callbackfn A function that accepts up to two arguments. forEach calls the callbackfn function one time for each element in the iterator. | ||
| */ | ||
| forEach(callbackfn: (value: T, index: number) => void): void; | ||
|
|
||
| /** | ||
| * Determines whether the specified callback function returns true for any element of this iterator. | ||
| * @param predicate A function that accepts up to two arguments. The some method calls | ||
| * the predicate function for each element in this iterator until the predicate returns a value | ||
| * true, or until the end of the iterator. | ||
| */ | ||
| some(predicate: (value: T, index: number) => unknown): boolean; | ||
|
|
||
| /** | ||
| * Determines whether all the members of this iterator satisfy the specified test. | ||
| * @param predicate A function that accepts up to two arguments. The every method calls | ||
| * the predicate function for each element in this iterator until the predicate returns | ||
| * false, or until the end of this iterator. | ||
| */ | ||
| every(predicate: (value: T, index: number) => unknown): boolean; | ||
|
|
||
| /** | ||
| * Returns the value of the first element in this iterator where predicate is true, and undefined | ||
| * otherwise. | ||
| * @param predicate find calls predicate once for each element of this iterator, in | ||
| * order, until it finds one where predicate returns true. If such an element is found, find | ||
| * immediately returns that element value. Otherwise, find returns undefined. | ||
| */ | ||
| find<S extends T>(predicate: (value: T, index: number) => value is S): S | undefined; | ||
| find(predicate: (value: T, index: number) => unknown): T | undefined; | ||
|
|
||
| readonly [Symbol.toStringTag]: string; | ||
| } | ||
|
|
||
| // Global `IteratorConstructor` interface that can be augmented by polyfills | ||
| interface IteratorConstructor extends BuiltinIteratorConstructor { | ||
| /** | ||
| * Creates a native iterator from an iterator or iterable object. | ||
| * Returns its input if the input already inherits from the built-in Iterator class. | ||
| * @param value An iterator or iterable object to convert a native iterator. | ||
| */ | ||
| from<T>(value: Iterator<T> | Iterable<T>): BuiltinIterator<T>; | ||
|
rbuckton marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| var Iterator: IteratorConstructor; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // @target: esnext | ||
|
|
||
| const iterator = Iterator.from([0, 1, 2]); | ||
|
|
||
| const mapped = iterator.map(String); | ||
|
|
||
| const filtered = iterator.filter(x => x > 0); | ||
|
|
||
| function isZero(x: number): x is 0 { | ||
| return x === 0; | ||
| } | ||
| const zero = iterator.filter(isZero); | ||
|
|
||
| const iteratorFromBare = Iterator.from({ | ||
| next() { | ||
| return { | ||
| done: Math.random() < .5, | ||
| value: "a string", | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
|
|
||
| function* gen() { | ||
| yield 0; | ||
| } | ||
|
|
||
| const mappedGen = gen().map(x => x === 0 ? "zero" : "other"); | ||
|
|
||
| const mappedValues = [0, 1, 2].values().map(x => x === 0 ? "zero" : "other"); | ||
|
|
||
|
|
||
| class GoodIterator extends Iterator<number> { | ||
| next() { | ||
| return { done: false, value: 0 } as const; | ||
| } | ||
| } | ||
|
|
||
| // error cases | ||
| new Iterator<number>(); | ||
|
|
||
| class C extends Iterator<number> {} | ||
|
|
||
| // it's unfortunate that these are an error | ||
| class BadIterator1 extends Iterator<number> { | ||
| next() { | ||
| if (Math.random() < .5) { | ||
| return { done: false, value: 0 } as const; | ||
| } else { | ||
| return { done: true, value: "a string" } as const; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class BadIterator2 extends Iterator<number> { | ||
| next() { | ||
| return { done: false, value: 0 }; | ||
| } | ||
| } | ||
|
|
||
| class BadIterator3 extends Iterator<number> { | ||
| next() { | ||
| if (Math.random() < .5) { | ||
| return { done: false, value: 0 }; | ||
| } else { | ||
| return { done: true, value: "a string" }; | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.