I have a REST api interface like this:
interface APIs {
artist: {
POST: { req: TypeA, resp: TypeB}
DELETE: { req: TypeC, resp: TypeD },
}
location: {
GET: { req: TypeE, resp: TypeF }
}
}
That I want to convert to a set of functions like this:
const server: Server<APIs> = {
postArtist: (req: TypeA) => TypeB,
deleteArtist: (req: TypeC) => TypeD,
getLocation: (req: TypeE) => TypeF,
}
I have managed to create almost create a Server<T>
type however I'm not able to reference the POST
|DELETE
|GET
keys used in the template literal. They appear as the key of T[Key]
below so that it can be used on the right side where the ?
is.
type Server<T> = {
[Key in keyof T as `${Lowercase<keyof T[Key] & string>}${Capitalize<Key & string>}`]: (req: T[Key][?]) => void
}
// |^=How to get this=^| and use it here -^^
How do I reference the key that was used to generate the function name so that I can reference it on the right side.