I am working on defining an enum that contains a list of routes.
enum ServerRoutes {
WHISPER_SECRET = 0,
SHOUT_SECRET = 1,
SHOUT_SECRET_MULTIPLE_TIMES = 2
}
My goal is to develop a ServerRouter that ensures all routes are handled at compile-time. I want to accomplish this by using generics so that I can later create a ClientRouter
type Router<T> = { [route in keyof T]: () => void }
type ServerRouter = Router<ServerRoutes>;
Up to this point, everything compiles without any issues. However, when I try to implement the ServerRouter, I encounter an error. Here is the code snippet causing the error:
const serverRouter: ServerRouter = {
WHISPER_SECRET: () => {},
SHOUT_SECRET: () => {},
SHOUT_SECRET_MULTIPLE_TIMES: () => {}
}
Here is the specific error message:
Type '{ WHISPER_SECRET: () => void; SHOUT_SECRET: () => void; SHOUT_SECRET_MULTIPLE_TIMES: () => void; }' is not assignable to type 'ServerRoutes'.
Unfortunately, the error provides little explanation beyond "not assignable". How can I troubleshoot and resolve this issue or explore alternative strategies to achieve my objective?