aws-sdk-js-v3 icon indicating copy to clipboard operation
aws-sdk-js-v3 copied to clipboard

Cannot find name 'ReadableStream'.

Open chrisguttandin opened this issue 3 years ago • 7 comments

Describe the bug

Since version 3.127.0 the client-dynamodb package can't be used anymore without including the 'DOM' lib in the tsconfig. TypeScript complains:

node_modules/@aws-sdk/types/dist-types/serde.d.ts:58:33 - error TS2304: Cannot find name 'ReadableStream'.

I guess the change was introduced here: https://github.com/aws/aws-sdk-js-v3/commit/f311cab406a16274dc2487dfe55c36b45a5811f5

Expected Behavior

I would expect the SDK to be usable with the type definitions for Node.js only. Adding the 'DOM' lib in the tsconfig fixes the issue but it also makes TypeScript believe it has access to all the DOM APIs which is not the case at runtime. Therefore it's not really an option.

Current Behavior

The code doesn't compile.

Reproduction Steps

Create a project which uses @aws-sdk/[email protected] or above.

Possible Solution

I would suggest to only use the ReadableStream when it is defined on the global scope. This should work in the browser and also outside. It will return never instead of the expected type which exactly matches the runtime behavior.

This could be a fix:

type OptionalReadableStream = typeof globalThis extends { ReadableStream: infer GlobalReadableStream } ? GlobalReadableStream : never;
/**
 * The interface contains mix-in utility functions to transfer the runtime-specific
 * stream implementation to specified format. Each stream can ONLY be transformed
 * once.
 */
export interface SdkStreamMixin {
    transformToByteArray: () => Promise<Uint8Array>;
    transformToString: (encoding?: string) => Promise<string>;
    transformToWebStream: () => OptionalReadableStream;
}

The OptionalReadableStream gets assigned to the global ReadableStream definition in case there is one. If there is none it becomes never.

Additional Information/Context

Please let me know if I should provide a PR with the proposed fix from above. I would be happy to do so.

SDK version used

3.127.0

Environment details (OS name and version, etc.)

[email protected]

chrisguttandin avatar Jul 19 '22 13:07 chrisguttandin

This is also true of cognito-client-identity-provider

stocks29 avatar Jul 19 '22 14:07 stocks29

This was also a long standing issue with the s3 client (see https://github.com/aws/aws-sdk-js-v3/issues/3063), but the change from https://github.com/aws/aws-sdk-js-v3/commit/f311cab406a16274dc2487dfe55c36b45a5811f5 in the @aws-sdk/types package now breaks most if not all aws clients.

workaround from the linked issue until this is fixed:

Add this where you are exporting code that uses the SDK:

declare global {
   interface ReadableStream {}
}

barryhagan avatar Jul 20 '22 13:07 barryhagan

@barryhagan I think it would be a bit better to add a .d.ts file to a Node.js-only project which sets ReadableStream to never to prevent any unintended usage.

type ReadableStream = never;

chrisguttandin avatar Jul 20 '22 13:07 chrisguttandin

One problem with that approach is interfaces can merge, but types can not. If your code is a dependency used by something that does include dom types, you will get Duplicate identifier 'ReadableStream' errors.

There are too many packages out there that unwittingly bring in dom types via triple slash refs:

/// <reference lib="dom" />

For example, lru-cache which is a dependency of Apollo server causes the dom to show up in my graphql api project. So for me, the empty stub interface is the workaround to use. type: never can work fine if your project isn't used as a dependency anywhere that might have dom type pollution.

barryhagan avatar Jul 20 '22 14:07 barryhagan

@barryhagan That makes sense. I wasn't aware that so many packages do this.

But I think the fix that I proposed above (to define a custom OptionalReadableStream based on the availability of a global ReadableStream) should still work in those scenarios.

chrisguttandin avatar Jul 20 '22 14:07 chrisguttandin

Same incident with aws-xray-sdk-core...

<repo>/node_modules/aws-xray-sdk-core/node_modules/@aws-sdk/types/dist-types/ts3.4/serde.d.ts:34:33 - error TS2304: Cannot find name 'ReadableStream'.
34     transformToWebStream: () => ReadableStream;
                                   ~~~~~~~~~~~~~~
Found 1 error.
npm ERR! code ELIFECYCLE
npm ERR! errno 1

ClaudenirFreitas avatar Jul 28 '22 10:07 ClaudenirFreitas

Also hitting this issue with @aws-sdk/client-personalize-runtime

lib/recommend-lambda/node_modules/@aws-sdk/client-personalize-runtime/node_modules/@aws-sdk/types/dist-types/ts3.4/serde.d.ts:34:33 - error TS2304: Cannot find name 'ReadableStream'.

34     transformToWebStream: () => ReadableStream;
                                   ~~~~~~~~~~~~~~


Found 1 error.

davekiss avatar Aug 10 '22 17:08 davekiss

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

github-actions[bot] avatar Sep 14 '22 00:09 github-actions[bot]