类型“ ReadableStream <any>”的参数不能分配给类型“ ReadableStream”的参数

时间:2020-08-28 08:24:09

标签: javascript node.js typescript stream

我想从Blob中获得一个可读格式。

import {Readable} from 'stream';

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

我收到此错误

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.

我使用Node 14 angular 10和打字稿

1 个答案:

答案 0 :(得分:2)

此代码中有两个不同的 ReadableStream 定义,它们彼此不兼容。


Blob 来自 DOM 类型。 Blob.stream() returns ReadableStream<any> 定义在 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 来自 ReadableStream 中 NodeJS 定义的 @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


您的代码尝试将 DOM ReadableStream 分配给需要 NodeJS ReadableStream 的函数。您收到一条错误消息,告诉您此 Node 版本预期的所有属性,但这些属性不存在于 DOM 版本变量 data.stream() 中。