I've been working on adjusting the getResults
function to return the correct type, but I'm currently faced with this issue:
interface IResponse<T> {
result: T;
}
type FnType<P, R> = (params: P) => R;
const r1: IResponse<string[]> = { result: ['123'] };
const r2: IResponse<number[]> = { result: [456] };
const getResults = <T, R extends IResponse<T[]>, P>(fn: FnType<P, R>, params: P): T[] => {
return [
...fn(params).result,
...fn(params).result
];
};
const getValue = <T>(r: IResponse<T>) => r;
// The returned type is unknown[]
// How can we modify it to return string[]?
getResults(getValue, r1);
// The returned type is unknown[]
// How can we modify it to return number[]?
getResults(getValue, r2);
This code is simply a demonstration; in a real-world scenario, the getResults
function is utilized for merging results from paginated requests (where fn
acts as a request function).