如何获取Algolia搜索结果的类型?

How to get the type of Algolia search results?

我的 firestore 正在连接 algoliasearch。我正在使用 typescriptnextjs

我尝试得到如下结果

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

然后我将结果存储在一个状态中,这样我就可以将它们作为道具传递给另一个组件。 但是,我不断收到有关类型 ObjectWithObjectID 的错误。我已经安装了 @types/algoliasearch,但我似乎找不到 ObjectWithObjectID 的道具。有什么解决办法吗?

我不得不手动输入我的 'hit' 的样子,例如:

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)
});

在索引上调用 .search 允许将类型作为泛型传入。这会将返回的 hits 类型指定为与给定类型相同的类型。

const content = await index.search<AlgoliaHit>(searchText, {
  hitsPerPage: HITS_PER_PAGE,
  facetFilters: ["cpi_visibility:published"].filter(Boolean)
});

这还将为 content 提供 SearchResponse<AlgoliaHit> 类型,这将为您提供一些关于 content;

上可用属性的不错的自动完成功能
content.hits.map(hit => 
  console.log(hit, "<- this has every property that you've defined in AlgoliaHit")
)