diff --git a/index.d.ts b/index.d.ts index ba4af3a..40b341d 100644 --- a/index.d.ts +++ b/index.d.ts @@ -198,7 +198,7 @@ declare module "flexsearch" { D extends DocumentData = undefined > = R extends true ? DefaultSearchResults - : Resolver; + : Resolver /** * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants \ @@ -209,12 +209,13 @@ declare module "flexsearch" { export type IndexSearchResultsWrapper< W extends WorkerType = false, S extends StorageInterface | boolean = false, - R extends boolean = true + R extends boolean = true, + D extends DocumentData = undefined > = W extends false ? S extends false - ? SearchResults - : Promise> - : Promise> + ? SearchResults + : Promise> + : Promise> export class Index< W extends WorkerType = false, @@ -364,8 +365,8 @@ declare module "flexsearch" { type WorkerConfigPath = string; type WorkerType = boolean | WorkerURL | WorkerPath; - export type WorkerIndexOptions = IndexOptions & { - config?: WorkerConfigURL | WorkerConfigPath, + export type WorkerIndexOptions = IndexOptions & IndexWorkerConfig & { + //config?: WorkerConfigURL | WorkerConfigPath, export?: () => Promise; import?: () => Promise; // no persistent supported @@ -373,6 +374,10 @@ declare module "flexsearch" { commit?: null; }; + export interface IndexWorkerConfig { + config?: WorkerConfigURL | WorkerConfigPath; + } + export class Worker extends Index { constructor(options?: Preset | WorkerIndexOptions); @@ -402,33 +407,31 @@ declare module "flexsearch" { }[keyof D] : never; - export type DefaultFieldOptions< - D = DocumentData, - > = IndexOptions & { - field: FieldName; - filter?: (doc: D) => boolean; - db?: StorageInterface; - }; + export type DefaultFieldOptions = + IndexOptions & { + field: FieldName; + filter?: (doc: D) => boolean; + db?: StorageInterface; + }; - export type DefaultCustomFieldOptions< - D = DocumentData, - > = IndexOptions & { - custom: CustomFN; - field: FieldName; - filter?: (doc: D) => boolean; - db?: StorageInterface; - }; + export type DefaultCustomFieldOptions = + IndexOptions & { + custom: CustomFN; + field: FieldName; + filter?: (doc: D) => boolean; + db?: StorageInterface; + }; - export type TagOptions = DefaultFieldOptions | DefaultCustomFieldOptions; + export type TagOptions = + | DefaultFieldOptions + | DefaultCustomFieldOptions; - export type StoreOptions = DefaultFieldOptions | DefaultCustomFieldOptions; - - export interface IndexWorkerConfig { - config?: WorkerConfigURL | WorkerConfigPath; - } + export type StoreOptions = + | DefaultFieldOptions + | DefaultCustomFieldOptions; export type FieldOptions = - (DefaultFieldOptions & IndexWorkerConfig) + | (DefaultFieldOptions & IndexWorkerConfig) | (DefaultCustomFieldOptions & IndexWorkerConfig) /** @@ -992,16 +995,16 @@ declare module "flexsearch" { constructor(options?: ResolverOptions | IntermediateSearchResults); and(...args: ResolverOptions[]): - SearchResults; + IndexSearchResultsWrapper; or(...args: ResolverOptions[]): - SearchResults; + IndexSearchResultsWrapper; xor(...args: ResolverOptions[]): - SearchResults; + IndexSearchResultsWrapper; not(...args: ResolverOptions[]): - SearchResults; + IndexSearchResultsWrapper; limit(limit: number): Resolver; @@ -1009,7 +1012,7 @@ declare module "flexsearch" { boost(boost: number): Resolver; - resolve(options?: DefaultResolve): SearchResults; + resolve(options?: DefaultResolve): IndexSearchResultsWrapper; } export class StorageInterface { diff --git a/test/types.ts b/test/types.ts index 347811d..e1ce506 100644 --- a/test/types.ts +++ b/test/types.ts @@ -3,7 +3,7 @@ import { Index, Worker, Resolver, - IndexedDB + IndexedDB, EnrichedResults } from "flexsearch"; import { DefaultDocumentSearchResults, @@ -44,9 +44,14 @@ async function test_index() { const idx19: Promise = index2.searchCacheAsync({}); const res1: Resolver = new Resolver({ index }); - const res2: Resolver = res1.and({ index }).limit(100); + const res2: Resolver = res1.and({}, { index }).limit(100); const res3: DefaultSearchResults = res2.resolve(); + const res4: Resolver = new Resolver({ index: index3 }); + const res5: Promise> = res4.and(); + const res6: Promise> = res4.and({ index: index3 }); + const res7: Promise = (await res6).limit(100).resolve(); + // @ts-expect-error const idx_err1 = index.search({ highlight: true }); // @ts-expect-error @@ -78,6 +83,8 @@ async function test_index() { // @ts-expect-error const idx_err12: Promise = index3.searchAsync({ resolve: false, cache: true }); + // @ts-expect-deprecation + const idx_err13: DefaultSearchResults = index.search("query", 100); } async function test_document() { @@ -168,12 +175,22 @@ async function test_document() { const doc10: Promise = document.searchAsync({ cache: true }); const doc11: Promise = document.searchCacheAsync({}); const doc13: DefaultDocumentSearchResults = document.search({ resolve: true }); + const doc14: Resolver = document.search({ resolve: false }); const doc15: DefaultSearchResults = doc14.resolve({}); const doc16: DefaultSearchResults = doc14.and({ resolve: true }); + const doc17: EnrichedResults = doc14.resolve({ enrich: true }); + const doc18: EnrichedResults = doc14.and({ resolve: true, enrich: true }); + const doc19: Resolver = doc14.and({ index: document, field: "title" }); + const doc20: Resolver = doc19.or({ index: document2, field: "meta:title" }); + const doc21: DefaultDocumentSearchResults = doc20.resolve(); + const doc22: EnrichedResults = doc20.resolve({ enrich: true }); + const doc23: EnrichedResults = doc20.resolve({ highlight: true }); - const res1: Resolver = doc14.and({ index: document, field: "meta:title" }); - const res2: DefaultDocumentSearchResults = doc14.resolve(); + const doc24: Resolver = new Resolver({ index: document }); + const doc25: EnrichedResults = doc24.and({}, { index: document2, resolve: true, enrich: true }); + const doc26: EnrichedResults = doc24.and({}, { index: document2 }).resolve({ enrich: true }); + const doc27: EnrichedResults = doc24.and({}, { index: document2 }).resolve({ highlight: true }); // @ts-expect-error let tmp1: DocumentData = doc1[0].result[0].doc;