Skip to content

Commit 5dcc2e1

Browse files
committed
Improve generics
Signed-off-by: guido <[email protected]>
1 parent 6b20073 commit 5dcc2e1

File tree

3 files changed

+21
-16
lines changed

3 files changed

+21
-16
lines changed

@types/index.d.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,26 @@
1-
import type { OrderedSet } from 'immutable';
1+
import type { OrderedSet, Set } from 'immutable';
22
import React from 'react';
33

44
import { BaseEntityRecords, Context } from '@wordpress/core-data';
55

66
export default EntitiesSearch;
77

8+
// TODO Try to convert it to a module.
89
declare namespace EntitiesSearch {
910
type Post<C extends Context = 'view'> = BaseEntityRecords.Post<C>;
1011
type PostType<C extends Context = 'view'> = BaseEntityRecords.Type<C>;
1112
type Taxonomy<C extends Context = 'view'> = BaseEntityRecords.Taxonomy<C>;
1213

14+
interface QueryArguments<V>
15+
extends Partial<
16+
Readonly<{
17+
exclude: Set<V>;
18+
include: Set<V>;
19+
fields: EntitiesSearch.SearchQueryFields;
20+
[p: string]: unknown;
21+
}>
22+
> {}
23+
1324
interface SearchEntityFields
1425
extends Readonly<{
1526
id: number;
@@ -109,11 +120,13 @@ declare namespace EntitiesSearch {
109120
entities: Omit<BaseControl<E>, 'options'>;
110121
searchEntities: (
111122
phrase: string,
112-
kind: BaseControl<K>['value'],
113-
queryArguments?: Record<string, unknown>
123+
kind: CompositeEntitiesKinds<E, K>['kind']['value'],
124+
queryArguments?: QueryArguments<E>
114125
) => Promise<BaseControl<E>['options']>;
115126
children(
116-
entities: BaseControl<E>,
127+
entities: CompositeEntitiesKinds<E, K>['entities'] & {
128+
options: BaseControl<E>['options'];
129+
},
117130
kind: CompositeEntitiesKinds<E, K>['kind'],
118131
search: (
119132
phrase: Parameters<SearchControl<E, K>['search']>[0]

sources/client/src/api/search-entities.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import EntitiesSearch from '@types';
2-
import { OrderedSet, Set } from 'immutable';
2+
import { OrderedSet } from 'immutable';
33

44
import { fetch } from './fetch';
55

6-
// TODO Parametrize the fields.
7-
// TODO Write the underline search function.
8-
// TODO Make the `queryArguments` optional.
96
export async function searchEntities<E>(
107
type: string,
118
subtype: OrderedSet<string>,
129
phrase: string,
13-
queryArguments: {
14-
exclude: Set<string>;
15-
include: Set<string>;
16-
fields?: EntitiesSearch.SearchQueryFields;
17-
[p: string]: unknown;
18-
}
10+
queryArguments: EntitiesSearch.QueryArguments<string>
1911
): Promise<OrderedSet<E>> {
2012
const {
2113
exclude,

sources/client/src/components/composite-entities-by-kind.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { uniqueControlOptions } from '../utils/unique-control-options';
88

99
type SearchPhrase = Parameters<EntitiesSearch.SearchControl['onChange']>[0];
1010
type Entities<V> = EntitiesSearch.BaseControl<V>['value'];
11-
type Kind<V> = EntitiesSearch.BaseControl<V>['value'];
11+
type Kind<V> = EntitiesSearch.BaseControl<V>['value'] | V;
1212
type SearchEntities<P, T> = EntitiesSearch.CompositeEntitiesKinds<
1313
P,
1414
T
@@ -118,7 +118,7 @@ export function CompositeEntitiesByKind<E, K>(
118118
});
119119
};
120120

121-
const onChangeKind = (kind: Kind<K> | K) => {
121+
const onChangeKind = (kind: Kind<K>) => {
122122
const _kind =
123123
kind instanceof OrderedSet ? kind : OrderedSet<K>([kind as K]);
124124

0 commit comments

Comments
 (0)