Skip to content

HighlightResult/SnippetResult do not support Array key types #1311

@daniel-shuy

Description

@daniel-shuy

HighlightResult/SnippetResult are defined as:

export type HighlightResult<THit> = THit extends string | number
  ? HighlightMatch
  : {
      [KAttribute in keyof THit]?: HighlightResult<THit[KAttribute]>;
    };

export type SnippetResult<THit> = THit extends string | number
  ? SnippetMatch
  : {
      [KAttribute in keyof THit]: SnippetResult<THit[KAttribute]>;
    };

which resolves to HighlightMatch/SnippetMatch if THit is a string | number, else recurses.

This unfortunately means that array properties/attributes of THit will not resolve to HighlightMatch/SnippetMatch.

See related issue meilisearch/meilisearch-js-plugins#552 (comment)

Since the recursion is meant to support nested Objects, maybe the type definitions can be changed to:

export declare type HighlightResult<THit> = THit extends object
    ? THit extends Array<any>
        ? HighlightMatch
        : {
            [KAttribute in keyof THit]: HighlightResult<THit[KAttribute]>;
        }
    : HighlightMatch;

export declare type SnippetResult<THit> = THit extends object ?
    ? THit extends Array<any>
        ? SnippetMatch
        : {
            [KAttribute in keyof THit]: SnippetResult<THit[KAttribute]>;
        }
    : SnippetMatch;

which would support all primitives and arrays.

I wish TypeScript had an easier way to define (object | !Array). Sadly Exclude<object, Array<any>> doesn't work with object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions