我的问题是关于nodejs 中的 typescript 和流。我不想扩展流基类并访问 highWaterMark 选项。
在 typescript 中,以下代码有效(即打印 highWaterMark 选项)
import * as stream from 'stream';
class ExampleStream extends stream.Readable {
constructor() {
super(options)
console.log(this._readableState.highWaterMark)
}
_read() {}
_write() {}
}
但是 typescript 给了我以下关于该行的错误消息:
console.log(this._readableState.highWaterMark)
[ts] 类型“ExampleStream”上不存在属性“_readableState”。
我该如何解决这个问题?
最佳答案
鉴于_readableState
没有作为公共(public) API 的一部分公开,我认为它不会/不应该添加到 Node 类型中。要解决此问题,您可以将 this
强制转换为 any 并使用 Node 类型定义中不可用的属性:
console.log((this as any)._readableState.highWaterMark)
关于javascript - 在 typescript 中访问 Nodejs 流的 highWaterMark 属性给出 : "Property ' _readableState' does not exist"error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46233107/