node.js - ویژگی readline در TypeScript + NodeJS وجود ندارد


من روی یک پروژه کوچک در تایپاسکریپت با tsc -v 2.4.2 و Node v6.10.3 کار میکنم.

من میخواهم فشردن کلیدها را در خط فرمان ثبت کنم، بنابراین سعی کردم import * as readline from 'readline' را انجام دهم و سپس بعداً از readline.emitKeyPressEvents(process.stdin) استفاده کنم، اما خطا میدهد که ویژگی emitKeyPressEvents در 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 با افزودن آن به DefinitelyTyped ارسال کردهام. ممکن است پردازش آن مدتی طول بکشد (حدود یک هفته، اگر همه چیز خوب پیش برود)، اما در این میان میتوانید کد خود را با اضافه کردن یک اعلام محلی به فایلی که شامل InputManager است، بررسی نوع کنید:

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