-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[RFC] Improve core types for (Async) Iterable/Iterator/Generator #3990
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
base: main
Are you sure you want to change the base?
Conversation
d3f4ca6
to
cd3d8ba
Compare
* Changes `@@iterator` method to return more specific type in Generators. * An `$Iterator` is an `$Iterable` * Add optional `return` and `throw` methods to `$Iterator` (http://www.ecma-international.org/ecma-262/7.0/#table-54) * A `$Generator` is an `$Iterator` * Fix mistaken function name to `$asyncIterate` to match style of `$iterate`.
cd3d8ba
to
e1617b3
Compare
@nmote do you think this is ok to merge? |
@@iterator(): $Iterator<Yield,Return,Next>; | ||
next(value?: Next): IteratorResult<Yield,Return>; | ||
+return?: <R>(value: R) => IteratorResult<Yield,R|Return>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A return
definition would be great. :)
I'd think it should just be () => IteratorResult<Yield, Return>
(same for $AsyncIterator
below) as long as that doesn't break passing a generator where an iterator is expected.
If you're hand-writing a return
method to handle abrupt completions, you normally wouldn't accept any value because none are passed on IteratorClose
. See http://www.ecma-international.org/ecma-262/7.0/#sec-iteratorclose
interface $Iterable<+Yield,+Return,-Next> { | ||
@@iterator(): $Iterator<Yield,Return,Next>; | ||
} | ||
type Iterable<+T> = $Iterable<T,void,void>; | ||
|
||
interface Generator<+Yield,+Return,-Next> { | ||
interface $Iterator<+Yield,+Return,-Next> extends $Iterable<Yield,Return,Next> { | ||
@@iterator(): $Iterator<Yield,Return,Next>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wish Flow didn't require an @@iterator
method on Iterators, even though they typically have one that returns this
by convention. (But I'm not sure how to use this without hacks in any case: #1163.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#6075 has some relevant discussion
@nmote anything preventing merging this? |
Happy comment anniversary @goodmind! I'm also finding lots of issues with async generators, but don't have the OCaml chops to help here. |
TIL that there's a convention for
The TS type for
|
@@iterator
method to return more specific type in Generators.$Iterator
is an$Iterable
return
andthrow
methods to$Iterator
(http://www.ecma-international.org/ecma-262/7.0/#table-54)$Generator
is an$Iterator
$asyncIterate
to match style of$iterate
.