17

I want to get a Readable form a Blob.

import {Readable} from 'stream';

const data: Blob = new Blob( );
const myReadable: Readable = (new Readable()).wrap(data.stream());
myReadable.pipe(ext);

I get This error

ERROR in src/app/features/recorder/components/record-panel/record-panel.component.ts:80:38 - error TS2345: Argument of type 'ReadableStream<any>' is not assignable to parameter of type 'ReadableStream'.
  Type 'ReadableStream<any>' is missing the following properties from type 'ReadableStream': readable, read, setEncoding, pause, and 22 more.

I use Node 14 angular 10 and typescript

Nasgar
  • 859
  • 2
  • 11
  • 26

1 Answers1

25

There are two different definitions of ReadableStream in this code which are not compatible with each other.


Blob comes from the DOM typings. Blob.stream() returns a ReadableStream<any> as defined in lib.dom.d.ts:

interface ReadableStream<R = any> {
    readonly locked: boolean;
    cancel(reason?: any): Promise<void>;
    getReader(): ReadableStreamDefaultReader<R>;
    pipeThrough<T>(transform: ReadableWritablePair<T, R>, options?: StreamPipeOptions): ReadableStream<T>;
    pipeTo(dest: WritableStream<R>, options?: StreamPipeOptions): Promise<void>;
    tee(): [ReadableStream<R>, ReadableStream<R>];
}

GitHub source


Readable.wrap() expects to be called with a ReadableStream from the NodeJS definitions in @types/node/globals.ts:

interface ReadableStream extends EventEmitter {
    readable: boolean;
    read(size?: number): string | Buffer;
    setEncoding(encoding: BufferEncoding): this;
    pause(): this;
    resume(): this;
    isPaused(): boolean;
    pipe<T extends WritableStream>(destination: T, options?: { end?: boolean; }): T;
    unpipe(destination?: WritableStream): this;
    unshift(chunk: string | Uint8Array, encoding?: BufferEncoding): void;
    wrap(oldStream: ReadableStream): this;
    [Symbol.asyncIterator](): AsyncIterableIterator<string | Buffer>;
}

GitHub source


Your code attempts to assign a DOM ReadableStream to a function that requires a NodeJS ReadableStream. You get a error telling you all of the properties that are expected from this Node version which aren't present in the DOM version variable data.stream().

Linda Paiste
  • 38,446
  • 6
  • 64
  • 102
  • What would be the solution here then? Is there a way to get a `ReadableStream` into the node.js format? I have the same problem except the functionality works, its just the type that is complaining. – liamlows Mar 25 '22 at 17:13
  • 3
    @liamlows I think you can use a type assertion? `someVar as NodeJS.ReadableStream` or possibly `someVar as unknown as NodeJS.ReadableStream` – Linda Paiste Mar 25 '22 at 21:23
  • thanks that seemed to work! I was missing the `as unknown` chunk. – liamlows Apr 06 '22 at 00:45
  • This same error can come up in other contexts, but require different typecasting - see https://stackoverflow.com/questions/37614649/how-can-i-download-and-save-a-file-using-the-fetch-api-node-js/74722818#comment133510726_74722818 for another example of the same issue with a different solution. – antonok Mar 08 '23 at 22:18