javascript - 在 typescript 中访问 Nodejs 流的 highWaterMark 属性给出 : "Property ' _readableState' does not exist"error

标签 javascript node.js typescript

我的问题是关于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/

相关文章:

javascript - 使用 tf.browser.toPixels() 时内存泄漏

javascript - jquery-nice-select 与 vue.js 不是一个函数

node.js - 使用 pg-promise 的连接池

javascript - 错误 TypeError : _v. context.$implicit.toggle 不是函数

javascript - 如何让回车键点击搜索按钮

javascript - 使用jest+enzyme测试,mock函数无故调用两次

node.js - Azure 函数集 Node 版本

node.js - Mongoose 模式如何添加数组

typescript - 如何使用 Typescript 在函数中包含输出参数?

typescript - TypeScript 中 undefined 的协方差