Skip to content
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

Generator's return "any" type when yielded #10148

Closed
rylev opened this issue Aug 4, 2016 · 1 comment
Closed

Generator's return "any" type when yielded #10148

rylev opened this issue Aug 4, 2016 · 1 comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@rylev
Copy link

rylev commented Aug 4, 2016

TypeScript Version: 1.8, 2.0, and nightly

Code
The following code compiles even though it would appear that result should have type string:

function* myGenerator() {
  yield "hello"
}

function* myOtherGenerator() {
  const result = yield myGenerator()
  result.methodNotAvailableOnString()
}

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?

@DanielRosenwasser 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
@DanielRosenwasser
Copy link
Member

Unlike the await keyword that "grabs" the Promised value from an async function, yield does 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");
    let x = yield "Hello";
    console.log("BBB");
    console.log(x);
    console.log("CCC");
}

var iter = g();
var rr = 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.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants