Let's say I have this TypeScript code:
enum EVENT {
FIRST_EVENT = "game:first_encounter",
}
const EventHandler: keyof typeof EVENT = {
[EVENT.FIRST_EVENT]: (data: any) => {
console.log(`Hello there, ${data.blue_player}`);
}
}
const data = { blue_player: 'Blue McBlue', event: EVENT.FIRST_EVENT}
const eventName: EVENT = data.event;
EventHandler[eventName](data);
I have an error with EventHandler near the top.
Type '{ "game:first_encounter": (data: any) => void; }' is not assignable to type '"FIRST_EVENT"'
I also have an error of with eventName on the last line with:
Element implicitly has an 'any' type because index expression is not of type 'number'
See what I mean? How can I correctly type this?
const EventHandler: keyof typeof Eventseems incomplete, and refers to the DOMEventtype not toEVENT. What did you actually mean there?EventHandlervariable.EventHandlershould have?