If you've followed along with the documentation example for Conditional Requests for React InstantSearch and received a TypeScript error the code below should remove that error.
const algoliaClient = algoliasearch(
"latency",
"6be0576ff61c053d5f9a3225e2a90f76"
);
const searchClient = {
...algoliaClient,
search<TObject>(requests) {
if (requests.every(({ params }) => !params.query)) {
return Promise.resolve({
results: requests.map(() => ({
hits: [],
nbHits: 0,
nbPages: 0,
page: 0,
processingTimeMS: 0,
hitsPerPage: 0,
exhaustiveNbHits: false,
query: "",
params: ""
}))
});
}
return algoliaClient.search<TObject>(requests);
}
};
In TypeScript the call to algoliaClient.search
is expected to pass a type in a generic. Forwarding it from the overridden method should remove the error. The TObject type can be found here in the API Client Github repo. Here is a CodeSandBox showing this in action.