From 1a0bd73002f99424f8c51a61e83087aa86c44b0a Mon Sep 17 00:00:00 2001 From: Thomas Wilkerling Date: Wed, 7 May 2025 17:15:40 +0200 Subject: [PATCH] add further generics to index.d.ts --- index.d.ts | 574 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 362 insertions(+), 212 deletions(-) diff --git a/index.d.ts b/index.d.ts index a1859b9..54ea5d1 100644 --- a/index.d.ts +++ b/index.d.ts @@ -75,7 +75,7 @@ declare module "flexsearch" { * **Document:** * * Search options: https://github.com/nextapps-de/flexsearch#search-options */ - type SearchOptions = { + type SearchOptions = { query?: string; limit?: number; offset?: number; @@ -85,36 +85,7 @@ declare module "flexsearch" { resolve?: R; }; - /** - * **Document:** - * * The document descriptor: https://github.com/nextapps-de/flexsearch#the-document-descriptor - */ - type DocumentDescriptor = { - id?: string | "id"; - field?: FieldName | FieldName[] | FieldOptions | Array>; - index?: FieldName | FieldName[] | FieldOptions | Array>; - tag?: FieldName | FieldName[] | TagOptions | Array>; - store?: FieldName | FieldName[] | StoreOptions | Array | boolean; - }; - - type WorkerURL = string; - type WorkerPath = string; - type WorkerConfigURL = string; - type WorkerConfigPath = string; type SerializedFunctionString = string; - type FieldName = D extends object - ? { - [K in keyof D]: K extends string - ? D[K] extends Array - ? `${ K }` | `${ K }[]:${ FieldName & string }` - : K | `${ K }:${ FieldName & string }` - : never - }[keyof D] - : never - /** - * The template to be applied on matches (e.g. "\$1\"), where \$1 is a placeholder for the matched partial - */ - type TemplateResultHighlighting = string; /** * **Document:** @@ -183,7 +154,10 @@ declare module "flexsearch" { * * Right-To-Left: https://github.com/nextapps-de/flexsearch/doc/encoder.md#right-to-left-support * * Language: https://github.com/nextapps-de/flexsearch/doc/encoder.md#built-in-language-packs */ - type IndexOptions = { + type IndexOptions< + S extends StorageInterface = undefined, + R extends boolean = true + > = { preset?: Preset; tokenize?: Tokenizer; cache?: boolean | number; @@ -199,9 +173,10 @@ declare module "flexsearch" { partial: string, partial_index: number, ) => number; + resolve?: R; // Persistent-specific options - db?: D; + db?: S; commit?: boolean; // Language-specific Options and Encoding @@ -210,42 +185,36 @@ declare module "flexsearch" { rtl?: boolean; }; - type WorkerIndexOptions = IndexOptions & { - config?: WorkerConfigURL | WorkerConfigPath, - export?: () => Promise; - import?: () => Promise; - // no persistent supported - db?: null; - commit?: null; - }; - /************************************/ /* Index Search */ /************************************/ type DefaultSearchResults = Id[]; type IntermediateSearchResults = Array; - type SearchResults = R extends true ? Resolver : DefaultSearchResults; + type SearchResults = R extends true ? DefaultSearchResults : Resolver; /** - * **Document:** - * * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants - * * API overview: https://github.com/nextapps-de/flexsearch#api-overview - * * Usage: https://github.com/nextapps-de/flexsearch#usage + * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants \ + * API overview: https://github.com/nextapps-de/flexsearch#api-overview \ + * Usage: https://github.com/nextapps-de/flexsearch#usage */ type IndexSearchResultsWrapper< W extends boolean = false, - D extends StorageInterface = undefined, - R extends boolean = false + S extends StorageInterface = undefined, + R extends boolean = true > = W extends false - ? D extends undefined + ? S extends undefined ? SearchResults : Promise> : Promise> - export class Index { - constructor(options?: Preset | IndexOptions); + export class Index< + W extends boolean = false, + S extends StorageInterface = undefined, + r extends boolean = true + > { + constructor(options?: Preset | IndexOptions); add(id: Id, content: string): this | Promise; @@ -258,15 +227,20 @@ declare module "flexsearch" { remove(id: Id): this | Promise; - search(query: string, limit?: Limit): IndexSearchResultsWrapper; - search(query: string, options?: SearchOptions): IndexSearchResultsWrapper; - search(query: string, limit: Limit, options: SearchOptions): IndexSearchResultsWrapper; - search(options: SearchOptions): IndexSearchResultsWrapper; + /** @deprecated Pass "limit" within options */ + search(query: string, limit?: Limit): IndexSearchResultsWrapper; + /** @deprecated Pass "limit" within options */ + search(query: string, options?: SearchOptions): IndexSearchResultsWrapper; + /** @deprecated Pass "limit" within options */ + search(query: string, limit: Limit, options: SearchOptions): IndexSearchResultsWrapper; + search(options: SearchOptions): IndexSearchResultsWrapper; + /** @deprecated Pass "limit" within options */ searchCache(query: string, limit?: Limit): W extends false ? SearchResults : Promise; - searchCache(query: string, options?: Limit | SearchOptions): IndexSearchResultsWrapper; - searchCache(query: string, limit: Limit, options: SearchOptions): IndexSearchResultsWrapper; - searchCache(options: SearchOptions): IndexSearchResultsWrapper; + searchCache(query: string, options?: SearchOptions): IndexSearchResultsWrapper; + /** @deprecated Pass "limit" within options */ + searchCache(query: string, limit: Limit, options: SearchOptions): IndexSearchResultsWrapper; + searchCache(options: SearchOptions): IndexSearchResultsWrapper; // https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids contain(id: Id): boolean | Promise; @@ -315,34 +289,52 @@ declare module "flexsearch" { callback?: AsyncCallback, ): Promise; + searchAsync( + query: string, + callback?: AsyncCallback, + ): Promise + /** @deprecated Pass "limit" within options */ searchAsync( query: string, limit?: Limit, callback?: AsyncCallback, ): Promise - searchAsync( + searchAsync( query: string, options?: SearchOptions, callback?: AsyncCallback>, ): Promise> - searchAsync( + /** @deprecated Pass "limit" within options */ + searchAsync( query: string, limit: Limit, options?: SearchOptions, callback?: AsyncCallback>, ): Promise>; - searchAsync( + searchAsync( options: SearchOptions, callback?: AsyncCallback>, ): Promise>; } - /** - * **Document:** - * * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants - * * API overview: https://github.com/nextapps-de/flexsearch#api-overview - * * Worker index: https://github.com/nextapps-de/flexsearch#worker-index - */ + /************************************/ + /* Worker Index */ + /************************************/ + + type WorkerURL = string; + type WorkerPath = string; + type WorkerConfigURL = string; + type WorkerConfigPath = string; + type WorkerType = boolean | WorkerURL | WorkerPath; + + type WorkerIndexOptions = IndexOptions & { + config?: WorkerConfigURL | WorkerConfigPath, + export?: () => Promise; + import?: () => Promise; + // no persistent supported + db?: null; + commit?: null; + }; export class Worker extends Index { constructor(options?: Preset | WorkerIndexOptions); @@ -356,81 +348,43 @@ declare module "flexsearch" { /* Document Search */ /************************************/ + /** + * The template to be applied on matches (e.g. "\$1\"), where \$1 is a placeholder for the matched partial + */ + type TemplateResultHighlighting = string; + type TagName = string; + type FieldName = D extends object + ? { + [K in keyof D]: K extends string + ? D[K] extends Array + ? `${ K }` | `${ K }[]:${ FieldName & string }` + : K | `${ K }:${ FieldName & string }` + : never + }[keyof D] + : never; + type FieldOptions = IndexOptions & { field: FieldName, filter?: (content: string) => boolean; - custom?: (content: string) => string; + custom?: (content: string) => string | boolean; config?: WorkerConfigURL | WorkerConfigPath; + db?: StorageInterface; }; type TagOptions = { field: FieldName; filter?: (content: string) => boolean; - custom?: (content: string) => string; + custom?: (content: string) => string | boolean; db?: StorageInterface; }; type StoreOptions = { field: FieldName; filter?: (content: string) => boolean; - custom?: (content: string) => string; + custom?: (content: string) => string | boolean; db?: StorageInterface; }; - /* - * **Document:** - * * Document options: https://github.com/nextapps-de/flexsearch#document-options - */ - - type WorkerType = boolean | WorkerURL | WorkerPath; - - type DocumentOptions< - D extends DocumentData = DocumentData, - W extends WorkerType = false, - B extends StorageInterface = undefined - > = IndexOptions & DocumentDescriptor & { - worker?: W; - doc?: DocumentDescriptor; - document?: DocumentDescriptor; - }; - - type DefaultDocumentSearchResults = Array<{ - field?: FieldName; - tag?: FieldName; - result: DefaultSearchResults; - }>; - - type EnrichedDocumentSearchResults = Array<{ - field?: FieldName; - tag?: FieldName; - result: Array<{ - id: Id; - doc: D | null; - highlight?: string; - }>; - }>; - - type MergedDocumentSearchResults = Array<{ - id: Id; - doc: D | null; - field?: FieldName[]; - tag?: FieldName[]; - highlight?: {[field: FieldName]: string}; - }>; - - type DocumentSearchResults< - D extends DocumentData = DocumentData, - R extends boolean = false, - E extends boolean = false, - M extends boolean = false - > = R extends true - ? Resolver - : M extends true - ? MergedDocumentSearchResults - : E extends true - ? EnrichedDocumentSearchResults - : DefaultDocumentSearchResults - /** * # Document Search Result * @@ -441,22 +395,104 @@ declare module "flexsearch" { * If neither of these conditions is met, then the returned result will be a `ISimpleDocumentSearchResult`. */ + /* + * **Document:** + * * Document options: https://github.com/nextapps-de/flexsearch#document-options + */ + + type DocumentOptions< + D extends DocumentData = DocumentData, + W extends WorkerType = false, + S extends StorageInterface = undefined + > = IndexOptions & DocumentDescriptor & { + worker?: W; + doc?: DocumentDescriptor; + document?: DocumentDescriptor; + }; + + /** + * **Document:** + * * The document descriptor: https://github.com/nextapps-de/flexsearch#the-document-descriptor + */ + type DocumentDescriptor = { + id?: string | "id"; + field?: FieldName | FieldName[] | FieldOptions | Array>; + index?: FieldName | FieldName[] | FieldOptions | Array>; + tag?: FieldName | FieldName[] | TagOptions | Array>; + store?: FieldName | FieldName[] | StoreOptions | Array | boolean; + }; + + type DefaultDocumentSearchResults = Array<{ + field?: FieldName; + tag?: FieldName; + result: DefaultSearchResults; + }>; + + type EnrichedResults = Array<{ + id: Id; + doc: D | null; + highlight?: string; + }>; + + type EnrichedDocumentSearchResults = Array<{ + field?: FieldName; + tag?: FieldName; + result: EnrichedResults; + }>; + + type MergedDocumentSearchResults = Array<{ + id: Id; + doc?: D | null; + field?: FieldName[]; + tag?: FieldName[]; + highlight?: {[field: FieldName]: string}; + }>; + + type DocumentSearchResults< + D extends DocumentData = DocumentData, + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, + E extends boolean = false, + M extends boolean = false + > = R extends true + ? P extends PluckOptions + ? E extends true + ? EnrichedResults + : DefaultSearchResults + : M extends true + ? MergedDocumentSearchResults + : E extends true + ? EnrichedDocumentSearchResults + : H extends HighlightOptions + ? EnrichedDocumentSearchResults + : DefaultDocumentSearchResults + : Resolver; + + type PluckOptions< + D extends DocumentData = DocumentData, + H extends HighlightOptions = undefined, + R extends boolean = true, + E extends boolean = false + > = FieldName | DocumentSearchOptions; + /** * **Document:** * * Document search options: https://github.com/nextapps-de/flexsearch#document-search-options */ - type DocumentSearchOptions< D extends DocumentData = DocumentData, - R extends boolean = false, + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, E extends boolean = false, - M extends boolean = false + M extends boolean = false, > = SearchOptions & { - tag?: {[field: FieldName]: string} | Array<{[field: FieldName]: string}>; - field?: Array> | DocumentSearchOptions | FieldName[] | FieldName; - index?: Array> | DocumentSearchOptions | FieldName[] | FieldName; - pluck?: FieldName | DocumentSearchOptions; - highlight?: HighlightOptions | TemplateResultHighlighting; + tag?: {[field: FieldName]: TagName} | Array<{[field: FieldName]: TagName}>; + field?: Array> | DocumentSearchOptions | FieldName[] | FieldName; + index?: Array> | DocumentSearchOptions | FieldName[] | FieldName; + pluck?: P; + highlight?: H; enrich?: E; merge?: M; }; @@ -475,79 +511,148 @@ declare module "flexsearch" { type DocumentSearchResultsWrapper< D extends DocumentData = DocumentData, W extends WorkerType = false, - B extends StorageInterface = undefined, - R extends boolean = false, + S extends StorageInterface = undefined, + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, E extends boolean = false, - M extends boolean = false, + M extends boolean = false > = W extends false - ? B extends undefined - ? DocumentSearchResults - : Promise> - : Promise> + ? S extends undefined + ? DocumentSearchResults + : Promise> + : Promise> /** - * **Document:** - * * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants - * * API overview: https://github.com/nextapps-de/flexsearch#api-overview - * * Document store: https://github.com/nextapps-de/flexsearch#document-store + * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants \ + * API overview: https://github.com/nextapps-de/flexsearch#api-overview \ + * Document store: https://github.com/nextapps-de/flexsearch#document-store */ export class Document< D extends DocumentData = DocumentData, W extends WorkerType = false, - B extends StorageInterface = undefined + S extends StorageInterface = undefined > { - constructor(options: DocumentOptions); + constructor(options: DocumentOptions); - add(id: Id, document: D): this | Promise; - add(document: D): this | Promise; + add(id: Id, document: D): W extends WorkerType + ? Promise + : this; + add(document: D): W extends WorkerType + ? Promise + : this; /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ - append(id: Id, document: D): this | Promise; + append(id: Id, document: D): W extends WorkerType + ? Promise + : this; /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ - append(document: D): this | Promise; + append(document: D): W extends WorkerType + ? Promise + : this; - update(id: Id, document: D): this | Promise; - update(document: D): this | Promise; + update(id: Id, document: D): W extends WorkerType + ? Promise + : this; + update(document: D): W extends WorkerType + ? Promise + : this; - remove(id: Id): this | Promise; - remove(document: D): this | Promise; + remove(id: Id): W extends WorkerType + ? Promise + : this; + remove(document: D): W extends WorkerType + ? Promise + : this; // https://github.com/nextapps-de/flexsearch#field-search - search(query: string, limit: Limit): DocumentSearchResultsWrapper; - search( + /** @deprecated Pass "limit" within options */ + search(query: string, limit: Limit): DocumentSearchResultsWrapper; + /** @deprecated Pass "limit" within options */ + searchCache(query: string, limit: Limit): DocumentSearchResultsWrapper; + + search< + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, + E extends boolean = false, + M extends boolean = false + >( query: string, - options?: DocumentSearchOptions, - ): DocumentSearchResultsWrapper; - search( + options?: DocumentSearchOptions, + ): DocumentSearchResultsWrapper; + searchCache< + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, + E extends boolean = false, + M extends boolean = false + >( + query: string, + options?: DocumentSearchOptions, + ): DocumentSearchResultsWrapper; + + /** @deprecated Pass "limit" within options */ + search< + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, + E extends boolean = false, + M extends boolean = false + >( query: string, limit: Limit, - options: DocumentSearchOptions, - ): DocumentSearchResultsWrapper; - search( - options: DocumentSearchOptions, - ): DocumentSearchResultsWrapper; + options: DocumentSearchOptions, + ): DocumentSearchResultsWrapper; + /** @deprecated Pass "limit" within options */ + searchCache< + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, + E extends boolean = false, + M extends boolean = false + >( + query: string, + limit: Limit, + options: DocumentSearchOptions, + ): DocumentSearchResultsWrapper; + + search< + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, + E extends boolean = false, + M extends boolean = false + >( + options: DocumentSearchOptions, + ): DocumentSearchResultsWrapper; + searchCache< + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, + E extends boolean = false, + M extends boolean = false + >( + options: DocumentSearchOptions, + ): DocumentSearchResultsWrapper; - searchCache(query: string, limit: Limit): W extends false ? DocumentSearchResults : Promise>; - searchCache( - query: string, - options?: DocumentSearchOptions, - ): DocumentSearchResultsWrapper; - searchCache( - query: string, - limit: Limit, options: DocumentSearchOptions, - ): DocumentSearchResultsWrapper; - searchCache( - options: DocumentSearchOptions, - ): DocumentSearchResultsWrapper; // https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids - contain(id: Id): boolean | Promise; + contain(id: Id): D extends StorageInterface + ? Promise + : boolean; - clear(): void | Promise; + clear(): D extends StorageInterface + ? Promise + : W extends WorkerType + ? Promise + : void; - cleanup(): void | Promise; + cleanup(): void; - get(id: Id): Promise | D | null; + get(id: Id): D extends StorageInterface + ? Promise + : D | null; set(id: Id, document: D): this; set(document: D): this; @@ -607,22 +712,56 @@ declare module "flexsearch" { callback?: AsyncCallback, ): Promise; - searchAsync(query: string, limit?: Limit): Promise> - searchAsync( + /** @deprecated Pass "limit" within options */ + searchAsync(query: string, limit?: Limit): Promise> + + searchAsync< + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, + E extends boolean = false, + M extends boolean = false + >( query: string, - options?: DocumentSearchOptions, - callback?: AsyncCallback>, - ): Promise> - searchAsync( + callback?: AsyncCallback>, + ): Promise> + + searchAsync< + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, + E extends boolean = false, + M extends boolean = false + >( + query: string, + options?: DocumentSearchOptions, + callback?: AsyncCallback>, + ): Promise> + + /** @deprecated Pass "limit" within options */ + searchAsync< + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, + E extends boolean = false, + M extends boolean = false + >( query: string, limit: Limit, - options?: DocumentSearchOptions, - callback?: AsyncCallback>, - ): Promise>; - searchAsync( - options: DocumentSearchOptions, - callback?: AsyncCallback>, - ): Promise>; + options?: DocumentSearchOptions, + callback?: AsyncCallback>, + ): Promise>; + + searchAsync< + H extends HighlightOptions = undefined, + P extends PluckOptions = undefined, + R extends boolean = true, + E extends boolean = false, + M extends boolean = false + >( + options: DocumentSearchOptions, + callback?: AsyncCallback>, + ): Promise>; } type IdType = @@ -653,28 +792,39 @@ declare module "flexsearch" { db?: any; }; - type DefaultResolve = { - query?: string; - limit?: number; - offset?: number; - enrich?: boolean; - }; - - type ResolverOptions = DefaultResolve & { - index?: Index | Document; - pluck?: FieldName; - field?: FieldName; - and?: ResolverOptions | Array; - or?: ResolverOptions | Array; - xor?: ResolverOptions | Array; - not?: ResolverOptions | Array; - boost?: number; - suggest?: boolean; - resolve?: boolean; + type DefaultResolve< + D extends DocumentData = DocumentData, + H extends HighlightOptions = undefined, + E extends boolean = false + > = { limit?: number; offset?: number; /** only usable when "resolve" was not set to false */ - enrich?: boolean; + enrich?: D extends DocumentData ? E : undefined; + /** only usable when "resolve" was not set to false */ + highlight?: D extends DocumentData ? H : undefined; + }; + + type ResolverOptions< + D extends DocumentData = DocumentData, + W extends WorkerType = false, + S extends StorageInterface = undefined, + H extends HighlightOptions = undefined, + R extends boolean = true, + E extends boolean = false + > = DefaultResolve & { + query?: string; + index?: Index | Document; + pluck?: FieldName; + field?: FieldName; + tag?: {[field: FieldName]: TagName} | Array<{[field: FieldName]: TagName}>; + and?: ResolverOptions | Array>; + or?: ResolverOptions | Array>; + xor?: ResolverOptions | Array>; + not?: ResolverOptions | Array>; + boost?: number; + suggest?: boolean; + resolve?: R; }; type HighlightBoundaryOptions = { @@ -688,7 +838,7 @@ declare module "flexsearch" { pattern?: string | boolean; }; - type HighlightOptions = { + type HighlightOptions = TemplateResultHighlighting | { template?: TemplateResultHighlighting; boundary?: HighlightBoundaryOptions | number; ellipsis?: HighlightEllipsisOptions | string | boolean;