【问题标题】:Convert String to Readable in TypeScript在 TypeScript 中将字符串转换为可读
【发布时间】:2021-06-19 08:49:55
【问题描述】:

我想这样做:How to create streams from string in Node.Js? in TypeScript。

我尝试了以下方法:

import * as stream from 'stream'
dataStream = stream.Readable.from(["My String"])

这会导致错误:类型'typeof Readable'上不存在属性'from'。

import { Readable } from 'stream'
dataStream = Readable.from(["My String"])

这会导致错误:类型'typeof Readable'上不存在属性'from'。

我检查了stream.d.ts,静态方法在那里:

declare module "stream" {
    import * as events from "events";

    class internal extends events.EventEmitter {
        pipe<T extends NodeJS.WritableStream>(destination: T, options?: { end?: boolean; }): T;
    }

    namespace internal {
        class Stream extends internal {
            constructor(opts?: ReadableOptions);
        }

        interface ReadableOptions {
            highWaterMark?: number;
            encoding?: BufferEncoding;
            objectMode?: boolean;
            read?(this: Readable, size: number): void;
            destroy?(this: Readable, error: Error | null, callback: (error: Error | null) => void): void;
            autoDestroy?: boolean;
        }

        class Readable extends Stream implements NodeJS.ReadableStream {
            /**
             * A utility method for creating Readable Streams out of iterators.
             */
            static from(iterable: Iterable<any> | AsyncIterable<any>, options?: ReadableOptions): Readable;
...

那么我在这里错过了什么?

我的 tsconfig.json:

{
    "compilerOptions": {
        "lib": [
          "es5",
          "es6",
          "es2019"
      ],
      "target": "es5",
      "moduleResolution": "node",
      "module": "commonjs",
      "declaration": true,
      "outDir": "./lib",
      "emitDecoratorMetadata": true,
      "experimentalDecorators": true,
      "sourceMap": true
    },
    "include": ["src"],
    "exclude": ["node_modules", "**/__tests__/*"]
  }

我的 package.json:

{
  "name": "apprestservice",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "ts-node __tests__/index.ts",
    "build": "tsc",
    "format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
    "lint": "tslint -p tsconfig.json",
    "start": "ts-node src/index.ts"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^8.0.29",
    "@types/express": "^4.17.7",
    "@types/jsonstream": "^0.8.30",
    "JSONStream": "^1.3.5",
    "express": "^4.17.1",
    "libmodelcvescanner": "file:../libModelCveScanner",
    "mysql": "^2.18.1",
    "reflect-metadata": "^0.1.13",
    "typeorm": "0.2.25",
    "tslint": "^6.1.2",
    "typescript": "^3.9.6"
  },
  "devDependencies": {
    "prettier": "^2.0.5",
    "ts-node": "^3.3.0",
    "tslint": "^6.1.2",
    "tslint-config-prettier": "^1.18.0",
    "typescript": "3.3.3333"
  }
}

【问题讨论】:

  • 你的 tsconfig.json 文件是什么样的?
  • 我将它添加到问题中。
  • 这在我的机器上工作。打开的stream.d.ts 的位置是什么?请也包含您的 package.json。
  • 添加了 package.json 和 stream.d.ts 位置:myprojectfolder/node_modules/@type/node/stream.d.ts

标签: typescript nodejs-stream


【解决方案1】:

首先的问题是我安装了一个旧的@types/node 模块,我对其进行了更新,现在它可以工作了。

npm install @types/node

这是适用于旧版本的解决方法:

import { Readable } from 'stream'
/* tslint:disable-next-line: no-string-literal */
const dataStream = Readable['from'](["My String"])

【讨论】:

    猜你喜欢
    • 2022-01-17
    • 2019-05-12
    • 2014-09-20
    • 1970-01-01
    • 2019-03-10
    • 1970-01-01
    • 2019-04-29
    相关资源
    最近更新 更多