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

1

21

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

Joannajoanne answered 28/8, 2020 at 8:24 Comment(0)
I
31

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

Institution answered 14/3, 2021 at 20:20 Comment(4)
What would be the solution here then? Is there a way to get a ReadableStream<Uint8Array> into the node.js format? I have the same problem except the functionality works, its just the type that is complaining.Nahshun
@Nahshun I think you can use a type assertion? someVar as NodeJS.ReadableStream or possibly someVar as unknown as NodeJS.ReadableStreamInstitution
thanks that seemed to work! I was missing the as unknown chunk.Nahshun
This same error can come up in other contexts, but require different typecasting - see #37615149 for another example of the same issue with a different solution.Hargrove

© 2022 - 2024 — McMap. All rights reserved.