I'm currently working on creating comprehensive typings for the https://www.npmjs.com/package/keypress npm package.
While I have successfully typed out the basic function, I am facing challenges in extending the declaration of process.stdin
to align with the requirements of the package for better IntelliSense support.
The key issue lies in the fact that keypress
introduces an
event: process.stdin.on('keypress', listener)
, where the listener
should have a signature like (ch: Ch, key?: Key) => void
.
I am uncertain whether I should extend the Process
interface or possibly the ReadStream
interface because, theoretically speaking, this functionality could apply to any ReadStream
.
Moreover, I am unsure if it is feasible to notify TypeScript that keypress()
is being invoked on a ReadStream
that incorporates this new functionality. Is there a way to restrict access unless keypress()
has been called? While I suspect the answer to be negative, I thought it was worth inquiring about.
The code snippet below showcases my current attempt:
declare module "keypress" {
export default function keypress(stream: NodeJS.ReadStream): void;
export type Ch = any; // TODO
export type Key = {
// TODO
ctrl: any;
name: string;
};
namespace global {
namespace NodeJS {
interface Process {
stdin: NodeJS.ReadStream & {
fd: 0; // From official node types
on(event: "keypress", listener: (ch: Ch, key?: Key) => void): this;
};
}
}
}
}
Despite trying several variations of this approach, I find myself stuck and would greatly appreciate any guidance or pointers in resolving this issue. Thank you! Cheers!