Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Argument of type 'ReadableStream<any>' is not assignable to parameter of type 'ReadableStream'

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

like image 294
Nasgar Avatar asked Aug 28 '20 08:08

Nasgar


1 Answers

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

like image 197
Linda Paiste Avatar answered Nov 02 '22 22:11

Linda Paiste