Skip to content

Missing generator's type parameter in functions and constructors #32248

Closed

Description

@falsandtru

Ron Buckton (@rbuckton)

TypeScript Version: master

Search Terms:

Code

export class Coroutine<T = unknown, R = unknown, S = unknown> {
  constructor(gen: (this: Coroutine<T, R, S>) => Generator<R, T, S> | AsyncGenerator<R, T, S>) {
  }
}
new Coroutine<0, 0, 1>(async function* g() {
  const a = yield 0;
  return 0;
});

Expected behavior:

Type g is AsyncGenerator<0, 0, 1>.
Type a is 1.

Actual behavior:

Type g is AsyncGenerator<0, 0, unknown>.
Type a is any.

Playground Link:

Related Issues:

Activity

  1. rbuckton commented on Jul 4, 2019

    @rbuckton
    Contributor

    This is an issue with contextual typing. The contextual type from the constructor isn't flowing to the async generator.

  2. falsandtru commented on Jul 4, 2019

    @falsandtru
    ContributorAuthor

    Since the constructor has been given all type parameters the type of the parameter gen must be AsyncGenerator<0, 0, 1>. All type parameters are clearly decided. And the return type of yield must be unknown with the current behavior, as is defined by the context type.

  3. falsandtru commented on Jul 4, 2019

    @falsandtru
    ContributorAuthor

    Note that the type declaration for this doesn't affect this bug. This is one of the verbose elements for effectivity of regression tests.

  4. falsandtru commented on Jul 4, 2019

    @falsandtru
    ContributorAuthor

    Same also with functions.

    export function f<T = unknown, R = unknown, S = unknown>(gen: () => Generator<R, T, S> | AsyncGenerator<R, T, S>) {
    }
    f<0, 0, 1>(async function* g() {
      const a = yield 0;
      return 0;
    });
  5. falsandtru commented on Jul 16, 2019

    @falsandtru
    ContributorAuthor

    Ron Buckton (@rbuckton) Why you still haven't fixed this bug?

  6. falsandtru commented on Jul 17, 2019

    @falsandtru
    ContributorAuthor
  7. falsandtru commented on Jul 26, 2019

    @falsandtru
    ContributorAuthor
  8. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions