#Type for combined functions

2 messages · Page 1 of 1 (latest)

jolly osprey
#

Hi , I'm trying to make a type that accepts a dict/object of user defined functions of type (...args: unknown[]) => Promise<unknown> and will return a function of type (functionId: E,...args: [infered from args FunctionMap[E]]) => Promise<[infered from return type of FunctionMap[E]>
right now I have

export type BatchedScript<ScriptMap extends { [key: string]: (...args: unknown[]) => Promise<unknown>},Id extends keyof ScriptMap = keyof ScriptMap> = (functionId: Id,...args: Parameters<ScriptMap[Id]>) => ReturnType<ScriptMap[Id]>

declare const c: BatchedScript<{
    foo : () => Promise<boolean>;
    bar: () => Promise<{
        nested: boolean;
    }>
}>;

but when I do c("foo") the return type says

Promise<boolean> | Promise<{
        nested: boolean;
    }>``` 
instead of `Promise<boolean>`
#

nvm solved it with this

export type BatchedScript<ScriptMap extends { [key: string]: (...args: unknown[]) => Promise<unknown>},Id extends keyof ScriptMap = keyof ScriptMap> = <K extends Id>(functionId: K,...args: Parameters<ScriptMap[K]>) => ReturnType<ScriptMap[K]>