Imagine you have the following TypeScript snippet:
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);
An issue arises with EventHandler
at the top.
Type '{ "game:first_encounter": (data: any) => void; }' is not assignable to type '"FIRST_EVENT"'
Another error occurs with eventName
on the last line stating:
Element implicitly has an 'any' type because index expression is not of type 'number'
If you are curious about a solution, check out this link: Click here
Do you see what's going on? How can I properly define the types in this code?