Search code examples
typescriptnodejs-stream

Convert String to Readable in TypeScript


I want to do this: How to create streams from string in Node.Js? in TypeScript.

I tryed the folowing things:

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

This leads to the error:Property 'from' does not exist on type 'typeof Readable'.

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

This leads to the error:Property 'from' does not exist on type 'typeof Readable'.

I checked the stream.d.ts and the static method is there:

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;
...

So what am i missing here?

My 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__/*"]
  }

my 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"
  }
}

Solution

  • The problem in the first place was that i had an old @types/node module installed i updated it and now it Works.

    npm install @types/node
    

    Here is the workaround that worked with the older Version:

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