-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Basic support for generators as iterators #3031
Conversation
… generators Conflicts: src/compiler/diagnosticInformationMap.generated.ts src/compiler/diagnosticMessages.json
error(node.type, Diagnostics.A_generator_cannot_have_a_void_type_annotation); | ||
} | ||
else { | ||
let generatorElementType = getElementTypeFromIterableIterator(returnType) || anyType; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's kind of weird that getTypeFromTypeNode
returns unknownType
when failing whereas getElementTypeFromIterableIterator
returns undefined
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
We'll need to do work on quick info and highlighting spans later on |
What is there to do for quick info? Highlighting spans will require work yes. |
Just to confirm what quick info looks like for a function type. By the way, can you default-export a generator? |
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
Basic support for generators as iterators
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>
)yield
expression 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>
yield
andyield *
expressions again have type anyyield
expressions 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.