在 TypeScript 中将字符串转换为可读
Convert String to Readable in TypeScript
我想这样做:How to create streams from string in Node.Js? 在 TypeScript 中。
我尝试了以下操作:
import * as stream from 'stream'
dataStream = stream.Readable.from(["My String"])
这会导致错误:属性 'from' 在类型 'typeof Readable' 上不存在。
import { Readable } from 'stream'
dataStream = Readable.from(["My String"])
这会导致错误:属性 'from' 在类型 'typeof Readable' 上不存在。
我检查了 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"
}
}
首先的问题是我安装了一个旧的@types/node模块,我更新了它,现在它可以工作了。
npm install @types/node
这是适用于旧版本的解决方法:
import { Readable } from 'stream'
/* tslint:disable-next-line: no-string-literal */
const dataStream = Readable['from'](["My String"])
我想这样做:How to create streams from string in Node.Js? 在 TypeScript 中。
我尝试了以下操作:
import * as stream from 'stream'
dataStream = stream.Readable.from(["My String"])
这会导致错误:属性 'from' 在类型 'typeof Readable' 上不存在。
import { Readable } from 'stream'
dataStream = Readable.from(["My String"])
这会导致错误:属性 'from' 在类型 'typeof Readable' 上不存在。
我检查了 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"
}
}
首先的问题是我安装了一个旧的@types/node模块,我更新了它,现在它可以工作了。
npm install @types/node
这是适用于旧版本的解决方法:
import { Readable } from 'stream'
/* tslint:disable-next-line: no-string-literal */
const dataStream = Readable['from'](["My String"])