2017-09-13 147 views
1

我正在使用tsc -v 2.4.2和Node v6.10.3来处理TypeScript中的一个小项目。TypeScript + NodeJS readline属性丢失

我想在CLI中捕获按键,所以我试图import * as readline from 'readline',然后用readline.emitKeyPressEvents(process.stdin),但它抱怨说the property emitKeyPressEvents is not found on typeof readline。我也做过npm install --save @types/node

这里的一个M(N)WE:

import * as readline from "readline"; 
import {SIGINT} from "constants"; 

export class InputManager 
{ 
    private _currentStates: Array<IKeyEntity>; 
    private _oldStates: Array<IKeyEntity>; 

    public constructor() 
    { 
     // Throws error, won't compile 
     readline.emitKeyPressEvents(process.stdin); 
    } 

    public handleInput() 
    { 
     if (process.stdin.isTTY) 
      process.stdin.setRawMode(true); 

     process.stdin.on('keypress', (str: string, key: any) => { 
      process.stdout.write('Handling keypress ['+str+']'); 

      if (key && key.ctrl && (key.name == 'c' || key.name == 'l')) 
      { 
       process.kill(process.pid, SIGINT); 
      } 
     }); 
    } 
} 

回答

2

的方法,确实是从分型node失踪。它的正确名称实际上是emitKeypressEvents(使用小写字母p),但那个名称也不存在。我认为这是一个简单的疏忽,所以我提交了一个PR以及DefinitelyTyped。这可能需要一段时间来处理(一个星期左右,如果一切顺利的话),但在平均时间,你可以输入通过增加局部声明包含InputManager文件检查你的代码:

declare module 'readline' { 
    export function emitKeypressEvents(stream: NodeJS.ReadableStream, interface?: ReadLine): void; 
} 
+0

它花了一点今天发布的'@ types/node @ 8.0.36'包含'emitKeypressEvents'。 – Oblosys