I’m struggling a little with TypeScript typings (w...
# community-help
d
I’m struggling a little with TypeScript typings (which is a surprise given that the client library is written in typescript). I have a type for my document:
Copy code
import { DocumentSchema, SearchResponse, SearchResponseHit } 
    from "../node_modules/typesense/src/TypeSense/Documents";

export interface PPDocument extends DocumentSchema  {
  title: string,
  code:  string,
  id:    string,
}

export type PPSearchResponse  = SearchResponse<PPDocument>
export type PPSearchResponseHit = SearchResponseHit<PPDocument>
The first surprise is that the various
Document
types don’t seem to be available at the top-level. I then have code that performs the search, updating a preact state with the result:
Copy code
export function doSearch(
  client: SearchClient, 
  query:  string, 
  setResultHolder: StateUpdater<PPSearchResponse>
) {

  let searchParameters: SearchParams = { ... }

  client
    .collections<PPDocument>('ppbooks')
    .documents()
    .search(searchParameters, {})
    .then((result) => { setResultHolder(result) })
    .catch((error) => console.log(error))
}
The problem comes in the
.then
clause. The
result
parameter has been given the type
SearchResponse<{}>
which is incompatible with the expected type of the parameter to
setResultHolder
, which is
SearchResponse<PPDocument>
. I know I’m missing something fundamental. Can anyone show we what that is? Cheers
j
Hmm, could you try something like this:
Copy code
client
    .collections<PPDocument>('ppbooks')
    .documents<PPDocument>()
    .search(searchParameters, {})
    .then((result) => { setResultHolder(result) })
    .catch((error) => console.log(error))
d
documents
expected 0 type arguments…” 😪
j
I think I see the issue, but I’m not a Typescript expert, so I’m sure what the proper fix would be for this. In this line it looks like we’re not passing the generic
T
to
new Collection
. I wonder if it should read
new Collection<T>
and that would fix this issue. @Damian C any thoughts on this?