Skip to content
Open
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
40 changes: 23 additions & 17 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,9 @@ declare class Object {

// Well known Symbols.
declare class $SymbolHasInstance mixins Symbol {}
declare class $SymboIsConcatSpreadable mixins Symbol {}
declare class $SymbolIsConcatSpreadable mixins Symbol {}
declare class $SymbolIterator mixins Symbol {}
declare class $SymbolAsyncIterator mixins Symbol {}
declare class $SymbolMatch mixins Symbol {}
declare class $SymbolReplace mixins Symbol {}
declare class $SymbolSearch mixins Symbol {}
Expand All @@ -76,8 +77,9 @@ declare class Symbol {
static (value?:any): Symbol;
static for(key: string): Symbol;
static hasInstance: $SymbolHasInstance;
static isConcatSpreadable: $SymboIsConcatSpreadable;
static isConcatSpreadable: $SymbolIsConcatSpreadable;
static iterator: string; // polyfill '@@iterator'
static asyncIterator: $SymbolAsyncIterator;
static keyFor(sym: Symbol): ?string;
static length: 0;
static match: $SymbolMatch;
Expand Down Expand Up @@ -547,20 +549,22 @@ type IteratorResult<Yield,Return> =
| { done: true, value?: Return }
| { done: false, value: Yield };

interface $Iterator<+Yield,+Return,-Next> {
@@iterator(): $Iterator<Yield,Return,Next>;
next(value?: Next): IteratorResult<Yield,Return>;
}
type Iterator<+T> = $Iterator<T,void,void>;

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>;

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.)

Copy link
Contributor

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

next(value?: Next): IteratorResult<Yield,Return>;
+return?: <R>(value: R) => IteratorResult<Yield,R|Return>;

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

+throw?: (error?: any) => IteratorResult<Yield,Return>;
}
type Iterator<+T> = $Iterator<T,void,void>;

interface Generator<+Yield,+Return,-Next> extends $Iterator<Yield,Return,Next> {
@@iterator(): Generator<Yield,Return,Next>;
next(value?: Next): IteratorResult<Yield,Return>;
return<R>(value: R): IteratorResult<Yield,R|Return>;
throw(error?: any): IteratorResult<Yield,Return>;
}
Expand All @@ -569,25 +573,27 @@ declare function $iterate<T>(p: Iterable<T>): T;

/* Async Iterable/Iterator/Generator */

interface $AsyncIterator<+Yield,+Return,-Next> {
@@asyncIterator(): $AsyncIterator<Yield,Return,Next>;
next(value?: Next): Promise<IteratorResult<Yield,Return>>;
}
type AsyncIterator<+T> = $AsyncIterator<T,void,void>;

interface $AsyncIterable<+Yield,+Return,-Next> {
@@asyncIterator(): $AsyncIterator<Yield,Return,Next>;
}
type AsyncIterable<+T> = $AsyncIterable<T,void,void>;

interface AsyncGenerator<+Yield,+Return,-Next> {
interface $AsyncIterator<+Yield,+Return,-Next> extends $AsyncIterable<Yield,Return,Next> {
@@asyncIterator(): $AsyncIterator<Yield,Return,Next>;
next(value?: Next): Promise<IteratorResult<Yield,Return>>;
+return?: <R>(value: R) => Promise<IteratorResult<Yield,R|Return>>;
+throw?: (error?: any) => Promise<IteratorResult<Yield,Return>>;
}
type AsyncIterator<+T> = $AsyncIterator<T,void,void>;

interface AsyncGenerator<+Yield,+Return,-Next> extends $AsyncIterator<Yield,Return,Next> {
@@asyncIterator(): AsyncGenerator<Yield,Return,Next>;
next(value?: Next): Promise<IteratorResult<Yield,Return>>;
return<R>(value: R): Promise<IteratorResult<Yield,R|Return>>;
throw(error?: any): Promise<IteratorResult<Yield,Return>>;
}

declare function $asyncIterator<T>(p: AsyncIterable<T>): T;
declare function $asyncIterate<T>(p: AsyncIterable<T>): T;

/* Maps and Sets */

Expand Down
4 changes: 2 additions & 2 deletions newtests/array_literal_tuple_spread/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ export default suite(({addFile, addFiles, addCode}) => [
test.js:3
3: const arr: Array<number> = [..."hello"];
^^^^^^ number. This type is incompatible with
276: @@iterator(): Iterator<string>;
^^^^^^ string. See lib: [LIB] core.js:276
278: @@iterator(): Iterator<string>;
^^^^^^ string. See lib: [LIB] core.js:278
`,
)
.because('String is an Iterable<string>'),
Expand Down
Loading