Skip to content
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

Spread operator doesn't work for arrays #573

Closed
karelbilek opened this issue Jun 26, 2015 · 11 comments
Closed

Spread operator doesn't work for arrays #573

karelbilek opened this issue Jun 26, 2015 · 11 comments
Labels

Comments

@karelbilek
Copy link
Contributor

This code doesn't typecheck

var a: Array<number> = [1, 2, 3]
var b: Array<number> = [...a];

This is how it works with babel.

Flow just returns

...: rest array of spread operand
This type is incompatible with
....: number

(Spread operator is marked here #560 as done, so I am adding it as a separate issue)

@karelbilek
Copy link
Contributor Author

This idiom is kinda useless in the simple case, but it is useful in "array-like" objects.

var collection: HTMLCollection = window.document.getElementsByClassName("my-class");
var elements: Array<Element> = [...collection]
//then I can do
elements.filter( /*....something...*/ ).map //...... etc

With Babel REPL.

(Note that this requires Babel polyfill in chrome, because of the use of Array.from.)

@samwgoldman
Copy link
Member

Very easy to repro. Seems like a bug to me. Thanks for the report!

@karelbilek
Copy link
Contributor Author

No problem! I report the bugs as I encounter them in real code :D

@gabelevi gabelevi added the bug label Jul 24, 2015
@JavascriptMick
Copy link

related?

class Person{
  name: string;
  constructor(name){
    this.name = name
  }
}

var map: Map<string, Person> = new Map([
    ['bob', new Person('bob')],
    ['tim',  new Person('tim')],
]);

var count = [...map.values()].filter(p => (p.name=='bob')).length

/Users/michael/Tutorials/node/clickforce_es6/src/Test.js
line 14 col 13 call of method filter - Error:
line 14 col 44 property name - Property cannot be accessed on
line 14 col 17 rest array of spread operand
line 14 col 17 call of method values - Error:
line 14 col 17 Iterator - This type is incompatible with
line 14 col 17 spread operand

@kkirby
Copy link
Contributor

kkirby commented Aug 24, 2015

@mdausmann I would guess so.

I just found this bug myself when trying to use it in favor of splice, e.g.: ['abc',...arr]. For others, a workaround would be Array.from as that's what's happening in Babel when you do [...arrayLike].

@jaredly
Copy link
Contributor

jaredly commented Oct 16, 2015

still a bug!

@karelbilek
Copy link
Contributor Author

It works on my copy (at least the current github version)

@gabelevi
Copy link
Contributor

Right, this should be fixed in 0.17.0. 89b5aa6 was the commit that fixed this.

@wldcordeiro
Copy link

I can open another issue for this, if you'd like. But from reading into Spread it should work on iterables, so in that case it should work for a Set which is currently an error as of 0.22.0.

@samwgoldman
Copy link
Member

@wldcordeiro there is already an open issue for that—on mobile so I can't find it now.

@glortho
Copy link

glortho commented May 2, 2016

Progress is being made here: #1668

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants