在打字稿中访问Nodejsstream的highWaterMark属性给出:“Property'_readableState'不存在”错误

我的问题是关于nodejs中的打字稿和stream。 我不想扩展stream基类,并访问highWaterMark选项。

在打字稿中,以下代码起作用(即打印highWaterMark选项)

import * as stream from 'stream'; class ExampleStream extends stream.Readable { constructor() { super(options) console.log(this._readableState.highWaterMark) } _read() {} _write() {} } 

但打字稿给我关于这一行的以下错误信息:

 console.log(this._readableState.highWaterMark) 

['ts]types'ExampleStream'上不存在'_readableState'属性。

我如何解决这个问题?

鉴于_readableState不作为公开API的一部分公开,我不认为它会/应该被添加到节点types中。 为了解决这个问题,你可以把this成任何forms,并使用节点types定义中不可用的proeprties:

 console.log((this as any)._readableState.highWaterMark)