2

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"
  }
}
4
  • What does you tsconfig.json file look like? Commented Mar 22, 2021 at 18:15
  • I added it to the question. Commented Mar 22, 2021 at 18:16
  • 1
    This is working on my machine. What is the location of the opened stream.d.ts ? Please include your package.json too. Commented Mar 22, 2021 at 18:24
  • Added package.json and the stream.d.ts location is: myprojectfolder/node_modules/@type/node/stream.d.ts Commented Mar 25, 2021 at 17:01

2 Answers 2

4

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"])
Sign up to request clarification or add additional context in comments.

1 Comment

Readable.from automatically sets objectMode to true, if you were expecting the chunks to be buffers, you'd have to set it yourself like: Readable.from([str], { objectMode: false })
0

In my case, I had to explicitly specify @latest to resolve the issue:

npm install --save-dev @types/node@latest

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.