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

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

Closed
falsandtru opened this issue Jul 4, 2019 · 7 comments · Fixed by #32433
Closed

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

falsandtru opened this issue Jul 4, 2019 · 7 comments · Fixed by #32433

Comments

@falsandtru
Copy link
Contributor

@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:

@rbuckton
Copy link
Member

rbuckton commented Jul 4, 2019

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

@falsandtru
Copy link
Contributor Author

falsandtru commented Jul 4, 2019

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.

@falsandtru
Copy link
Contributor Author

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

@falsandtru
Copy link
Contributor Author

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;
});

@falsandtru falsandtru changed the title Missing type parameter of generators Missing generator's type parameter in functions and constructors Jul 4, 2019
@falsandtru
Copy link
Contributor Author

@rbuckton Why you still haven't fixed this bug?

@falsandtru
Copy link
Contributor Author

Thanks @rbuckton !

@falsandtru
Copy link
Contributor Author

@RyanCavanaugh Label this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants