facebook / facebook/flow

Cannot implement Iterable interface

Open
#5,063 3 comments 5 reactions 0 assignees View on GitHub
iterators
Dominant language
Rust
Stars
22.3k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

If I implement an `Iterator` interface with `Symbol.iterator` like this, it works at runtime.

```js
let iterable = {
[Symbol.iterator]: function*() {
yield 1;
yield 2;
yield 3;
},
};
console.log([...iterable]);
```
```
[ 1, 2, 3 ]
```

But Flow complains that the object isn't iterable, expecting a property named `"@@iterator"`:

```
8: console.log([...iterable]);
^ object literal. This type is incompatible with
8: console.log([...iterable]);
^ $Iterable
Property `@@iterator` is incompatible:
8: console.log([...iterable]);
^ property `@@iterator` of $Iterable. Property not found in
8: console.log([...iterable]);
^ object literal
```

If I change the name of the property to `"@@iterator"` like this, Flow is happy.

```js
let iterable = {
"@@iterator": function*() {
yield 1;
yield 2;
yield 3;
},
};
console.log([...iterable]);
```
```
No errors!
```

But it fails at runtime:

```
TypeError: iterable[Symbol.iterator] is not a function
```

I can't keep the runtime and the typechecker happy at the same time.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.