Skip to content

AsyncIterator type is not covariant #103

@Peeja

Description

@Peeja

I would have expected the AsyncIterator type to be covariant; that is, I would expect to be able to do something like:

declare const oneIterator: AsyncIterator<1>;
const numberIterator: AsyncIterator<number> = oneIterator;

However, this fails because:

Type 'AsyncIterator<1>' is not assignable to type 'AsyncIterator<number>'.
  Types of property 'transform' are incompatible.
    Type '<D>(options: TransformOptions<1, D>) => AsyncIterator<D>' is not assignable to type '<D>(options: TransformOptions<number, D>) => AsyncIterator<D>'.
      Types of parameters 'options' and 'options' are incompatible.
        Type 'TransformOptions<number, any>' is not assignable to type 'TransformOptions<1, any>'.
          Types of property 'source' are incompatible.
            Type 'SourceExpression<number> | undefined' is not assignable to type 'SourceExpression<1> | undefined'.
              Type 'AsyncIterator<number>' is not assignable to type 'SourceExpression<1> | undefined'.
                Type 'AsyncIterator<number>' is not assignable to type 'AsyncIterator<1>'.
                  The types returned by 'read()' are incompatible between these types.
                    Type 'number | null' is not assignable to type '1 | null'.
                      Type 'number' is not assignable to type '1'. ts(2322)

I'm inexperienced with AsyncIterator, so I'm not completely following what that error amounts to. It looks to me like it's complaining that you could call numberIterator.transform({ source: anotherNumberIterator }), and since anotherNumberIterator emits numbers, but numberIterator (which is secretly also oneIterator) is only expecting 1s, not other numbers.

Except, I don't understand why you'd provide numberIterator.transform() with a source option. Isn't numberIterator itself the source? I'm not clear why that option is available or what it means to provide it. It doesn't appear to be documented.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions