You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since 4.0.1 we have the the following typing issues with EventEmitter that we did not have with 4.0.0. We think it probably has something to do with e84ca51#diff-b52768974e6bc0faccb7d4b75b162c99R122.
Please see code below. Works on 4.0.0, gives type errors on 4.0.1.
Thank you!
import EventEmitter from "eventemitter3";
export enum EventFoo {
ERROR = "error",
PLAY = "play",
}
export enum EventBar {
AD_LOADED = "adLoaded",
AD_STARTED = "adStarted",
}
interface PayloadFoo {
[EventFoo.ERROR]: [{ error: Error }];
[EventFoo.PLAY]: [{}];
}
interface PayloadBar {
[EventBar.AD_LOADED]: [{}];
[EventBar.AD_STARTED]: [{}];
}
type Payload = PayloadFoo & PayloadBar;
const emitter = new EventEmitter<Payload>();
emitter.emit(EventFoo.ERROR, { error: new Error() });
emitter.on(EventFoo.ERROR, (payload) => {
console.log(payload.error);
});
type WrappedApi = {
on<K extends keyof Payload>(name: K, fn: (...args: Payload[K]) => void): void;
};
const api: WrappedApi = {
on(name, fn) {
// TYPE ERROR: Argument of type '(...args: Payload[K]) => void' is not assignable to parameter of type 'K extends EventFoo | EventBar.AD_LOADED ? Handler<Payload[K], void> : never'.ts(2345)
emitter.on(name, fn);
return emitter;
},
};
(Object.keys(EventFoo) as [keyof typeof EventFoo]).forEach((eventName) => {
api.on(EventFoo[eventName], (payload) => {
console.log(payload);
});
});
(Object.keys(EventFoo) as [keyof typeof EventFoo]).forEach((eventName) => {
// TYPE ERROR: Parameter 'payload' implicitly has an 'any' type.ts(7006)
emitter.on(EventFoo[eventName], (payload) => {
console.log(payload);
});
});
The text was updated successfully, but these errors were encountered:
Since 4.0.1 we have the the following typing issues with EventEmitter that we did not have with 4.0.0. We think it probably has something to do with e84ca51#diff-b52768974e6bc0faccb7d4b75b162c99R122.
Please see code below. Works on 4.0.0, gives type errors on 4.0.1.
Thank you!
The text was updated successfully, but these errors were encountered: