Apologies in advance if this question has been asked before, but I am struggling to phrase it in a way that leads me to a solution.
I have an enum type:
enum Events {
FOO = 'foo',
BAR = 'bar'
}
There is a function that accepts another function as a parameter. The function it accepts has a type EventHandler
export const wrapEventFn = async (fn: EventHandler): Promise<unknown> => {
...
The type definition for EventHandler
is:
type EventHandler = (event: Events) => Promise<void>
Now, the functions that I pass are restricted to handling specific events. For example:
export const actionHandler = async (
event: Events.FOO
): Promise<void> => {...
However, when trying to use the above code, TypeScript throws an error:
wrapEventFn(actionHandler) // actionHandler is underlined w error
Error
Argument of type '(event: Events.FOO) => Promise<void>' is not assignable to parameter of type 'EventHandler'.
Types of parameters 'event' and 'event' are incompatible.
Type 'Events' is not assignable to type
How can I resolve this issue?