In search of a solution: I am attempting to develop a function, which we'll refer to as test
, designed to handle nested objects with dynamic keys on the first level. The goal is for this function to automatically suggest the type of method without requiring manual input. Additionally, it should also support generic types for other methods and properties being passed in. See the example below for clarification.
type NestedProperties = {
property: () => any,
anotherProperty: () => any,
key: (...args: any) => string[],
method: (data: ReturnType<Test['property']>) => any,
}
Here's how you can use it:
function test<T extends Record<string, NestedProperties>>(obj: T){}
function key({ productId }: { productId: number }) {
return [ 'list', { productId } ]
}
test({
list: {
property: () => 'string',
anotherProperty: () => 1, // will not result as () => any.
key, // this will also not result as (...args: any) => string[]
method: (data) /* and this will automatically infer type of returntype of list.property*/ {
...do something
}
}
});