Skip to content

Commit 2ee049a

Browse files
🤖 Merge PR DefinitelyTyped#72579 [itemsjs] Rename generics and fix extends by @MysteryBlokHed
1 parent 6fc1d01 commit 2ee049a

File tree

2 files changed

+52
-47
lines changed

2 files changed

+52
-47
lines changed

‎types/itemsjs/index.d.ts‎

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ declare namespace itemsjs {
99

1010
type Buckets<K> = Array<Bucket<K>>;
1111

12-
interface SearchAggregation<I extends {}, A extends keyof I & string> {
13-
name: A;
12+
interface SearchAggregation<Items extends Record<string, any>, Aggregations extends keyof Items & string> {
13+
name: Aggregations;
1414
title: string;
1515
position: number;
16-
buckets: Buckets<I[A]>;
16+
buckets: Buckets<Items[Aggregations]>;
1717
}
1818

1919
interface Pagination {
@@ -23,21 +23,21 @@ declare namespace itemsjs {
2323
}
2424

2525
interface SearchOptions<
26-
I extends {},
27-
S extends string,
28-
A extends keyof I & string,
29-
IdField extends keyof I & string,
26+
Items extends Record<string, any>,
27+
Sortings extends string,
28+
Aggregations extends keyof Items & string,
29+
IdField extends keyof Items & string,
3030
> {
3131
query?: string | undefined;
3232
/** @default 1 */
3333
page?: number | undefined;
3434
/** @default 12 */
3535
per_page?: number | undefined;
3636
/** The name of a sort defined in the configuration's sortings, or a new custom one */
37-
sort?: S | Sorting<I> | undefined;
38-
filters?: Partial<Record<A, string[]>> | undefined;
37+
sort?: Sortings | Sorting<Items> | undefined;
38+
filters?: Partial<Record<Aggregations, string[]>> | undefined;
3939
/** A custom function to filter values */
40-
filter?: ((item: I) => boolean) | undefined;
40+
filter?: ((item: Items) => boolean) | undefined;
4141
/** @default false */
4242
isExactSearch?: boolean | undefined;
4343
/** @default false */
@@ -46,11 +46,11 @@ declare namespace itemsjs {
4646
is_all_filtered_items?: boolean | undefined;
4747

4848
/** Restricts results to items whose values match the ID field (`id` by default or as defined in `custom_id_field`). */
49-
ids?: I[IdField][];
49+
ids?: Items[IdField][];
5050
}
5151

52-
interface AggregationOptions<A extends string> {
53-
name: A;
52+
interface AggregationOptions<Aggregations extends string> {
53+
name: Aggregations;
5454
/** @default 1 */
5555
page?: number | undefined;
5656
/** @default 10 */
@@ -59,8 +59,8 @@ declare namespace itemsjs {
5959
conjunction?: boolean | undefined;
6060
}
6161

62-
interface SimilarOptions<I extends {}> {
63-
field: keyof I & string;
62+
interface SimilarOptions<Items extends Record<string, any>> {
63+
field: keyof Items & string;
6464
/** @default 0 */
6565
minimum?: number | undefined;
6666
/** @default 1 */
@@ -70,17 +70,17 @@ declare namespace itemsjs {
7070
}
7171

7272
interface ItemsJs<
73-
I extends {},
74-
S extends string,
75-
A extends keyof I & string,
76-
IdField extends keyof I & string,
73+
Items extends Record<string, any>,
74+
Sortings extends string,
75+
Aggregations extends keyof Items & string,
76+
IdField extends keyof Items & string,
7777
> {
7878
/** Search items */
79-
search(options?: SearchOptions<I, S, A, IdField>): {
79+
search(options?: SearchOptions<Items, Sortings, Aggregations, IdField>): {
8080
data: {
81-
items: I[];
82-
allFilteredItems: I[] | null;
83-
aggregations: Record<A, SearchAggregation<I, A>>;
81+
items: Items[];
82+
allFilteredItems: Items[] | null;
83+
aggregations: Record<Aggregations, SearchAggregation<Items, Aggregations>>;
8484
};
8585
pagination: Pagination;
8686
timings: {
@@ -92,8 +92,8 @@ declare namespace itemsjs {
9292
};
9393

9494
/** Get data for aggregation */
95-
aggregation(options: AggregationOptions<A>): {
96-
data: { buckets: Buckets<I[A]> };
95+
aggregation(options: AggregationOptions<Aggregations>): {
96+
data: { buckets: Buckets<Items[Aggregations]> };
9797
pagination: Pagination;
9898
};
9999

@@ -102,21 +102,21 @@ declare namespace itemsjs {
102102
* Uses the `id` key or the one set via `custom_id_field` to check for uniqueness..
103103
*/
104104
similar(
105-
id: I[IdField],
106-
options: SimilarOptions<I>,
105+
id: Items[IdField],
106+
options: SimilarOptions<Items>,
107107
): {
108-
data: { items: Array<I & { _id: number; intersection_length: number }> };
108+
data: { items: Array<Items & { _id: number; intersection_length: number }> };
109109
pagination: Pagination;
110110
};
111111

112112
/** Reindex with an entirely new list of items */
113-
reindex(data: I[]): void;
113+
reindex(data: Items[]): void;
114114
}
115115

116116
type Order = "asc" | "desc";
117117

118-
interface Sorting<I extends {}> {
119-
field: keyof I | Array<keyof I>;
118+
interface Sorting<Items extends Record<string, any>> {
119+
field: keyof Items | Array<keyof Items>;
120120
order?: Order | Order[] | undefined;
121121
}
122122

@@ -139,15 +139,15 @@ declare namespace itemsjs {
139139

140140
/** Configuration for itemsjs */
141141
interface Configuration<
142-
I extends {},
143-
S extends string,
144-
A extends keyof I & string,
142+
Items extends Record<string, any>,
143+
Sortings extends string,
144+
Aggregations extends keyof Items & string,
145145
IdField extends string = "id",
146146
> {
147-
sortings?: Record<S, Sorting<I>> | undefined;
148-
aggregations?: Record<A, Aggregation> | undefined;
147+
sortings?: Record<Sortings, Sorting<Items>> | undefined;
148+
aggregations?: Record<Aggregations, Aggregation> | undefined;
149149
/** @default [] */
150-
searchableFields?: Array<keyof I> | undefined;
150+
searchableFields?: Array<keyof Items> | undefined;
151151
/** @default true */
152152
native_search_enabled?: boolean | undefined;
153153
/** @default 'id' — defines which field represents the unique ID */
@@ -159,19 +159,19 @@ declare namespace itemsjs {
159159
* Main itemsjs function
160160
* @param items The items to index
161161
* @param configuration Configuration options including sortings, aggregations, and optionally a custom ID field.
162-
* @template I The type of items being indexed
163-
* @template S The keys of sortings defined in the configuration.
164-
* @template A The keys of aggregations defined in the configuration.
162+
* @template Items The type of items being indexed
163+
* @template Sortings The keys of sortings defined in the configuration.
164+
* @template Aggregations The keys of aggregations defined in the configuration.
165165
* @template IdField The field used as the unique identifier for items (defaults to "id").
166166
*/
167167
declare function itemsjs<
168-
I extends Record<string, any> = Record<string, any>,
169-
S extends string = string,
170-
A extends keyof I & string = keyof I & string,
171-
IdField extends keyof I & string = "id",
168+
Items extends Record<string, any> = Record<string, unknown>,
169+
Sortings extends string = string,
170+
Aggregations extends keyof Items & string = keyof Items & string,
171+
IdField extends keyof Items & string = "id",
172172
>(
173-
items: I[],
174-
configuration?: itemsjs.Configuration<I, S, A, IdField>,
175-
): itemsjs.ItemsJs<I, S, A, IdField>;
173+
items: Items[],
174+
configuration?: itemsjs.Configuration<Items, Sortings, Aggregations, IdField>,
175+
): itemsjs.ItemsJs<Items, Sortings, Aggregations, IdField>;
176176

177177
export = itemsjs;

‎types/itemsjs/itemsjs-tests.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,8 @@ itemsCustomId.search({
133133
itemsCustomId.search({
134134
ids: ["a"],
135135
});
136+
137+
// @ts-expect-error Can only pass objects
138+
itemsjs("foo");
139+
// @ts-expect-error Can only pass objects
140+
itemsjs(123);

0 commit comments

Comments
 (0)