diff --git a/index.d.ts b/index.d.ts index ab4f5cf..8addafe 100644 --- a/index.d.ts +++ b/index.d.ts @@ -177,7 +177,7 @@ 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 = { preset?: Preset; tokenize?: Tokenizer; cache?: boolean | number; @@ -195,7 +195,7 @@ declare module "flexsearch" { ) => number; // Persistent-specific options - db?: StorageInterface; + db?: D; commit?: boolean; // Language-specific Options and Encoding @@ -228,8 +228,11 @@ declare module "flexsearch" { * * Usage: https://github.com/nextapps-de/flexsearch#usage */ - export class Index { - constructor(options?: Preset | IndexOptions); + type IndexSearchResultsWrapper = + W extends false ? D extends undefined ? SearchResults : Promise> : Promise> + + export class Index { + constructor(options?: Preset | IndexOptions); add(id: Id, content: string): this | Promise; @@ -242,15 +245,15 @@ declare module "flexsearch" { remove(id: Id): this | Promise; - search(query: string, limit?: Limit): SearchResults | Promise; - search(query: string, options?: SearchOptions): SearchResults | Promise>; - search(query: string, limit: Limit, options: SearchOptions): SearchResults | Promise>; - search(options: SearchOptions): SearchResults | 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; - searchCache(query: string, limit?: Limit): SearchResults | Promise; - searchCache(query: string, options?: Limit | SearchOptions): SearchResults | Promise>; - searchCache(query: string, limit: Limit, options: SearchOptions): SearchResults | Promise>; - searchCache(options: SearchOptions): SearchResults | Promise>; + 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; // https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids contain(id: Id): boolean | Promise; @@ -328,7 +331,7 @@ declare module "flexsearch" { * * Worker index: https://github.com/nextapps-de/flexsearch#worker-index */ - export class Worker extends Index { + export class Worker extends Index { constructor(options?: Preset | WorkerIndexOptions); export(): Promise; @@ -366,8 +369,12 @@ declare module "flexsearch" { * * Document options: https://github.com/nextapps-de/flexsearch#document-options */ - type DocumentOptions = IndexOptions & { - worker?: boolean | WorkerURL | WorkerPath; + type WorkerType = boolean | WorkerURL | WorkerPath + + type DocumentOptions = + IndexOptions + & { + worker?: W; doc?: DocumentDescriptor; document?: DocumentDescriptor; }; @@ -442,14 +449,27 @@ declare module "flexsearch" { [key: string]: DocumentValue | DocumentValue[]; }; + type DocumentSearchResultsWrapper< + D extends DocumentData = DocumentData, + W extends WorkerType = false, + B extends StorageInterface = undefined, + R extends boolean = false, + E extends boolean = false, + M extends boolean = false, + > = W extends false + ? B 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 */ - export class Document { - constructor(options: DocumentOptions); + export class Document { + constructor(options: DocumentOptions); add(id: Id, document: D): this | Promise; add(document: D): this | Promise; @@ -466,32 +486,32 @@ declare module "flexsearch" { remove(document: D): this | Promise; // https://github.com/nextapps-de/flexsearch#field-search - search(query: string, limit: Limit): DocumentSearchResults | Promise>; + search(query: string, limit: Limit): DocumentSearchResultsWrapper; search( query: string, options?: DocumentSearchOptions, - ): DocumentSearchResults | Promise>; + ): DocumentSearchResultsWrapper; search( query: string, limit: Limit, options: DocumentSearchOptions, - ): DocumentSearchResults | Promise>; + ): DocumentSearchResultsWrapper; search( options: DocumentSearchOptions, - ): DocumentSearchResults | Promise>; + ): DocumentSearchResultsWrapper; - searchCache(query: string, limit: Limit): DocumentSearchResults | Promise>; + searchCache(query: string, limit: Limit): W extends false ? DocumentSearchResults : Promise>; searchCache( query: string, options?: DocumentSearchOptions, - ): DocumentSearchResults | Promise>; + ): DocumentSearchResultsWrapper; searchCache( query: string, limit: Limit, options: DocumentSearchOptions, - ): DocumentSearchResults | Promise>; + ): DocumentSearchResultsWrapper; searchCache( options: DocumentSearchOptions, - ): DocumentSearchResults | Promise>; + ): DocumentSearchResultsWrapper; // https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids contain(id: Id): boolean | Promise; @@ -689,7 +709,7 @@ declare module "flexsearch" { resolve(options?: DefaultResolve): SearchResults; } - class StorageInterface { + export class StorageInterface { db: any; constructor(name: string, config: PersistentOptions); diff --git a/test/types.ts b/test/types.ts index d590462..3d93bb5 100644 --- a/test/types.ts +++ b/test/types.ts @@ -1,20 +1,26 @@ -import { DefaultDocumentSearchResults, Document, Resolver } from "flexsearch"; +import { + DefaultDocumentSearchResults, + Document, + Index, + Worker, + Resolver, + StorageInterface, +} from "flexsearch"; import "../index"; -const document = new Document<{ - title: string - description: string - tags: { - name: string - id: number - }[] -}>({ - document: { - index: ["tags"], - }, -}); - async function test() { + const document = new Document<{ + title: string + description: string + tags: { + name: string + id: number + }[] + }>({ + document: { + index: [ "tags" ], + }, + }); // The correct type const doc1 = await document.searchAsync({}); const doc2: Resolver = await document.searchAsync({ @@ -35,6 +41,30 @@ async function test() { const docw2: DefaultDocumentSearchResults = await document.searchAsync({ enrich: true, }); - // ... + // Promise? + const documentNoWorker = new Document({}); + const doc5 = documentNoWorker.search({}); // No Promise + + const documentWorker = new Document({ + worker: true, + }); + const doc6 = await documentWorker.search({}) // Promise + + const documentWorker2 = new Document({ + worker: '...', + }); + const doc7 = await documentWorker2.search({}) // Promise + + const index = new Index({}) + const idx = index.search({}) // No Promise + + const worker = new Worker() + const wkr = await worker.search({}) // Promise + + const documentDb = new Document({ + db: {} as unknown as StorageInterface + }) + + const doc8 = documentDb.search({}) // Promise }