Skip to content

Commit 1287b3e

Browse files
Merge #1416
1416: types: fix search default type of `search` and `searchGet` r=bidoubiwa a=trim21 # Pull Request ## Related issue No ## What does this PR do? This pr fix the default genetic type params of `search` and `searchGet`, it should use generic type param of `index` expected type behavior in readme ```ts client.index<T>('xxx').search(...): Promise<SearchResponse<T>> ``` current actually type, because `search` has a default generic type param `Record<string, any>`: ```ts client.index<T>('xxx').search(...): ``` is implicit ```ts client.index<T>('xxx').search<Record<string, any>>(...): ``` and it's actually ```ts client.index<T>('xxx').search(...): Promise<SearchResponse<Record<string, any>>> ``` ## PR checklist Please check if your PR fulfills the following requirements: - [X] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [X] Have you read the contributing guidelines? - [X] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: Trim21 <[email protected]>
2 parents 14051d4 + 289eb84 commit 1287b3e

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

src/indexes.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ import { HttpRequests } from './http-requests'
4444
import { Task, TaskClient } from './task'
4545
import { EnqueuedTask } from './enqueued-task'
4646

47-
class Index<T = Record<string, any>> {
47+
class Index<T extends Record<string, any> = Record<string, any>> {
4848
uid: string
4949
primaryKey: string | undefined
5050
createdAt: Date | undefined
@@ -78,11 +78,11 @@ class Index<T = Record<string, any>> {
7878
* @param {Partial<Request>} config? Additional request configuration options
7979
* @returns {Promise<SearchResponse<T>>} Promise containing the search response
8080
*/
81-
async search<T = Record<string, any>>(
81+
async search<D = T>(
8282
query?: string | null,
8383
options?: SearchParams,
8484
config?: Partial<Request>
85-
): Promise<SearchResponse<T>> {
85+
): Promise<SearchResponse<D>> {
8686
const url = `indexes/${this.uid}/search`
8787

8888
return await this.httpRequest.post(
@@ -103,11 +103,11 @@ class Index<T = Record<string, any>> {
103103
* @param {Partial<Request>} config? Additional request configuration options
104104
* @returns {Promise<SearchResponse<T>>} Promise containing the search response
105105
*/
106-
async searchGet<T = Record<string, any>>(
106+
async searchGet<D = T>(
107107
query?: string | null,
108108
options?: SearchParams,
109109
config?: Partial<Request>
110-
): Promise<SearchResponse<T>> {
110+
): Promise<SearchResponse<D>> {
111111
const url = `indexes/${this.uid}/search`
112112

113113
const parseFilter = (filter?: Filter): string | undefined => {
@@ -130,7 +130,7 @@ class Index<T = Record<string, any>> {
130130
attributesToHighlight: options?.attributesToHighlight?.join(','),
131131
}
132132

133-
return await this.httpRequest.get<SearchResponse<T>>(
133+
return await this.httpRequest.get<SearchResponse<D>>(
134134
url,
135135
removeUndefinedFromObject(getParams),
136136
config

tests/env/typescript-node/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ interface Movie {
1919
id: number
2020
title: string
2121
genre?: string
22+
comment?: string
23+
isNull?: null
24+
isTrue?: true
2225
}
2326

2427
const client = new MeiliSearch(config)

tests/typed_search.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ interface Movie {
2121
title: string
2222
comment?: string
2323
genre?: string
24+
isNull?: null
25+
isTrue?: boolean
2426
}
2527

2628
interface NestedDocument {

0 commit comments

Comments
 (0)