Basic support for generators as iterators#3031
Merged
JsonFreeman merged 46 commits intomasterfrom May 30, 2015
Merged
Conversation
… generators Conflicts: src/compiler/diagnosticInformationMap.generated.ts src/compiler/diagnosticMessages.json
src/compiler/checker.ts
Outdated
Member
There was a problem hiding this comment.
I think it's kind of weird that getTypeFromTypeNode returns unknownType when failing whereas getElementTypeFromIterableIterator returns undefined.
Contributor
Author
There was a problem hiding this comment.
getElementTypeFromIterableIterator is used for computing a contextual type, and everything that is used for getting a contextual type has to return undefined if it has nothing to contribute.
Member
|
We'll need to do work on quick info and highlighting spans later on |
Contributor
Author
|
What is there to do for quick info? Highlighting spans will require work yes. |
Member
|
Just to confirm what quick info looks like for a function type. By the way, can you default-export a generator? |
Contributor
Author
|
Yes, you can default-export a generator. |
… generators Conflicts: src/compiler/diagnosticInformationMap.generated.ts src/compiler/diagnosticMessages.json tests/baselines/reference/emitArrowFunctionWhenUsingArguments14_ES6.symbols tests/baselines/reference/emitArrowFunctionWhenUsingArguments15_ES6.symbols tests/baselines/reference/emitArrowFunctionWhenUsingArguments16_ES6.symbols tests/baselines/reference/emitArrowFunctionWhenUsingArguments17_ES6.symbols tests/baselines/reference/emitArrowFunctionWhenUsingArguments18_ES6.symbols tests/baselines/reference/for-of37.symbols tests/baselines/reference/for-of38.symbols tests/baselines/reference/for-of40.symbols tests/baselines/reference/for-of45.symbols tests/baselines/reference/for-of50.symbols tests/baselines/reference/iterableArrayPattern30.symbols tests/baselines/reference/promiseVoidErrorCallback.symbols tests/baselines/reference/typedArrays.symbols
JsonFreeman
added a commit
that referenced
this pull request
May 30, 2015
Basic support for generators as iterators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This covers part of the proposal in #2873. Namely, it handles checking types in a generator function, and inferring the return type of a generator function.
Specifically, here is what is implemented (copied from the proposal, with strikethroughs marking the differences):
Type annotation on a generator
A generator function can have a return type annotation, just like a function. The annotation represents the type of the generator returned by the function. Here is an example:
Here are the rules:
IterableIterator<any>must be assignable to the type annotation.IterableIterator<T>for this T is not assignable to the type denoted by the type annotation.yield *expression must be assignable toIterable<any>yield *expression must be assignable to the element type of the generator. (string is assignable to string)yield(if present) expression is contextually typed by the element type of the generator (string)yield *expression is contextually typed by the type of the generator (Iterable<string>)yieldexpression has type any.yield *expression has type any.The generator cannot have return expressions.Inferring the type of a generator
A generator function with no type annotation can have the type annotation inferred. So in the following case, the type will be inferred from the yield statements:
yield *operands.yield *expression must be assignable toIterable<any>yieldandyield *expressions again have type anyyieldexpressions are contextually typed by the element type of the contextual typeyield *expressions are contextually typed by the contextual type.yield*expressions, the element type is an implicit any, which errors in noImplicitAny mode.