gpt4 book ai didi

javascript - 'ReadableStream' 类型的参数不可分配给 'ReadableStream' 类型的参数

转载 作者:行者123 更新时间:2023-12-03 12:12:52 36 4
gpt4 key购买 nike

我想从 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 和 typescript

最佳答案

这段代码中有两种不同的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() 中。

关于javascript - 'ReadableStream<any>' 类型的参数不可分配给 'ReadableStream' 类型的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63630114/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com