I am currently working on creating a function that can handle specific data types ("resource") along with an array of arrays containing call objects and corresponding callbacks for when the calls finish ("handlers").
function useHandleResource<
R extends ReturnType<typeof useApi>,
H extends [ReturnType<typeof useCall>, ((data: R['data'], cp: H[number]['0']['callParam']) => R['data'])?][]
>(
resource: R,
handlers: H
)
The result obtained from "useApi" includes a "data" property of a generic type.
The outcome of "useCall" has a "callParam" property of a generic type.
When utilizing this function, I expect TypeScript to provide me with the correct types for the callback parameters.
useHandleResource(
testResource(), // "data" is of type "string[]"
[
[
testCall(), // "callParam" is of type "string",
// expected types: "string[]" for data and "string" for cp derived from callParam in testCall
(data, cp) => []
]
]
);
Although there are no errors thrown, the "cp" parameter in the callback becomes "unknown"...
My goal is to pass multiple handlers, each specifying the corresponding type for "cp".
[
[
testCall(), // "callParam" is of type "string",
// expecting cp to be of type "string"
(data, cp) => [cp]
],
[
otherTestCall(), // "callParam" is of type "number",
// should have cp as type "number"
(data, cp) => [cp.toString()]
]
]