2017-09-13 144 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