TypeScript readline.createInterface引发exception

我正在尝试创build一个电子应用程序。 在main.ts中我构build了我认为是相当简单的类(下面),但是构造函数不会运行。 对readline.createInterface(inputStream)的调用失败。 看来运行时并不认为调用fs.createReadStream返回的variables有一个名为'on'的函数。

例外是

Uncaught Exception: TypeError: Cannot read property 'on' of undefined at new Interface (readline.js:142:10) at Object.exports.createInterface (readline.js:28:10) at new FileMonitor (/Users/mikedice/code/electron-rm/app/FileMonitor.js:13:38) at App.Main.onReady (/Users/mikedice/code/electron-rm/app/Main.js:27:23) at emitTwo (events.js:111:20) at App.emit (events.js:191:7) 

这是生成它的代码块。

 import readline = require("readline"); import fs = require("fs"); export class FileMonitor { constructor(public filePath:string){ if (!fs.existsSync(filePath)){ console.log(`file does not exist at path ${filePath}`); return; } var inputStream = fs.createReadStream(filePath); var readInterface = readline.createInterface(inputStream); readInterface.on('line', (val)=>{ // when new lines arrive we can publish an event to listeners console.log(val); }); } } 

我认为,问题是在/Users/mikedice/code/electron-rm/node_modules/@types/node/index.d.ts文件中,fs.createReadStream返回一个ReadStream对象,并在同一个文件中readline.createInterface期望一个NodeJS.ReadableStream和TypeScript将这两种types视为不同,即使运行时期望它们是相同的东西。 我不确定这个问题的解决scheme是什么,总的来说,这种types的问题。 看起来这种语言的types系统与运行时不一致,但不知道为什么运行时不能解决执行代码时的问题。 有什么build议么?

 export function createReadStream(path: string | Buffer, options?: { flags?: string; encoding?: string; fd?: number; mode?: number; autoClose?: boolean; start?: number; end?: number; }): ReadStream; export function createInterface(input: NodeJS.ReadableStream, output?: NodeJS.WritableStream, completer?: Completer | AsyncCompleter, terminal?: boolean): ReadLine; 

您需要修改您的readline.createInterface()选项调用以传递具有属性input.readline的对象。 createInterface()需要一个分配了input和/或输出stream的对象。

 readline.createInterface({input: inputStream});