Improve _highlightResult on Hit

This commit is contained in:
Gordon
2018-12-05 13:09:11 -06:00
parent 56218fdbce
commit 30e6b2c00d
2 changed files with 9 additions and 5 deletions
+7 -1
View File
@@ -497,12 +497,18 @@ export interface SearchResults<TDoc = BasicDoc> {
*/
export type Hit<TDoc = BasicDoc> = 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<TDoc>;
};
export type HighlightResult<TDoc> =
TDoc extends { [k: string]: any } ?
{ [K in keyof TDoc]: HighlightResultField<TDoc[K]> } :
{ [K in keyof TDoc]?: HighlightResultField<TDoc[K]> } :
never;
type HighlightResultField<TField> =
@@ -102,9 +102,7 @@ import {
<h1>{additionalProp}</h1>
{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 <span>{compound}</span>;
})}
</div>;
@@ -121,7 +119,7 @@ import {
<h1>{additionalProp}</h1>
{searchResults.hits.map((h) => {
// $ExpectType string[]
const words = h._highlightResult.field3.compound.matchedWords;
const words = h._highlightResult.field3!.compound!.matchedWords;
return <span>{h.field2}: {words.join(',')}</span>;
})}
</div>;