From 3d3c7b298b74effe9bb722a04fbb47dc39a4bd95 Mon Sep 17 00:00:00 2001 From: Aymeric Giraudet Date: Mon, 8 Aug 2022 16:28:07 +0200 Subject: [PATCH] feat(geo-search): make `GeoHit` type generic (#5083) --- src/connectors/geo-search/connectGeoSearch.ts | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/connectors/geo-search/connectGeoSearch.ts b/src/connectors/geo-search/connectGeoSearch.ts index d19e187143..8194033199 100644 --- a/src/connectors/geo-search/connectGeoSearch.ts +++ b/src/connectors/geo-search/connectGeoSearch.ts @@ -12,6 +12,7 @@ import { noop, } from '../../lib/utils'; import type { + BaseHit, Connector, GeoLoc, Hit, @@ -39,7 +40,8 @@ function setBoundingBoxAsString(state: SearchParameters, value: string) { ); } -export type GeoHit = Hit & Required>; +export type GeoHit> = Hit & + Required>; type Bounds = { /** @@ -52,7 +54,7 @@ type Bounds = { southWest: GeoLoc; }; -export type GeoSearchRenderState = { +export type GeoSearchRenderState> = { /** * Reset the current bounding box refinement. */ @@ -76,7 +78,7 @@ export type GeoSearchRenderState = { /** * The matched hits from Algolia API. */ - items: GeoHit[]; + items: Array>; /** * The current position of the search. */ @@ -101,7 +103,9 @@ export type GeoSearchRenderState = { toggleRefineOnMapMove(): void; }; -export type GeoSearchConnectorParams = { +export type GeoSearchConnectorParams< + THit extends BaseHit = Record +> = { /** * If true, refine will be triggered as you move the map. * @default true @@ -111,18 +115,20 @@ export type GeoSearchConnectorParams = { * Function to transform the items passed to the templates. * @default items => items */ - transformItems?: TransformItems; + transformItems?: TransformItems>; }; const $$type = 'ais.geoSearch'; -export type GeoSearchWidgetDescription = { +export type GeoSearchWidgetDescription< + THit extends BaseHit = Record +> = { $$type: 'ais.geoSearch'; - renderState: GeoSearchRenderState; + renderState: GeoSearchRenderState; indexRenderState: { geoSearch: WidgetRenderState< - GeoSearchRenderState, - GeoSearchConnectorParams + GeoSearchRenderState, + GeoSearchConnectorParams >; }; indexUiState: { @@ -139,10 +145,8 @@ export type GeoSearchWidgetDescription = { }; }; -export type GeoSearchConnector = Connector< - GeoSearchWidgetDescription, - GeoSearchConnectorParams ->; +export type GeoSearchConnector> = + Connector, GeoSearchConnectorParams>; /** * The **GeoSearch** connector provides the logic to build a widget that will display the results on a map. It also provides a way to search for results based on their position. The connector provides functions to manage the search experience (search on map interaction or control the interaction for example).