Typescript definition for a function which returns based on nested keys in parameter

Cuel

I'm trying to write a declaration file for a library we're using without any means to modify it.

The way it works is that you can send a configuration object, the return will be based on some keys inside of it

const args = {
  // ...
  resources: {
    One: {
      query: { url: 'value' }
    }
  } 
}

library(args)

Invoking it returns an object with the deeply nested keys of resources as functions

const ret = {
  One: {
    query: async (query) => <data>
 }
}

// ret.One.query(args) => data

Ideally each <data> would also be typed, I'm not sure if this is possible due to the dynamic keys? I've tried a couple of approaches using keyof the parameters without any luck

Edit: Updated example

const config = {
  // ...
  resources: {
    User: {
      find: { url: 'http://example.com/userapi' }
    },
    Dashboard: {
      update: { method: 'post', url: 'http://example.com/dashboardapi'}
    }
  } 
}

const services = serviceCreator(config)

// service creator turns the supplied resources object into promise functions

await services.User.find({id: '123'}) // Promise<User>
await services.Dashboard.update({user: '123', action: 'save'}) // Promise<Dashboard>

Mu-Tsun Tsai

Without further information, I'm guessing that you're looking for something like this:

type Arg<T> = { resources: T; }
type Ret<T> = {
    [K in keyof T]: {
        query: (...query: any[]) => Promise<any>;
    }
}
declare const library: <T>(arg: Arg<T>) => Ret<T>;

Let's test it to see if it works:

const args = {
    resources: {
        One: {
            query: { url: 'value' }
        }
    }
}
var ret = library(args);

async function test() {
    await ret.One.query();  // OK
}

You can try it in this playground.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

TypeScript function return type based on input parameter

Typescript definition for subclass as parameter

Typescript: define function, which transforms an object, keeping keys

instanceof function parameter returns false in Typescript/Angular

Nested Generics in function definition in TypeScript

Can I reuse the parameter definition of a function in Typescript?

Typescript function definition to ensure second parameter is same type as first parameter

Typescript: Declare function that returns a nested array

Implementing a function which returns function definition as string

Describe typescript function which returns modified object with keys from array

Conditionally type function based on parameter value in typescript

Typescript definition for values function which always return array of argument

Call nested function based on input parameter

Can I make a parameter optional in a Typescript definition and function call?

Typescript: enum keys as parameter to function

Filtering a nested Object based on keys which match values in an array

Collapse nested keys in TypeScript?

Typescript function arguments based on types with enums as keys

Typescript: Dictionary type with keys which belongs to a nested type

Typescript function parameter type based on another parameter

Typescript function that accepts array of strings and returns object with given strings as keys

Passing a function which returns array as a parameter

TypeScript - function return type based on parameter type

TypeScript dynamic Function definition based on functions argument

Write function, which returns an unique identifier for parameter.(Java Script)

Allow Typescript to understand which keys on an object were passed into function

Typescript function that returns a dict with keys based on function input

typescript function definition confusion

control returned type based function parameter in typescript

TOP Ranking

HotTag

Archive