Skip to content

Typescript issues with ponyfill #66

@dominictobias-bullish

Description

@dominictobias-bullish

Hi thanks for this package. I have two TS errors using the Ponyfill.

  1. does not work with moduleResolution: "Node" (solvable)

The most common resolution to use but the defs can't be found with it. Solved by changing to:

"module": "NodeNext",
"moduleResolution": "nodenext",

or

"module": "ES2022",
"moduleResolution": "Bundler",

I guess the way defs are compiled is too modern but that's fine, it's solvable.

  1. incorrect type defs

The current def for asyncIterator.d.ts is:

/**
 * the implementer that does all the heavy works
 */
declare class ReadableStreamAsyncIterableIteratorImpl<R, TReturn> implements AsyncIterator<R> {
    #private;
    constructor(reader: ReadableStreamDefaultReader<R>, preventCancel: boolean);
    next(): Promise<IteratorResult<R, undefined>>;
    return(value?: TReturn): Promise<IteratorReturnResult<TReturn>>;
}
declare const implementSymbol: unique symbol;
/**
 * declare `ReadableStreamAsyncIterableIterator` interaface
 */
interface ReadableStreamAsyncIterableIterator<R, TReturn> extends ReadableStreamAsyncIterator<R> {
    [implementSymbol]: ReadableStreamAsyncIterableIteratorImpl<R, TReturn>;
}
export interface ReadableStreamIteratorOptions {
    preventCancel?: boolean;
}
/**
 * Get an async iterable iterator from a readable stream
 * @param readableStream
 * @param readableStreamIteratorOptions
 * @returns
 */
export declare function asyncIterator<R, TReturn>(readableStream: ReadableStream<R>, { preventCancel }?: ReadableStreamIteratorOptions): ReadableStreamAsyncIterableIterator<R, TReturn>;
export {};

This results in:

for await (const chunk of asyncIterator(res.body)) {}
Type 'ReadableStreamAsyncIterableIterator<Uint8Array, unknown>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.ts(2504)

This is because the interface doesn't define an asyncIterator, it has this funny implementSybol instead. Simply doing:

interface ReadableStreamAsyncIterableIterator<R, TReturn> extends ReadableStreamAsyncIterator<R> {
    [Symbol.asyncIterator](): ReadableStreamAsyncIterableIteratorImpl<R, TReturn>;
    // Or more acceptable by eslint (@typescript-eslint/method-signature-style):
    // [Symbol.asyncIterator]: () => ReadableStreamAsyncIterableIteratorImpl<R, TReturn>
}

Fixes the issue.

Is there a reason you defined an anonymous symbol that Typescript isn't aware of?:

const implementSymbol = Symbol();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions