缺少 TypeScript + NodeJS readline 属性 [英] TypeScript + NodeJS readline property missing
问题描述
我正在使用 tsc -v 2.4.2
和 Node v6.10.3 在 TypeScript 中开发一个小项目.
I'm working on a small project in TypeScript with tsc -v 2.4.2
and Node v6.10.3.
我想在 CLI 中捕获按键,所以我尝试 import * as readline from 'readline'
然后使用 readline.emitKeyPressEvents(process.stdin)
,但它抱怨 在 typeof readline 上找不到属性 emitKeyPressEvents
.
I would like to capture keypresses in the CLI, so I tried to import * as readline from 'readline'
and then later use readline.emitKeyPressEvents(process.stdin)
, but it complains that the property emitKeyPressEvents is not found on typeof readline
.
我也做了npm install --save @types/node
.
这是一个 M(N)WE:
Here's a 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 以及肯定打字.这可能需要一段时间来处理(大约一周,如果一切顺利),但与此同时,您可以通过向包含 InputManager
的文件添加本地声明来键入检查您的代码:
The method is indeed missing from the node
typings. Its correct name is actually emitKeypressEvents
(with a lower-case p
), but that one is also missing. I assume this is a simple oversight, so I've submitted a PR with the addition to DefinitelyTyped. This might take a while to process (around a week, if all goes well), but in the mean time you can type check your code by adding a local declaration to the file containing InputManager
:
declare module 'readline' {
export function emitKeypressEvents(stream: NodeJS.ReadableStream, interface?: ReadLine): void;
}
这篇关于缺少 TypeScript + NodeJS readline 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!