I have an enum with a list of routes.
enum ServerRoutes {
WHISPER_SECRET = 0,
SHOUT_SECRET = 1,
SHOUT_SECRET_MULTIPLE_TIMES = 2
}
I'd like to create a ServerRouter that has compile-time guarantees that every route is handled. I'd like to do this using a generic so that I can later on create a ClientRouter
type Router<T> = { [route in keyof T]: () => void }
type ServerRouter = Router<ServerRoutes>;
So far so good. Everything above compiles fine. However, when I try to actually implement a ServerRouter, I get an error. Here is the code that generates the error:
const serverRouter: ServerRouter = {
WHISPER_SECRET: () => {},
SHOUT_SECRET: () => {},
SHOUT_SECRET_MULTIPLE_TIMES: () => {}
}
And here is the error:
Type '{ WHISPER_SECRET: () => void; SHOUT_SECRET: () => void; SHOUT_SECRET_MULTIPLE_TIMES: () => void; }' is not assignable to type 'ServerRoutes'.
Unfortunately, the error doesn't really explain what the problem is other than "not assignable". How can I either fix my code or use another strategy to acheive the same goal?