You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When doing client.suggest(...), i want to define a type to model what the response is. Looking at the package, both SuggestOptions and client.suggest are generics. However, no matter what I do, the resulting array is always {} | Pick<MyModel, "field name"> | .... I don't think I should need to cast the result into MyModel (ie: result.document as MyModel). Should just work.
Here's an example of what is happening. I discovered this while converting my search over to a suggest. once i converted, i got some typescript errors. i believe it's an issue with the type returned by the suggest method.
export interface ISearchResult {
description?: string;
name?: string;
}
private async _search(term: string): Promise<ISearchResult[]> {
if (!term) return [];
const items: ISearchResult[] = [];
const options: SuggestOptions<ISearchResult> = {
top: 10,
useFuzzyMatching: true,
searchFields: ['name'],
select: ['name', 'description'],
};
const results = await this._searchClient.suggest(term, 'suggestor-name', options);
for await (const result of results.results) {
const document = result.document; // TS says this is {} | Pick<ISearchResult, "name"> | ...
console.log(document); // actually ISearchResult
if (document.name) { ... } // TS2339: Property name does not exist on type Pick<ISearchResult, "name" | ....
items.push(document);
}
return items;
}
If i give a type to suggest, i get
TS2344: Type ISearchResult does not satisfy the constraint 'description' | 'name' | undefined
this._searchClient.suggest<ISearchResult>(...)
However, if you use searchClient.search, it acts as it should.
private async _search(term: string): Promise<ISearchResult[]> {
if (!term) return [];
const items: ISearchResult[] = [];
const options: SearchOptions<ISearchResult> = {
top: 10,
};
const results = await this._searchClient.search(term, options);
for await (const result of results.results) {
const document = result.document; // intellisense has it as ISearchResult
console.log(document); // actually ISearchResult
if (document.name) { ... } // no typescript error
items.push(result.document);
}
return items;
}
Is this just a bug on the suggest method or is client.suggest supposed to act differently?
The text was updated successfully, but these errors were encountered:
marc-wilson
changed the title
client.suggest type is not incorrect
client.suggest return type seems to be incorrect / behaves different from client.search
Feb 21, 2025
Describe the bug
When doing
client.suggest(...)
, i want to define a type to model what the response is. Looking at the package, bothSuggestOptions
andclient.suggest
are generics. However, no matter what I do, the resulting array is always{} | Pick<MyModel, "field name"> | ...
. I don't think I should need to cast the result intoMyModel
(ie:result.document as MyModel
). Should just work.Here's an example of what is happening. I discovered this while converting my search over to a suggest. once i converted, i got some typescript errors. i believe it's an issue with the type returned by the suggest method.
If i give a type to suggest, i get
However, if you use
searchClient.search
, it acts as it should.Is this just a bug on the
suggest
method or isclient.suggest
supposed to act differently?The text was updated successfully, but these errors were encountered: