When trying to call registerCallback()
below, seems there’s an issue with the type of the argument of the callback itself. The reported error indicates that the types of the event
arg are incompatible, which is unexpected since it seems the types would actually resolve to the same type?
type Events = { type: "e1" } | { type: "e2" };
function registerCallback(cb: (event: Events) => void) {
console.log(cb);
}
type AddListener = <const EventName extends Events['type']>(eventName: EventName, cb: (event: Extract<Events, {
type: EventName;
}>) => void) => () => void;
export const addListener: AddListener = (eventName, cb) => {
console.log(eventName);
registerCallback(cb);
return function removeEventListener() {/*...*/ };
}
(playground link)
1