mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-07 00:07:07 +00:00
Improve _highlightResult on Hit
This commit is contained in:
+7
-1
@@ -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>;
|
||||
|
||||
Reference in New Issue
Block a user