algolia / api-clients-automation

🤖 Monorepo of the Algolia API specs and their auto-generated clients and documentation
https://api-clients-automation.netlify.app/
MIT License
36 stars 14 forks source link

javascript: certain types aren't generic #853

Open Haroenv opened 2 years ago

Haroenv commented 2 years ago

Description

SnippetResults and HighlightResult should also be recursive as in v4

declare type SnippetMatch = {
    readonly value: string;
    readonly matchLevel: 'none' | 'partial' | 'full';
};

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

declare type HighlightMatch = {
    readonly value: string;
    readonly matchLevel: 'none' | 'partial' | 'full';
    readonly matchedWords: readonly string[];
    readonly fullyHighlighted?: boolean;
};

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

export declare type Hit<THit> = THit & {
    readonly objectID: string;
    readonly _highlightResult?: HighlightResult<THit>;
    readonly _snippetResult?: SnippetResult<THit>;
    readonly _rankingInfo?: RankingInfo;
    readonly _distinctSeqID?: number;
};
shortcuts commented 2 years ago

Thanks! This is being worked on for Java and JavaScript here: https://github.com/algolia/api-clients-automation/pull/829

millotp commented 2 years ago

Should the hit be overridden by the generic ? like hits: T[] and it's up to the user to extend its type with Hit ?

shortcuts commented 2 years ago

Should the hit be overridden by the generic ?

It should be type Hits<T> = Array<Hit<T>> and type Hit<T = Record<string, any>> = T & { ...baseProps }

millotp commented 2 years ago

Okay so the generic logic will be exclusively for hits, it would be hard to make it work with other generic type

Haroenv commented 2 years ago

Is there a plan for Highlight and Snippet to be correct as well?

shortcuts commented 2 years ago

Not for now as we have other priorities, but if you have an implementation in mind feel free to contribute!

Haroenv commented 3 months ago

customer found this too: https://github.com/algolia/algoliasearch-client-javascript/issues/1534