Boolean literal types and return type propagation for generators · Issue #2983 · microsoft/TypeScript (original) (raw)
Navigation Menu
- Explore
- Pricing
Provide feedback
Saved searches
Use saved searches to filter your results more quickly
Description
This suggestion has a few pieces:
- Implement boolean literal types for
true
andfalse
, in a fashion similar to Singleton types under the form of string literal types #1003 - Implement type guards for booleans. Essentially, control flow constructs like if-else, while, for, etc would be subject to a type guard if their guard expression is a boolean.
- Now we can update generators to have a
next
method that returns{ done: false; value: TYield; } | { done: true; value: TReturn; }
, where TYield is inferred from the yield expressions of a generator, and TReturn is inferred from the return expressions of a generator. - Iterators would return
{ done: false; value: TYield; } | { done: true; value: any; }
to be compatible with generators. - for-of, spread, array destructuring, and the type yielded by
yield*
would only pick the value associated with done being false. - The value of a
yield*
expression would pick the value associated with done being true. - Introduce a Generator type that can be used to track the desired type of a
yield
expression. This would be TNext, and would be the type of the parameter for the next method on a generator.
The generator type would look something like this:
interface Generator<TYield, TReturn, TNext> extends IterableIterator { next(value?: TNext): IteratorYieldResult | IteratorReturnResult; throw(exception: any): IteratorYieldResult | IteratorReturnResult; return(value: any): IteratorYieldResult | IteratorReturnResult; Symbol.iterator: Generator<TYield, TReturn, TNext>; [Symbol.toStringTag]: string; }