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

Iterators (ES6) #1900

Closed
4 tasks done
ahejlsberg opened this issue Feb 3, 2015 · 14 comments
Closed
4 tasks done

Iterators (ES6) #1900

ahejlsberg opened this issue Feb 3, 2015 · 14 comments
Labels
Bug A bug in TypeScript ES6 Relates to the ES6 Spec Fixed A PR has been merged for this issue

Comments

@ahejlsberg
Copy link
Member

ahejlsberg commented Feb 3, 2015

Creating an issue to track work on iterators. This includes:

  • Finalize Iterable and Iterator types.
  • Support iterators with spread operator (...) in function calls and array literals.
  • Support destructuring of iterator values.
  • Support for...of with iterator values.
@ahejlsberg ahejlsberg added Bug A bug in TypeScript ES6 Relates to the ES6 Spec labels Feb 3, 2015
@mhegazy mhegazy added this to the TypeScript 1.5 milestone Feb 5, 2015
@JsonFreeman
Copy link
Contributor

Also, when we do generators, we have to support yield* on an iterable.

@DanielRosenwasser
Copy link
Member

I've made each of the bullet points into a task list item so that we can check these off and keep appropriate track of them.

@JsonFreeman
Copy link
Contributor

Iterables and Iterator types are addressed by #2083

@JsonFreeman
Copy link
Contributor

ES6 level for...of is handled by PR #2164.

@JsonFreeman
Copy link
Contributor

@ahejlsberg I have a question that may be relevant here, though it is not required. Should spread elements support contextual typing? By this I mean two things:

  1. If an array is contextually typed by T, any spread element inside gets contextually typed by T, rather than the element type of T.
  2. If a spread argument is contextually typed by T[] (because it aligned with a rest parameter T[]), then its operand gets contextually typed by T[] rather than T.

My gut says that this is not necessary. The only reason I bring it up is that Number 2 above could make type argument inference stronger.

@JsonFreeman
Copy link
Contributor

Actually, thinking about this more, I think it would make things more fluid and actually make things more consistent.

@JsonFreeman
Copy link
Contributor

My plan for spread and destructuring is as follows:
To type check a spread expression ...expr with language target ES6:

  • The operand has to be iterable (assignable to the Iterable<T> interface, where T given by the type of expr[Symbol.iterator]().next().value)
  • If it is a SpreadElement, the surrounding array gets that T as one of its element types.
  • If it is a SpreadArgument, then that T must be assignable to the element type of the rest parameter it aligns with. It will also be possible to do type argument inference from that T to the element type of the rest parameter it aligns with.

For array destructuring (both binding and assignment varieties), we will type each destructuring array element in the following way when target is ES6:

  • The destructuring parent type must be assignable to Iterator<T> in the same way as spread.
  • For non-rest elements:
    • For the element at index i, we take the property named "i" from the destructuring parent type.
    • If there is no such property, then the element gets associated with the iteration type T from above (just like spread).
  • For a rest element, the type is T[] instead of T.

The key difference between the two is that destructuring prefers the numerically named properties of the parent type, whereas the spread argument doesn't care about them.

Note that the type rules for ES3/ES5 don't change, the above are just the type rules for ES6.

@corporatepiyush
Copy link

eagerly waiting for this feature to use on io.js. Any tentative release date ?
Would we able to use that version of typescript to compile down to ES3 or ES5 for IE9 and IE10 ?

@JsonFreeman
Copy link
Contributor

@corporatepiyush, the only thing left for 'for...of' loops is #2308. Once that's in, you can use those loops.

Type checking for iterate on spread / iterate on destructuring is planned for the next release (TypeScript 1.5).

@JsonFreeman
Copy link
Contributor

To your second question, yes this will be supported for in ES3/5 but only for arrays. Except that in ES5, 'for...of' works on strings too.

@JsonFreeman
Copy link
Contributor

'for...of' loops are now fully supported, so feel free to start using them.

@corporatepiyush
Copy link

Awesome thanks 👍

@JsonFreeman
Copy link
Contributor

The spread and destructuring aspects of this feature are covered by #2498

@JsonFreeman
Copy link
Contributor

This is done now. The only remaining item for iterators is yield* but that is more of a generators thing.

@JsonFreeman JsonFreeman added the Fixed A PR has been merged for this issue label Apr 9, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript ES6 Relates to the ES6 Spec Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants