Skip to content

Commit b130b8e

Browse files
authored
chore(ts): add MaybePromise wrapper to make APIs shorter to read (#333)
* chore(ts): add MaybePromise wrapper to make APIs shorter to read * Update packages/autocomplete-js/src/types/index.ts
1 parent 001243f commit b130b8e

File tree

3 files changed

+6
-7
lines changed

3 files changed

+6
-7
lines changed

packages/autocomplete-core/src/types/api.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AutocompleteAccessibilityGetters } from './getters';
22
import { AutocompleteSetters } from './setters';
33
import { AutocompleteState } from './state';
4+
import { MaybePromise } from './wrappers';
45

56
export interface AutocompleteApi<
67
TItem,
@@ -92,7 +93,7 @@ export interface AutocompleteSource<TItem> {
9293
/**
9394
* Function called when the input changes. You can use this function to filter/search the items based on the query.
9495
*/
95-
getSuggestions(params: GetSourcesParams<TItem>): TItem[] | Promise<TItem[]>;
96+
getSuggestions(params: GetSourcesParams<TItem>): MaybePromise<TItem[]>;
9697
/**
9798
* Function called when an item is selected.
9899
*/
@@ -163,9 +164,7 @@ export type AutocompletePlugin<TItem, TData> = {
163164
*/
164165
getSources?(
165166
params: GetSourcesParams<TItem>
166-
):
167-
| Array<AutocompleteSource<TItem>>
168-
| Promise<Array<AutocompleteSource<TItem>>>;
167+
): MaybePromise<Array<AutocompleteSource<TItem>>>;
169168
/**
170169
* The function called when the autocomplete form is submitted.
171170
*/
@@ -244,9 +243,7 @@ export interface AutocompleteOptions<TItem> {
244243
*/
245244
getSources(
246245
params: GetSourcesParams<TItem>
247-
):
248-
| Array<AutocompleteSource<TItem>>
249-
| Promise<Array<AutocompleteSource<TItem>>>;
246+
): MaybePromise<Array<AutocompleteSource<TItem>>>;
250247
/**
251248
* The environment from where your JavaScript is running.
252249
* Useful if you're using autocomplete in a different context than
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type MaybePromise<TResolution> = Promise<TResolution> | TResolution;

packages/autocomplete-js/src/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export type InternalAutocompleteSource<TItem> = InternalAutocompleteCoreSource<
5252
type GetSources<TItem> = (
5353
params: GetSourcesParams<TItem>
5454
) =>
55+
// TODO: reuse MaybePromise from autocomplete-core when we find a way to share the type
5556
| Array<AutocompleteCoreSource<TItem>>
5657
| Promise<Array<AutocompleteCoreSource<TItem>>>;
5758

0 commit comments

Comments
 (0)