Cannot implement Iterable interface
- 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
Assessment
This issue has not been assessed yet.