I am trying to create type information for my event listeners. As all event listeners are set on the same .on() function, I am using generics.
type Name = "error" | "connected";
type Callback = {
error: (err: Error) => void,
connected: (err: number) => void,
};
function on<T extends Name>(eventName: T, callback: Callback[T]): void { }
on("error", (err) => err.stack);
on("connected", (err) => err.stack);
I would expect the above to give me an error for the connected event as I attempt to use a number as an Error, however I get no type hinting at all for my callback functions.
However if all function definitions in Callback match, it does begin to work. As below:
type Callback = {
error: (err: Error) => void,
connected: (err: Error) => void,
};
GIF of what I mean from VS code:
Am I doing something wrong?
