I have data structure like this:
const endpoints = {
async Login: (params) => { ... }
async Register: (params) => { ... }
}
Now I want to specify that every item in this object must accept params object and return a promise.
I can do something like this:
interface EndpointMap {
[endpointName: string]: (
params: Record<string, any>
) => Promise<any>;
}
This works well. But there's a downside to this.
If I do elsewhere that for example endpoint : keyof typeof endpoints the result would only be string. If I remove the EndpointMap interface, I'd get a String Union of all the keys on the endpoint object. Much better!
Is there a way to have the best of both worlds?
Thanks!
{ foo: (params) =>{} }as well. If you are aware about keys, you should specify them. That would even help TS in giving you intellisense.