Given a Typescript interface and a class extending Node.js EventEmitter, is it possible to define custom listeners that give typesafe check for the function arguments?
Given the following example:
import { EventEmitter } from 'events';
interface Payload {
id: string;
weight: number;
}
class CustomEventEmitter extends EventEmitter {
constructor() {
super();
this.on('my_event', (data) => {
// I would like data to be implicitly inferred as Payload type
console.log(data.weight); // This should compile
console.log(data.something); // This should not compile
});
}
}
A node EventEmitter listener is defined as (...args: any[]) => void), I would like to override the any[] type and use custom defined types instead. Is it possible?