Skip to content

Error with async generators #32247

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

Closed
falsandtru opened this issue Jul 4, 2019 · 6 comments · Fixed by #32558
Closed

Error with async generators #32247

falsandtru opened this issue Jul 4, 2019 · 6 comments · Fixed by #32558
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@falsandtru
Copy link
Contributor

@rbuckton

TypeScript Version: master

Search Terms:

Code

const g: <U, R, S>(com: () => Iterator<S, U, R> | AsyncIterator<S, U, R>) => Promise<U> = async <U, R, S>(com: () => Iterator<S, U, R> | AsyncIterator<S, U, R>): Promise<U> => {
  throw com;
};

Expected behavior:
pass
Actual behavior:

index.ts:1:7 - error TS2719: Type '<U, R, S>(com: () => Iterator<S, U, R> | AsyncIterator<S, U, R>) => Promise<U>' is not assignable to type '<U, R, S>(com: () => Iterator<S, U, R> | AsyncIterator<S, U, R>) => Promise<U>'. Two different types with this name exist, but they are unrelated.
  Type 'Promise<U | PromiseLike<U>>' is not assignable to type 'Promise<U>'.
    Type 'U | PromiseLike<U>' is not assignable to type 'U'.
      'U | PromiseLike<U>' is assignable to the constraint of type 'U', but 'U' could be instantiated with a different subtype of constraint '{}'.
        Type 'PromiseLike<U>' is not assignable to type 'U'.
          'PromiseLike<U>' is assignable to the constraint of type 'U', but 'U' could be instantiated with a different subtype of constraint '{}'.

1 const g: <U, R, S>(com: () => Iterator<S, U, R> | AsyncIterator<S, U, R>) => Promise<U> = async <U, R, S>(com: () => Iterator<S, U, R> | AsyncIterator<S, U, R>): Promise<U> => {
        ~

Playground Link:

Related Issues:

@rbuckton
Copy link
Contributor

rbuckton commented Jul 4, 2019

It doesn't seem to like the definitions of TNext | PromiseLike<TNext> and TReturn | PromiseLike<TReturn> for the next() and return() methods.

@falsandtru
Copy link
Contributor Author

falsandtru commented Jul 4, 2019

Yes, the actual type is not assignable to the declared type although they are the same type.

@falsandtru
Copy link
Contributor Author

@rbuckton Please don't forget this too.

@rbuckton
Copy link
Contributor

@ahejlsberg for reference

@ahejlsberg
Copy link
Member

This appears to be unaffected by #32460. Best I can tell the issue is that Iterator and AsyncIterator are alike enough that inference succeeds in inferring from either to either--and therefore gets confused.

@ahejlsberg ahejlsberg self-assigned this Jul 24, 2019
@ahejlsberg ahejlsberg added the Bug A bug in TypeScript label Jul 24, 2019
@ahejlsberg ahejlsberg added this to the TypeScript 3.6.1 milestone Jul 24, 2019
@ahejlsberg
Copy link
Member

A simpler repro:

interface Foo<T> {
    test(value: T): void;
}

interface Bar<T> {
    test(value: T | PromiseLike<T>): void;
}

declare let f1: <T>(x: Foo<T> | Bar<T>) => Promise<T>;
declare let f2: <U>(x: Foo<U> | Bar<U>) => Promise<U>;

f1 = f2;  // Error
f2 = f1;  // Error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants