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

Typescript issues with ponyfill #66

Open
dominictobias-bullish opened this issue Nov 1, 2024 · 0 comments
Open

Typescript issues with ponyfill #66

dominictobias-bullish opened this issue Nov 1, 2024 · 0 comments

Comments

@dominictobias-bullish
Copy link

dominictobias-bullish commented Nov 1, 2024

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

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

No branches or pull requests

1 participant