Search code examples
reactjstypescriptfull-text-searchnext.jsalgolia

How to get the type of Algolia search results?


My firestore is connecting with the algoliasearch. Im using typescript with nextjs.

I try to get the results as follow

products = index.search(name).then(({hits}) => {
  return hits
})

Then I store the results in a state so I can pass them down as a props to another component. However, I constantly getting an error about type ObjectWithObjectID. I have installed @types/algoliasearch, but I cant seem to find the props for ObjectWithObjectID. Any work around on this?


Solution

  • I had to manually type what my 'hit' looked like, for example:

    type AlgoliaHits = {
      hits: AlgoliaHit[];
    };
    
    export type AlgoliaHit = {
      identifier: string;
      cpi_visibility: string;
      title: string;
      published: string;
    };
    
    const content: AlgoliaHits = await index.search(searchText, {
      hitsPerPage: HITS_PER_PAGE,
      facetFilters: ["cpi_visibility:published"].filter(Boolean)
    });