我正在使用 TypeScript 开发一个小项目tsc -v 2.4.2
和节点 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)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);
}
});
}
}
确实缺少该方法node
打字。它的正确名称实际上是emitKeypressEvents
(带有小写p
),但那个也不见了。我认为这是一个简单的疏忽,所以我提交了一份PR https://github.com/DefinitelyTyped/DefinitelyTyped/pull/19733加上DefinitelyTyped。这可能需要一段时间来处理(如果一切顺利的话,大约需要一周),但与此同时,您可以通过向包含以下内容的文件添加本地声明来键入检查代码:InputManager
:
declare module 'readline' {
export function emitKeypressEvents(stream: NodeJS.ReadableStream, interface?: ReadLine): void;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)