Skip to content

Error with async generators #32247

Closed

Description

@falsandtru

Ron Buckton (@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:

Activity

  1. rbuckton commented on Jul 4, 2019

    @rbuckton
    Contributor

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

  2. falsandtru commented on Jul 4, 2019

    @falsandtru
    ContributorAuthor

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

  3. falsandtru commented on Jul 17, 2019

    @falsandtru
    ContributorAuthor

    Ron Buckton (@rbuckton) Please don't forget this too.

  4. rbuckton commented on Jul 17, 2019

    @rbuckton
    Contributor
  5. ahejlsberg commented on Jul 23, 2019

    @ahejlsberg
    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.

  6. ahejlsberg commented on Jul 25, 2019

    @ahejlsberg
    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
  7. 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

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions