Skip to content

yield expressions should be able to acquire the type of generator return typesΒ #10509

Closed
@DanielRosenwasser

Description

@DanielRosenwasser

Courtesy of @bterlson (and his idea in general)

interface Foo<T> extends IterableIterator<T> {
    next(valueGivenWhenYieldExpressionIsUsed: number)
}

This type is basically an IterableIterator except that it restricts the types of values a consumer can pass through to next.

function* foo(): Foo<string> {
    let x = yield "hello";
    console.log(x);
}

let gen = foo();
gen.next(100);
gen.next(200);

Here, calls to next can only accept numbers; however, the yield expressions in the generator are just typed as any.

Since we have an explicit type, we should just figure the type of yield expressions out from that.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions