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
The return type of G will be Iterable<string> that does not reflect the type of the final value, so here the variable will be of type any:
constres=yield*G();
This matters when yield is used not to yield some useful values, but to suspend/resume the function. For instance, a depth-first search function can make use of this trick:
// not sure if this is allowed in tsc... the idea is// that node.value is the value and node[0], node[1], ...// are subnodes (aka "children")interfaceNode<T>extendsIterable<Node<T>>{value: T;}function*search<T>(node: Node<T>,test: (node: Node<T>)=>boolean){if(test(node))returnnode;// see, the actual return type is Nodefor(constsubnodeofnode){// and here is the trick: in the debug mode this yield// suspends the search and gives control to UI, so it// can render the current state of the search and give// and option to continue or stop the search; while in// in the release mode (debug=false) this yield is skipped// and the performance of search(...) is the same as if it// was written as a plain recursive functiondebug&&(yield"going to "+subnode.value+"...");// search(...) returns Iterable<any>, so result is any hereconstresult=yield*search(subnode,test);if(result)returnresult;}returnnull;// node not found}
The text was updated successfully, but these errors were encountered:
#2983 list some steps toward achieving this. The boolean literals are in master, but I don't believe any of the generator-specific changes have been made yet. Once generators have their TReturn type inferred, then hopefully yield* expressions can be better typed.
Not sure why that issue is closed, given that most of the work described there has not been implemented.
Consider the following generator:
The return type of
G
will beIterable<string>
that does not reflect the type of the final value, so here the variable will be of typeany
:This matters when
yield
is used not to yield some useful values, but to suspend/resume the function. For instance, a depth-first search function can make use of this trick:The text was updated successfully, but these errors were encountered: