我正在使用tsc -v 2.4.2
和Node v6.10.3的打字稿中的一个小项目。
我想在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)我们:
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);
}
});
}
}
node
键入中确实缺少该方法。它的正确名称实际上是emitKeypressEvents
(带有低案例p
),但也缺少该名称。我认为这是一个简单的疏忽,所以我提交了一份PR,并加上了肯定的途径。这可能需要一段时间才能进行处理(大约一周,如果一切顺利),但是与此同时,您可以通过将本地声明添加到包含 InputManager
的文件中来键入检查代码:
declare module 'readline' {
export function emitKeypressEvents(stream: NodeJS.ReadableStream, interface?: ReadLine): void;
}