From 30e6b2c00df6d694dab9d7f0315caf41b0eb1dbd Mon Sep 17 00:00:00 2001 From: Gordon Date: Wed, 5 Dec 2018 13:02:29 -0600 Subject: [PATCH] Improve _highlightResult on Hit --- types/react-instantsearch-core/index.d.ts | 8 +++++++- .../react-instantsearch-core-tests.tsx | 6 ++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/types/react-instantsearch-core/index.d.ts b/types/react-instantsearch-core/index.d.ts index 71305a2406..af86139fde 100644 --- a/types/react-instantsearch-core/index.d.ts +++ b/types/react-instantsearch-core/index.d.ts @@ -497,12 +497,18 @@ export interface SearchResults { */ export type Hit = TDoc & { objectID: string; + /** + * Contains the searchable attributes within the document and shows which part of the + * attribute was matched by the search terms. Note that if the index has defined + * any searchable attributes, this object will only contain those keys and others + * will not exist. + */ '_highlightResult': HighlightResult; }; export type HighlightResult = TDoc extends { [k: string]: any } ? - { [K in keyof TDoc]: HighlightResultField } : + { [K in keyof TDoc]?: HighlightResultField } : never; type HighlightResultField = diff --git a/types/react-instantsearch-core/react-instantsearch-core-tests.tsx b/types/react-instantsearch-core/react-instantsearch-core-tests.tsx index d18e5a78e6..1ced5dee69 100644 --- a/types/react-instantsearch-core/react-instantsearch-core-tests.tsx +++ b/types/react-instantsearch-core/react-instantsearch-core-tests.tsx @@ -102,9 +102,7 @@ import {

{additionalProp}

{searchResults.hits.map((h) => { // $ExpectType string - const compound = h._highlightResult.field3.compound.value; - // $ExpectType never - const field2 = h._highlightResult.field2; + const compound = h._highlightResult.field3!.compound!.value; return {compound}; })} ; @@ -121,7 +119,7 @@ import {

{additionalProp}

{searchResults.hits.map((h) => { // $ExpectType string[] - const words = h._highlightResult.field3.compound.matchedWords; + const words = h._highlightResult.field3!.compound!.matchedWords; return {h.field2}: {words.join(',')}; })} ;