From 650cf81d9ee2e68ae6b6c3d70e646697a5f156bd Mon Sep 17 00:00:00 2001 From: Thomas Wilkerling Date: Wed, 7 May 2025 18:10:10 +0200 Subject: [PATCH] add exports to index.d.ts --- index.d.ts | 79 ++++++++++++++++++++++++++------------------------- test/types.ts | 61 ++++++++++++++++++++++++++++++--------- 2 files changed, 88 insertions(+), 52 deletions(-) diff --git a/index.d.ts b/index.d.ts index 54ea5d1..ef0994e 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 = { + export type SearchOptions = { query?: string; limit?: number; offset?: number; @@ -93,8 +93,7 @@ declare module "flexsearch" { * * Language: https://github.com/nextapps-de/flexsearch#languages */ - global { - type EncoderOptions = { + export type EncoderOptions = { rtl?: boolean; dedupe?: boolean; include?: EncoderSplitOptions; @@ -112,9 +111,9 @@ declare module "flexsearch" { minlength?: number; maxlength?: number; cache?: boolean | number; - }} + } - type EncoderSplitOptions = { + export type EncoderSplitOptions = { letter?: boolean; number?: boolean; symbol?: boolean; @@ -154,7 +153,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< + export type IndexOptions< S extends StorageInterface = undefined, R extends boolean = true > = { @@ -189,9 +188,9 @@ declare module "flexsearch" { /* Index Search */ /************************************/ - type DefaultSearchResults = Id[]; - type IntermediateSearchResults = Array; - type SearchResults = R extends true ? DefaultSearchResults : Resolver; + export type DefaultSearchResults = Id[]; + export type IntermediateSearchResults = Array; + export type SearchResults = R extends true ? DefaultSearchResults : Resolver; /** * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants \ @@ -199,7 +198,7 @@ declare module "flexsearch" { * Usage: https://github.com/nextapps-de/flexsearch#usage */ - type IndexSearchResultsWrapper< + export type IndexSearchResultsWrapper< W extends boolean = false, S extends StorageInterface = undefined, R extends boolean = true @@ -327,7 +326,7 @@ declare module "flexsearch" { type WorkerConfigPath = string; type WorkerType = boolean | WorkerURL | WorkerPath; - type WorkerIndexOptions = IndexOptions & { + export type WorkerIndexOptions = IndexOptions & { config?: WorkerConfigURL | WorkerConfigPath, export?: () => Promise; import?: () => Promise; @@ -351,9 +350,9 @@ declare module "flexsearch" { /** * The template to be applied on matches (e.g. "\$1\"), where \$1 is a placeholder for the matched partial */ - type TemplateResultHighlighting = string; + export type TemplateResultHighlighting = string; type TagName = string; - type FieldName = D extends object + export type FieldName = D extends object ? { [K in keyof D]: K extends string ? D[K] extends Array @@ -363,7 +362,7 @@ declare module "flexsearch" { }[keyof D] : never; - type FieldOptions = IndexOptions & { + export type FieldOptions = IndexOptions & { field: FieldName, filter?: (content: string) => boolean; custom?: (content: string) => string | boolean; @@ -371,14 +370,14 @@ declare module "flexsearch" { db?: StorageInterface; }; - type TagOptions = { + export type TagOptions = { field: FieldName; filter?: (content: string) => boolean; custom?: (content: string) => string | boolean; db?: StorageInterface; }; - type StoreOptions = { + export type StoreOptions = { field: FieldName; filter?: (content: string) => boolean; custom?: (content: string) => string | boolean; @@ -400,7 +399,7 @@ declare module "flexsearch" { * * Document options: https://github.com/nextapps-de/flexsearch#document-options */ - type DocumentOptions< + export type DocumentOptions< D extends DocumentData = DocumentData, W extends WorkerType = false, S extends StorageInterface = undefined @@ -414,7 +413,7 @@ declare module "flexsearch" { * **Document:** * * The document descriptor: https://github.com/nextapps-de/flexsearch#the-document-descriptor */ - type DocumentDescriptor = { + export type DocumentDescriptor = { id?: string | "id"; field?: FieldName | FieldName[] | FieldOptions | Array>; index?: FieldName | FieldName[] | FieldOptions | Array>; @@ -422,25 +421,25 @@ declare module "flexsearch" { store?: FieldName | FieldName[] | StoreOptions | Array | boolean; }; - type DefaultDocumentSearchResults = Array<{ + export type DefaultDocumentSearchResults = Array<{ field?: FieldName; tag?: FieldName; result: DefaultSearchResults; }>; - type EnrichedResults = Array<{ + export type EnrichedResults = Array<{ id: Id; doc: D | null; highlight?: string; }>; - type EnrichedDocumentSearchResults = Array<{ + export type EnrichedDocumentSearchResults = Array<{ field?: FieldName; tag?: FieldName; result: EnrichedResults; }>; - type MergedDocumentSearchResults = Array<{ + export type MergedDocumentSearchResults = Array<{ id: Id; doc?: D | null; field?: FieldName[]; @@ -448,7 +447,7 @@ declare module "flexsearch" { highlight?: {[field: FieldName]: string}; }>; - type DocumentSearchResults< + export type DocumentSearchResults< D extends DocumentData = DocumentData, H extends HighlightOptions = undefined, P extends PluckOptions = undefined, @@ -469,18 +468,18 @@ declare module "flexsearch" { : DefaultDocumentSearchResults : Resolver; - type PluckOptions< + export type PluckOptions< D extends DocumentData = DocumentData, H extends HighlightOptions = undefined, R extends boolean = true, E extends boolean = false - > = FieldName | DocumentSearchOptions; + > = FieldName | DocumentSearchOptions; /** * **Document:** * * Document search options: https://github.com/nextapps-de/flexsearch#document-search-options */ - type DocumentSearchOptions< + export type DocumentSearchOptions< D extends DocumentData = DocumentData, H extends HighlightOptions = undefined, P extends PluckOptions = undefined, @@ -497,18 +496,18 @@ declare module "flexsearch" { merge?: M; }; - type DocumentValue = + export type DocumentValue = | string | number | boolean | null | DocumentData; - type DocumentData = { + export type DocumentData = { [key: string]: DocumentValue | DocumentValue[]; }; - type DocumentSearchResultsWrapper< + export type DocumentSearchResultsWrapper< D extends DocumentData = DocumentData, W extends WorkerType = false, S extends StorageInterface = undefined, @@ -579,7 +578,7 @@ declare module "flexsearch" { M extends boolean = false >( query: string, - options?: DocumentSearchOptions, + options: DocumentSearchOptions, ): DocumentSearchResultsWrapper; searchCache< H extends HighlightOptions = undefined, @@ -589,7 +588,7 @@ declare module "flexsearch" { M extends boolean = false >( query: string, - options?: DocumentSearchOptions, + options: DocumentSearchOptions, ): DocumentSearchResultsWrapper; /** @deprecated Pass "limit" within options */ @@ -792,7 +791,7 @@ declare module "flexsearch" { db?: any; }; - type DefaultResolve< + export type DefaultResolve< D extends DocumentData = DocumentData, H extends HighlightOptions = undefined, E extends boolean = false @@ -805,7 +804,7 @@ declare module "flexsearch" { highlight?: D extends DocumentData ? H : undefined; }; - type ResolverOptions< + export type ResolverOptions< D extends DocumentData = DocumentData, W extends WorkerType = false, S extends StorageInterface = undefined, @@ -827,18 +826,18 @@ declare module "flexsearch" { resolve?: R; }; - type HighlightBoundaryOptions = { + export type HighlightBoundaryOptions = { before?: number; after?: number; total?: number; }; - type HighlightEllipsisOptions = { + export type HighlightEllipsisOptions = { template?: TemplateResultHighlighting; pattern?: string | boolean; }; - type HighlightOptions = TemplateResultHighlighting | { + export type HighlightOptions = TemplateResultHighlighting | { template?: TemplateResultHighlighting; boundary?: HighlightBoundaryOptions | number; ellipsis?: HighlightEllipsisOptions | string | boolean; @@ -886,8 +885,7 @@ declare module "flexsearch" { resolve(options?: DefaultResolve): SearchResults; } - global { - class StorageInterface { + export class StorageInterface { db: any; constructor(name: string, config: PersistentOptions); @@ -902,7 +900,7 @@ declare module "flexsearch" { destroy(): Promise; clear(): Promise; - }} + } export class IndexedDB extends StorageInterface { db: IDBDatabase; @@ -924,10 +922,12 @@ declare module "flexsearch" { // ----------------------------------- declare module "flexsearch/db/*" { + import { StorageInterface } from "flexsearch"; export default StorageInterface; } declare module "flexsearch/db/indexeddb" { + import { StorageInterface } from "flexsearch"; export default class IndexedDB extends StorageInterface{ db: IDBDatabase } @@ -936,6 +936,7 @@ declare module "flexsearch/db/indexeddb" { // ----------------------------------- declare module "flexsearch/lang/*" { + import { EncoderOptions } from "flexsearch"; const Options: EncoderOptions; export default Options; } diff --git a/test/types.ts b/test/types.ts index 3d93bb5..2079ca4 100644 --- a/test/types.ts +++ b/test/types.ts @@ -5,26 +5,29 @@ import { Worker, Resolver, StorageInterface, + DefaultSearchResults, } from "flexsearch"; -import "../index"; async function test() { + const document = new Document<{ - title: string - description: string - tags: { - name: string - id: number - }[] + id: number, + title: string, + description: string, + tags: string[] }>({ document: { - index: [ "tags" ], + id: "id", + index: [ "title", "description" ], + tag: [ "tags" ], + store: [ "title", "description" ] }, }); + // The correct type const doc1 = await document.searchAsync({}); const doc2: Resolver = await document.searchAsync({ - resolve: true, + resolve: false, }); const doc3 = await document.searchAsync({ enrich: true, @@ -34,14 +37,46 @@ async function test() { merge: true, }); doc4[0].doc.title; + // The wrong type + + const t_1_1: DefaultSearchResults = document.search("test", { pluck: "title" }); // @ts-expect-error - const docw1: Resolver = await document.searchAsync({}); + const t_1_2: DefaultSearchResults = document.search("test", {}); + + const t_2_1: DefaultDocumentSearchResults = document.search("test", {}); // @ts-expect-error - const docw2: DefaultDocumentSearchResults = await document.searchAsync({ - enrich: true, - }); + const t_2_2: DefaultSearchResults = document.search("test", {}); + + const t_3_1: DefaultDocumentSearchResults = document.search({}); + const t_3_2: DefaultDocumentSearchResults = document.search({ resolve: true }); + + const t_4_1: Resolver = document.search({ resolve: false }); + const t_4_2: Resolver = new Resolver(); + // @ts-expect-error + const t_4_3: Resolver = document.search({}); + // @ts-expect-error + const t_4_4: Resolver = document.search({ resolve: true }); + + // @ts-expect-error + const docw6: DefaultDocumentSearchResults = await document.searchAsync({}); + // @ts-expect-error + const docw7: DefaultDocumentSearchResults = await document.searchAsync({ pluck: false }); + // @ts-expect-error + const docw8: DefaultDocumentSearchResults = await document.searchAsync({ enrich: false }); + + // @ts-expect-error + const docw4: Resolver = await document.searchAsync({}); + // @ts-expect-error + const docw5: Resolver = await document.searchAsync({ resolve: true }); + // @ts-expect-error + const docw6: DefaultDocumentSearchResults = await document.searchAsync({}); + // @ts-expect-error + const docw7: DefaultDocumentSearchResults = await document.searchAsync({ pluck: false }); + // @ts-expect-error + const docw8: DefaultDocumentSearchResults = await document.searchAsync({ enrich: false }); // Promise? + const documentNoWorker = new Document({}); const doc5 = documentNoWorker.search({}); // No Promise