You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Expected behavior:
When a generator is yielded I expected that the type of the subsequent variable to be whatever T is in myGenerator's return type IterableIterator<T>
Actual behavior:
This is however not the case. result has type any. Should the TypeScript compiler be able to infer that that type of result should be string?
The text was updated successfully, but these errors were encountered:
DanielRosenwasser
added
External
Relates to another program, environment, or user action which we cannot control.
Working as Intended
The behavior described is the intended behavior; this is not a bug
and removed
External
Relates to another program, environment, or user action which we cannot control.
labels
Aug 4, 2016
Unlike the await keyword that "grabs" the Promised value from an async function, yielddoes not grab the yielded value of a generator. It instead makes it available for anyone consuming the current generator. In that case, you are yielding an iterator, meaning any callers will have an iterable of iterables of strings.
The value produced by yield within a generator is the argument that you pass in to next(). This value can be anything, but can't be inferred by the structure of the generator.
function*g(){console.log("AAA");letx=yield"Hello";console.log("BBB");console.log(x);console.log("CCC");}variter=g();varrr=iter.next("unwitnessed");// first call's value is console.log(rr.value);iter.next("World");
Outputs:
AAA
Hello
BBB
World
CCC
If we had default type parameters, we could say that the type passed into next defaults to any, but if you want extra strictness, you could enforce it otherwise.
TypeScript Version: 1.8, 2.0, and nightly
Code
The following code compiles even though it would appear that
result
should have typestring
:Expected behavior:
When a generator is yielded I expected that the type of the subsequent variable to be whatever
T
is inmyGenerator
's return typeIterableIterator<T>
Actual behavior:
This is however not the case.
result
has typeany
. Should the TypeScript compiler be able to infer that that type ofresult
should bestring
?The text was updated successfully, but these errors were encountered: