[@types/node] ReadableStream type definition here is not compatible with TypeScript's #65542
-
This will fail since types are incompatible const response = await fetch('xxx')
stream.Readable.fromWeb(response.body!) // fail! Error:
Node's version: DefinitelyTyped/types/node/stream/web.d.ts Line 135 in 16a2bef TypeScript's version in |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 6 replies
-
Thanks for the discussion about "node", some useful links for everyone: Pinging the DT module owners: @microsoft, @DefinitelyTyped, @jkomyno, @alvis, @r3nya, @btoueg, @smac89, @Touffy, @DeividasBakanas, @eyqs, @Hannes-Magnusson-CK, @hoo29, @kjin, @ajafff, @islishude, @mwiktorczyk, @mohsen1, @n-e, @galkin, @parambirs, @eps1lon, @SimonSchick, @ThomasdenH, @WilcoBakker, @wwwy3y3, @samuela, @kuehlein, @bhongy, @chyzwar, @trivikr, @yoursunny, @qwelias, @ExE-Boss, @peterblazejewicz, @addaleax, @victorperin, @ZYSzys, @nodejs, @LinusU, @wafuwafu13, @mcollina, @Semigradsky. |
Beta Was this translation helpful? Give feedback.
-
What's the TypeScript version? |
Beta Was this translation helpful? Give feedback.
-
Looks like related to microsoft/TypeScript#29867 |
Beta Was this translation helpful? Give feedback.
-
@julienvincent @wangzishi Please, take a look |
Beta Was this translation helpful? Give feedback.
-
The root cause of this problem is that typescript has removed the type definition that generates the async My personal advice is as follows:
import { default as stream } from 'node:stream'.
import type { ReadableStream } from 'node:stream/web'.
const response = await fetch('xxx')
stream.Readable.fromWeb(response.body as ReadableStream<Uint8Array>)
ReadableStream.prototype[Symbol.asyncIterator] = async function* () {
const reader = this.getReader()
try {
while (true) {
const {done, value} = await reader.read()
if (done) return
yield value
}
}
finally {
reader.releaseLock()
}
} 中文说明导致这个问题的根本原因是因为 typescript 移除了为 ReadableStream 生成 async iterator 的类型定义。他们这么做也是因为 chromium 对此功能的一直没有实现。 而 Nodejs 中的已经实现了这个功能,所以导致了两者的类型定义差异。 我的个人有如下建议:
import { default as stream } from 'node:stream';
import type { ReadableStream } from 'node:stream/web';
const response = await fetch('xxx')
stream.Readable.fromWeb(response.body as ReadableStream<Uint8Array>)
ReadableStream.prototype[Symbol.asyncIterator] = async function* () {
const reader = this.getReader()
try {
while (true) {
const {done, value} = await reader.read()
if (done) return
yield value
}
}
finally {
reader.releaseLock()
}
} |
Beta Was this translation helpful? Give feedback.
-
Would be nice to bump this up in priority for Chrome and WebKit - with streams gaining popularity and being standard, it is a shame you can't use idiomatic |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
The root cause of this problem is that typescript has removed the type definition that generates the async
iterator for ReadableStream. They also did this because chromium has never implemented this functionality, but
Nodejs already implements this, so the type definition difference is caused.
My personal advice is as follows: