From 7498972bcabd9e3ddc61bd840f0d9e40f9e29b4e Mon Sep 17 00:00:00 2001 From: 33431 Date: Mon, 5 May 2025 03:00:33 +0800 Subject: [PATCH 1/8] =?UTF-8?q?-=20=E6=B7=BB=E5=8A=A0tsconfig.json=20=20?= =?UTF-8?q?=20=E6=94=AF=E6=8C=81ts=E7=B1=BB=E5=9E=8B=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加types.ts 测试ts静态类型 --- test/types.ts | 16 ++++++++++++++++ tsconfig.json | 5 +++++ 2 files changed, 21 insertions(+) create mode 100644 test/types.ts create mode 100644 tsconfig.json diff --git a/test/types.ts b/test/types.ts new file mode 100644 index 0000000..101eea8 --- /dev/null +++ b/test/types.ts @@ -0,0 +1,16 @@ +import { Document } from 'flexsearch' +import '../index' + +const document = new Document<{ + title: string + description: string + tags: string[] +}>({}) + +async function test() { + const doc = await document.searchAsync('test', { + enrich: true, + pluck: 'test', + }) +} + diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..678ff85 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,5 @@ +{ + "compilerOptions": { + "target": "ESNext" + } +} From 94d46eb2a9a421a8c996077cc5ca82f456c35c91 Mon Sep 17 00:00:00 2001 From: 33431 Date: Mon, 5 May 2025 04:03:42 +0800 Subject: [PATCH 2/8] =?UTF-8?q?-=20=E6=9B=B4=E6=96=B0index.d.ts=20=20=20-?= =?UTF-8?q?=20=E5=AF=BC=E5=87=BA=E4=BA=86=E9=83=A8=E5=88=86=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=EF=BC=8C=E8=BF=99=E6=98=AF=E4=B8=BA=E4=BA=86=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=B1=BB=E5=9E=8B=E6=B5=8B=E8=AF=95=20=20=20-=20?= =?UTF-8?q?=E4=B8=BA`Document`=E6=B7=BB=E5=8A=A0=E6=B3=9B=E5=9E=8B=20=20?= =?UTF-8?q?=20-=20=E5=9C=A8`Document`=E5=AE=9E=E4=BE=8B=E5=8C=96=E6=97=B6?= =?UTF-8?q?=E4=BC=A0=E5=85=A5doc=E7=9A=84=E7=B1=BB=E5=9E=8B=EF=BC=8C?= =?UTF-8?q?=E5=AE=83=E5=B0=86=E4=B8=BA=E6=89=80=E6=9C=89=E8=BF=94=E5=9B=9E?= =?UTF-8?q?doc=E7=9A=84=E6=96=B9=E6=B3=95=E6=8F=90=E4=BE=9B=E6=AD=A3?= =?UTF-8?q?=E7=A1=AE=E7=9A=84=E7=B1=BB=E5=9E=8B=20=20=20-=20=E8=87=AA?= =?UTF-8?q?=E5=8A=A8=E6=8E=A8=E6=96=AD`search`,`searchCache`,`searchAsync`?= =?UTF-8?q?=E7=9A=84=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B=EF=BC=8C=E7=8E=B0?= =?UTF-8?q?=E5=9C=A8=E4=BB=96=E4=BB=AC=E4=B8=8D=E5=86=8D=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E8=81=94=E5=90=88=E7=B1=BB=E5=9E=8B=20=20=20-=20=E4=B8=BA?= =?UTF-8?q?=E4=B8=8A=E8=BF=B0=E6=96=B9=E6=B3=95=E7=9A=84`limit`=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=8D=95=E7=8B=AC=E4=BD=BF=E7=94=A8=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E9=87=8D=E8=BD=BD=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=8F=AF=E8=AF=BB?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新types.ts 测试ts静态类型 --- index.d.ts | 258 +++++++++++++++++++++++++++++++++----------------- test/types.ts | 37 ++++++-- 2 files changed, 198 insertions(+), 97 deletions(-) diff --git a/index.d.ts b/index.d.ts index 4d3fb19..f8ee434 100644 --- a/index.d.ts +++ b/index.d.ts @@ -77,14 +77,14 @@ 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; suggest?: boolean; resolution?: number; context?: boolean; - resolve?: boolean; + resolve?: R; }; /** @@ -121,19 +121,19 @@ declare module "flexsearch" { dedupe?: boolean; include?: EncoderSplitOptions; exclude?: EncoderSplitOptions; - split?: string|RegExp|""|false; + split?: string | RegExp | "" | false; numeric?: boolean; - normalize?: boolean|((str: string) => string); + normalize?: boolean | ((str: string) => string); prepare?: (str: string) => string; finalize?: (terms: string[]) => string[]; - filter?: Set|((term: string) => boolean); + filter?: Set | ((term: string) => boolean); matcher?: Map; mapper?: Map; stemmer?: Map; - replacer?: [string|RegExp, string|""]; + replacer?: [ string | RegExp, string | "" ]; minlength?: number; maxlength?: number; - cache?: boolean|number; + cache?: boolean | number; }; type EncoderSplitOptions = { @@ -142,7 +142,7 @@ declare module "flexsearch" { symbol?: boolean; punctuation?: boolean; control?: boolean; - char?: string|string[]; + char?: string | string[]; }; export const Charset: { @@ -183,7 +183,7 @@ declare module "flexsearch" { term: string, term_index: number, partial: string, - partial_index: number + partial_index: number, ) => number; // Persistent-specific options @@ -222,70 +222,87 @@ declare module "flexsearch" { export class Index { constructor(options?: Preset | IndexOptions); + add(id: Id, content: string): this | Promise; + /** * @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ append(id: Id, content: string): this | Promise; + update(id: Id, content: string): this | Promise; + remove(id: Id): this | Promise; + search(query: string, options?: Limit | SearchOptions): SearchResults | Promise; search(query: string, limit: number, options: SearchOptions): SearchResults | Promise; search(options: SearchOptions): SearchResults | Promise; + searchCache(query: string, options?: Limit | SearchOptions): SearchResults | Promise; searchCache(query: string, limit: number, options: SearchOptions): SearchResults | Promise; searchCache(options: SearchOptions): SearchResults | Promise; + // https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids contain(id: Id): boolean | Promise; + clear(): void | Promise; + cleanup(): void | Promise; // Export and Import export(handler: ExportHandler): void; export(handler: ExportHandlerAsync): Promise; + import(key: string, data: string): void; + serialize(with_function_wrapper?: boolean): SerializedFunctionString; // Persistent Index mount(db: StorageInterface): Promise; + commit(replace_all_contents?: boolean): Promise; + destroy(): Promise; // Async Methods addAsync( id: Id, content: string, - callback?: AsyncCallback + callback?: AsyncCallback, ): Promise; + /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ appendAsync( id: Id, content: string, - callback?: AsyncCallback + callback?: AsyncCallback, ): Promise; + updateAsync( id: Id, content: string, - callback?: AsyncCallback + callback?: AsyncCallback, ): Promise; + removeAsync( id: Id, - callback?: AsyncCallback + callback?: AsyncCallback, ): Promise; + searchAsync( query: string, options?: Limit | SearchOptions, - callback?: AsyncCallback + callback?: AsyncCallback, ): Promise searchAsync( query: string, limit: Limit, options?: SearchOptions, - callback?: AsyncCallback + callback?: AsyncCallback, ): Promise; searchAsync( options: SearchOptions, - callback?: AsyncCallback + callback?: AsyncCallback, ): Promise; } @@ -298,7 +315,9 @@ declare module "flexsearch" { export class Worker extends Index { constructor(options?: Preset | WorkerIndexOptions); + export(): Promise; + import(): Promise; } @@ -338,34 +357,37 @@ declare module "flexsearch" { document?: DocumentDescriptor; }; - type DefaultDocumentSearchResults = Array<{ + export type DefaultDocumentSearchResults = Array<{ field?: FieldName; tag?: FieldName; result: DefaultSearchResults; }>; - type EnrichedDocumentSearchResults = Array<{ + export type EnrichedDocumentSearchResults = Array<{ field?: FieldName; tag?: FieldName; result: Array<{ id: Id; - doc: DocumentData | null; + doc: D | null; highlight?: string; }>; }>; - type MergedDocumentSearchResults = Array<{ + export type MergedDocumentSearchResults = Array<{ id: Id; - doc: DocumentData | null; + doc: D | null; field?: FieldName[]; tag?: FieldName[]; }>; - type DocumentSearchResults = - DefaultDocumentSearchResults | - EnrichedDocumentSearchResults | - MergedDocumentSearchResults | - Resolver; + type DocumentSearchResults = + R extends true ? Resolver : + M extends true ? + MergedDocumentSearchResults : + E extends true ? + EnrichedDocumentSearchResults : + DefaultDocumentSearchResults + /** * # Document Search Result @@ -382,14 +404,16 @@ declare module "flexsearch" { * * Document search options: https://github.com/nextapps-de/flexsearch#document-search-options */ - type DocumentSearchOptions = SearchOptions & { + type DocumentSearchOptions = + SearchOptions + & { tag?: Object | Array; - field?: Array | DocumentSearchOptions | string[] | string; - index?: Array | DocumentSearchOptions | string[] | string; + field?: Array> | DocumentSearchOptions | string[] | string; + index?: Array> | DocumentSearchOptions | string[] | string; pluck?: FieldName | DocumentSearchOptions; highlight?: HighlightOptions | TemplateResultHighlighting; - enrich?: boolean; - merge?: boolean; + enrich?: E; + merge?: M; }; type DocumentValue = @@ -409,97 +433,134 @@ declare module "flexsearch" { * * API overview: https://github.com/nextapps-de/flexsearch#api-overview * * Document store: https://github.com/nextapps-de/flexsearch#document-store */ - export class Document { + export class Document { constructor(options: DocumentOptions); - add(id: Id, document: DocumentData): this | Promise; - add(document: DocumentData): this | Promise; + add(id: Id, document: D): this | Promise; + add(document: D): this | Promise; + /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ - append(id: Id, document: DocumentData): this | Promise; + append(id: Id, document: D): this | Promise; /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ - append(document: DocumentData): this | Promise; - update(id: Id, document: DocumentData): this | Promise; - update(document: DocumentData): this | Promise; + append(document: D): this | Promise; + + update(id: Id, document: D): this | Promise; + update(document: D): this | Promise; + remove(id: Id): this | Promise; - remove(document: DocumentData): this | Promise; + remove(document: D): this | Promise; + // https://github.com/nextapps-de/flexsearch#field-search - search(query: string, options?: Limit | DocumentSearchOptions): DocumentSearchResults | Promise; - search(query: string, limit: number, options: DocumentSearchOptions): DocumentSearchResults | Promise; - search(options: DocumentSearchOptions): DocumentSearchResults | Promise; - searchCache(query: string, options?: Limit | DocumentSearchOptions): DocumentSearchResults | Promise; - searchCache(query: string, limit: number, options: DocumentSearchOptions): DocumentSearchResults | Promise; - searchCache(options: DocumentSearchOptions): DocumentSearchResults | Promise; + search(query: string, limit: Limit): DocumentSearchResults | Promise>; + search( + query: string, + options?: DocumentSearchOptions, + ): DocumentSearchResults | Promise>; + search( + query: string, + limit: Limit, + options: DocumentSearchOptions, + ): DocumentSearchResults | Promise>; + search( + options: DocumentSearchOptions, + ): DocumentSearchResults | Promise>; + + searchCache(query: string, limit: Limit): DocumentSearchResults | Promise>; + searchCache( + query: string, + options?: DocumentSearchOptions, + ): DocumentSearchResults | Promise>; + searchCache( + query: string, + limit: Limit, options: DocumentSearchOptions, + ): DocumentSearchResults | Promise>; + searchCache( + options: DocumentSearchOptions, + ): DocumentSearchResults | Promise>; + // https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids contain(id: Id): boolean | Promise; + clear(): void | Promise; + cleanup(): void | Promise; - get(id: Id): Promise | DocumentData | null; - set(id: Id, document: DocumentData): this; - set(document: DocumentData): this; + + get(id: Id): Promise | D | null; + + set(id: Id, document: D): this; + set(document: D): this; // Export and Import export(handler: ExportHandler): void; export(handler: ExportHandlerAsync): Promise; + import(key: string, data: string): void; // Persistent Index mount(db: StorageInterface): Promise; + commit(replace_all_contents?: boolean): Promise; + destroy(): Promise; // Async Methods addAsync( id: Id, - document: DocumentData, - callback?: AsyncCallback + document: D, + callback?: AsyncCallback, ): Promise; addAsync( - document: DocumentData, - callback?: AsyncCallback + document: D, + callback?: AsyncCallback, ): Promise; + /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ appendAsync( id: Id, - document: DocumentData, - callback?: AsyncCallback + document: D, + callback?: AsyncCallback, ): Promise; /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ appendAsync( - document: DocumentData, - callback?: AsyncCallback + document: D, + callback?: AsyncCallback, ): Promise; + updateAsync( id: Id, - document: DocumentData, - callback?: AsyncCallback + document: D, + callback?: AsyncCallback, ): Promise; updateAsync( - document: DocumentData, - callback?: AsyncCallback + document: D, + callback?: AsyncCallback, ): Promise; + removeAsync( id: Id, - callback?: AsyncCallback + callback?: AsyncCallback, ): Promise; removeAsync( - document: DocumentData, - callback?: AsyncCallback + document: D, + callback?: AsyncCallback, ): Promise; - searchAsync( + + searchAsync(query: string, limit?: Limit): Promise> + searchAsync( query: string, - options?: Limit | DocumentSearchOptions, - callback?: AsyncCallback - ): Promise - searchAsync( + options?: DocumentSearchOptions, + callback?: AsyncCallback>, + ): Promise> + searchAsync( query: string, - limit: number, - options?: DocumentSearchOptions, - callback?: AsyncCallback - ): Promise; - searchAsync( - options: DocumentSearchOptions, - callback?: AsyncCallback - ): Promise; + limit: Limit, + options?: DocumentSearchOptions, + callback?: AsyncCallback>, + ): Promise>; + searchAsync( + options: DocumentSearchOptions, + callback?: AsyncCallback>, + ): Promise>; } type IdType = @@ -575,41 +636,64 @@ declare module "flexsearch" { export class Encoder { constructor(options?: EncoderOptions); + assign(options: EncoderOptions): this; + encode(content: string): string[]; + addMapper(char_match: string, char_replace: string): this; + addMatcher(match: string, replace: string): this; + addStemmer(match: string, replace: string): this; + addFilter(term: string): this; + addReplacer(match: string | RegExp, replace: string): this; } export class Resolver { - constructor(options?: ResolverOptions | IntermediateSearchResults); result: IntermediateSearchResults; + + constructor(options?: ResolverOptions | IntermediateSearchResults); + and(options: ResolverOptions): this; + or(options: ResolverOptions): this; + xor(options: ResolverOptions): this; + not(options: ResolverOptions): this; + limit(limit: number): this; + offset(offset: number): this; + boost(boost: number): this; + resolve(options?: DefaultResolve): SearchResults; } class StorageInterface { - constructor(name: string, config: PersistentOptions); - constructor(config: string | PersistentOptions); - mount(index: Index | Document) : Promise; - open() : Promise; - close() : Promise; - destroy() : Promise; - clear() : Promise; db: any; + + constructor(name: string, config: PersistentOptions); + + constructor(config: string | PersistentOptions); + + mount(index: Index | Document): Promise; + + open(): Promise; + + close(): Promise; + + destroy(): Promise; + + clear(): Promise; } - export class IndexedDB extends StorageInterface{ - db: IDBDatabase + export class IndexedDB extends StorageInterface { + db: IDBDatabase; } const FlexSearch: { @@ -620,7 +704,7 @@ declare module "flexsearch" { Charset: typeof Charset, Resolver: typeof Resolver, IndexedDB: typeof IndexedDB - } + }; export default FlexSearch; } diff --git a/test/types.ts b/test/types.ts index 101eea8..2617492 100644 --- a/test/types.ts +++ b/test/types.ts @@ -1,16 +1,33 @@ -import { Document } from 'flexsearch' -import '../index' +import { DefaultDocumentSearchResults, Document, Resolver } from "flexsearch"; +import "../index"; const document = new Document<{ - title: string - description: string - tags: string[] -}>({}) + title: string + description: string + tags: string[] +}>({}); async function test() { - const doc = await document.searchAsync('test', { - enrich: true, - pluck: 'test', - }) + // The correct type + const doc1 = await document.searchAsync({}); + const doc2: Resolver = await document.searchAsync({ + resolve: true, + }); + const doc3 = await document.searchAsync({ + enrich: true, + }); + const doc4 = await document.searchAsync({ + enrich: true, + merge: true, + }); + doc4[0].doc.title; + // The wrong type + // @ts-expect-error + const docw1: Resolver = await document.searchAsync({}); + // @ts-expect-error + const docw2: DefaultDocumentSearchResults = await document.searchAsync({ + enrich: true, + }); + // ... } From 21ebe443dca902d8cb0a28dff3bba893d827db2f Mon Sep 17 00:00:00 2001 From: flycran Date: Mon, 5 May 2025 10:39:38 +0800 Subject: [PATCH 3/8] =?UTF-8?q?-=20=E6=9B=B4=E6=96=B0index.d.ts=20=20=20-?= =?UTF-8?q?=20=E9=87=8D=E6=9E=84FieldName=E4=B8=BA=E6=B3=9B=E5=9E=8B?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=E8=87=AA=E5=8A=A8=E6=8E=A8=E6=96=AD?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=AE=9A=E4=B9=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新types.ts 测试ts静态类型 --- index.d.ts | 72 ++++++++++++++++++++++++++++----------------------- test/types.ts | 11 ++++++-- 2 files changed, 49 insertions(+), 34 deletions(-) diff --git a/index.d.ts b/index.d.ts index f8ee434..1a61e90 100644 --- a/index.d.ts +++ b/index.d.ts @@ -91,12 +91,12 @@ declare module "flexsearch" { * **Document:** * * The document descriptor: https://github.com/nextapps-de/flexsearch#the-document-descriptor */ - type DocumentDescriptor = { + 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; + field?: FieldName | FieldName[] | FieldOptions | Array>; + index?: FieldName | FieldName[] | FieldOptions | Array>; + tag?: FieldName | FieldName[] | TagOptions | Array>; + store?: FieldName | FieldName[] | StoreOptions | Array | boolean; }; type WorkerURL = string; @@ -104,7 +104,15 @@ declare module "flexsearch" { type WorkerConfigURL = string; type WorkerConfigPath = string; type SerializedFunctionString = string; - type FieldName = 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 */ @@ -325,15 +333,15 @@ declare module "flexsearch" { /* Document Search */ /************************************/ - type FieldOptions = IndexOptions & { - field: FieldName, + type FieldOptions = IndexOptions & { + field: FieldName, filter?: (content: string) => boolean; custom?: (content: string) => string; config?: WorkerConfigURL | WorkerConfigPath; }; - type TagOptions = { - field: FieldName; + type TagOptions = { + field: FieldName; filter?: (content: string) => boolean; custom?: (content: string) => string; db?: StorageInterface; @@ -351,21 +359,21 @@ declare module "flexsearch" { * * Document options: https://github.com/nextapps-de/flexsearch#document-options */ - type DocumentOptions = IndexOptions & { + type DocumentOptions = IndexOptions & { worker?: boolean | WorkerURL | WorkerPath; - doc?: DocumentDescriptor; - document?: DocumentDescriptor; + doc?: DocumentDescriptor; + document?: DocumentDescriptor; }; - export type DefaultDocumentSearchResults = Array<{ - field?: FieldName; - tag?: FieldName; + export type DefaultDocumentSearchResults = Array<{ + field?: FieldName; + tag?: FieldName; result: DefaultSearchResults; }>; export type EnrichedDocumentSearchResults = Array<{ - field?: FieldName; - tag?: FieldName; + field?: FieldName; + tag?: FieldName; result: Array<{ id: Id; doc: D | null; @@ -404,13 +412,13 @@ declare module "flexsearch" { * * Document search options: https://github.com/nextapps-de/flexsearch#document-search-options */ - type DocumentSearchOptions = + type DocumentSearchOptions = SearchOptions & { tag?: Object | Array; - field?: Array> | DocumentSearchOptions | string[] | string; - index?: Array> | DocumentSearchOptions | string[] | string; - pluck?: FieldName | DocumentSearchOptions; + field?: Array> | DocumentSearchOptions | string[] | string; + index?: Array> | DocumentSearchOptions | string[] | string; + pluck?: FieldName | DocumentSearchOptions; highlight?: HighlightOptions | TemplateResultHighlighting; enrich?: E; merge?: M; @@ -434,7 +442,7 @@ declare module "flexsearch" { * * Document store: https://github.com/nextapps-de/flexsearch#document-store */ export class Document { - constructor(options: DocumentOptions); + constructor(options: DocumentOptions); add(id: Id, document: D): this | Promise; add(document: D): this | Promise; @@ -454,28 +462,28 @@ declare module "flexsearch" { search(query: string, limit: Limit): DocumentSearchResults | Promise>; search( query: string, - options?: DocumentSearchOptions, + options?: DocumentSearchOptions, ): DocumentSearchResults | Promise>; search( query: string, limit: Limit, - options: DocumentSearchOptions, + options: DocumentSearchOptions, ): DocumentSearchResults | Promise>; search( - options: DocumentSearchOptions, + options: DocumentSearchOptions, ): DocumentSearchResults | Promise>; searchCache(query: string, limit: Limit): DocumentSearchResults | Promise>; searchCache( query: string, - options?: DocumentSearchOptions, + options?: DocumentSearchOptions, ): DocumentSearchResults | Promise>; searchCache( query: string, - limit: Limit, options: DocumentSearchOptions, + limit: Limit, options: DocumentSearchOptions, ): DocumentSearchResults | Promise>; searchCache( - options: DocumentSearchOptions, + options: DocumentSearchOptions, ): DocumentSearchResults | Promise>; // https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids @@ -548,17 +556,17 @@ declare module "flexsearch" { searchAsync(query: string, limit?: Limit): Promise> searchAsync( query: string, - options?: DocumentSearchOptions, + options?: DocumentSearchOptions, callback?: AsyncCallback>, ): Promise> searchAsync( query: string, limit: Limit, - options?: DocumentSearchOptions, + options?: DocumentSearchOptions, callback?: AsyncCallback>, ): Promise>; searchAsync( - options: DocumentSearchOptions, + options: DocumentSearchOptions, callback?: AsyncCallback>, ): Promise>; } diff --git a/test/types.ts b/test/types.ts index 2617492..d590462 100644 --- a/test/types.ts +++ b/test/types.ts @@ -4,8 +4,15 @@ import "../index"; const document = new Document<{ title: string description: string - tags: string[] -}>({}); + tags: { + name: string + id: number + }[] +}>({ + document: { + index: ["tags"], + }, +}); async function test() { // The correct type From 15a9ff334a5efd1a2bdfaba2d8fb159793baf950 Mon Sep 17 00:00:00 2001 From: flycran Date: Mon, 5 May 2025 10:47:57 +0800 Subject: [PATCH 4/8] =?UTF-8?q?-=20=E6=9B=B4=E6=96=B0index.d.ts=20=20=20-?= =?UTF-8?q?=20=E4=B8=BAResolver=E6=B7=BB=E5=8A=A0=E6=B3=9B=E5=9E=8B?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新types.ts 测试ts静态类型 --- index.d.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/index.d.ts b/index.d.ts index 1a61e90..f4ba065 100644 --- a/index.d.ts +++ b/index.d.ts @@ -217,9 +217,9 @@ declare module "flexsearch" { /* Index Search */ /************************************/ - type DefaultSearchResults = Id[]; + export type DefaultSearchResults = Id[]; type IntermediateSearchResults = Array; - type SearchResults = DefaultSearchResults | Resolver; + type SearchResults = DefaultSearchResults | Resolver; /** * **Document:** @@ -606,14 +606,14 @@ declare module "flexsearch" { enrich?: boolean; }; - type ResolverOptions = DefaultResolve & { - index?: Index | Document; - pluck?: FieldName; - field?: FieldName; - and?: ResolverOptions | Array; - or?: ResolverOptions | Array; - xor?: ResolverOptions | Array; - not?: ResolverOptions | Array; + 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; @@ -660,7 +660,7 @@ declare module "flexsearch" { addReplacer(match: string | RegExp, replace: string): this; } - export class Resolver { + export class Resolver { result: IntermediateSearchResults; constructor(options?: ResolverOptions | IntermediateSearchResults); @@ -679,7 +679,7 @@ declare module "flexsearch" { boost(boost: number): this; - resolve(options?: DefaultResolve): SearchResults; + resolve(options?: DefaultResolve): SearchResults; } class StorageInterface { From babbe1d73c5bec6d1899d9d0d92450565eea8d88 Mon Sep 17 00:00:00 2001 From: flycran Date: Mon, 5 May 2025 10:55:04 +0800 Subject: [PATCH 5/8] =?UTF-8?q?Revert=20"-=20=E6=9B=B4=E6=96=B0index.d.ts"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit 15a9ff334a5efd1a2bdfaba2d8fb159793baf950. --- index.d.ts | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/index.d.ts b/index.d.ts index f4ba065..1a61e90 100644 --- a/index.d.ts +++ b/index.d.ts @@ -217,9 +217,9 @@ declare module "flexsearch" { /* Index Search */ /************************************/ - export type DefaultSearchResults = Id[]; + type DefaultSearchResults = Id[]; type IntermediateSearchResults = Array; - type SearchResults = DefaultSearchResults | Resolver; + type SearchResults = DefaultSearchResults | Resolver; /** * **Document:** @@ -606,14 +606,14 @@ declare module "flexsearch" { enrich?: boolean; }; - type ResolverOptions = DefaultResolve & { - index?: Index | Document; - pluck?: FieldName; - field?: FieldName; - and?: ResolverOptions | Array>; - or?: ResolverOptions | Array>; - xor?: ResolverOptions | Array>; - not?: ResolverOptions | Array>; + 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; @@ -660,7 +660,7 @@ declare module "flexsearch" { addReplacer(match: string | RegExp, replace: string): this; } - export class Resolver { + export class Resolver { result: IntermediateSearchResults; constructor(options?: ResolverOptions | IntermediateSearchResults); @@ -679,7 +679,7 @@ declare module "flexsearch" { boost(boost: number): this; - resolve(options?: DefaultResolve): SearchResults; + resolve(options?: DefaultResolve): SearchResults; } class StorageInterface { From a685bc1f8fff915bcf82ffdd73570dd4d6e28580 Mon Sep 17 00:00:00 2001 From: flycran Date: Mon, 5 May 2025 11:05:50 +0800 Subject: [PATCH 6/8] =?UTF-8?q?-=20=E6=9B=B4=E6=96=B0index.d.ts=20=20=20-?= =?UTF-8?q?=20=E4=B8=BAIndex=E7=9A=84search=E7=AD=89=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E8=87=AA=E5=8A=A8=E6=8E=A8=E6=96=AD=E8=BF=94=E5=9B=9E=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.d.ts | 43 +++++++++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/index.d.ts b/index.d.ts index 1a61e90..ab4f5cf 100644 --- a/index.d.ts +++ b/index.d.ts @@ -108,8 +108,8 @@ declare module "flexsearch" { ? { [K in keyof D]: K extends string ? D[K] extends Array - ? `${K}` | `${K}[]:${FieldName & string}` - : K | `${K}:${FieldName & string}` + ? `${ K }` | `${ K }[]:${ FieldName & string }` + : K | `${ K }:${ FieldName & string }` : never }[keyof D] : never @@ -219,7 +219,7 @@ declare module "flexsearch" { type DefaultSearchResults = Id[]; type IntermediateSearchResults = Array; - type SearchResults = DefaultSearchResults | Resolver; + type SearchResults = R extends true ? Resolver : DefaultSearchResults; /** * **Document:** @@ -242,13 +242,15 @@ declare module "flexsearch" { remove(id: Id): this | Promise; - search(query: string, options?: Limit | SearchOptions): SearchResults | Promise; - search(query: string, limit: number, options: SearchOptions): SearchResults | Promise; - search(options: SearchOptions): SearchResults | 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>; - searchCache(query: string, options?: Limit | SearchOptions): SearchResults | Promise; - searchCache(query: string, limit: number, options: SearchOptions): SearchResults | Promise; - searchCache(options: SearchOptions): SearchResults | Promise; + 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>; // https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids contain(id: Id): boolean | Promise; @@ -299,19 +301,24 @@ declare module "flexsearch" { searchAsync( query: string, - options?: Limit | SearchOptions, + limit?: Limit, callback?: AsyncCallback, ): Promise - searchAsync( + searchAsync( + query: string, + options?: SearchOptions, + callback?: AsyncCallback>, + ): Promise> + searchAsync( query: string, limit: Limit, - options?: SearchOptions, - callback?: AsyncCallback, - ): Promise; - searchAsync( - options: SearchOptions, - callback?: AsyncCallback, - ): Promise; + options?: SearchOptions, + callback?: AsyncCallback>, + ): Promise>; + searchAsync( + options: SearchOptions, + callback?: AsyncCallback>, + ): Promise>; } /** From e0efcd8a6fc18a82e2394bd184b6f64cbaeb119e Mon Sep 17 00:00:00 2001 From: flycran Date: Tue, 6 May 2025 17:23:36 +0800 Subject: [PATCH 7/8] =?UTF-8?q?-=20=E6=9B=B4=E6=96=B0index.d.ts=20=20=20-?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D`search`=E7=9A=84=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=80=BC=E7=B1=BB=E5=9E=8B=EF=BC=8C=E6=A0=B9=E6=8D=AEworker?= =?UTF-8?q?=E3=80=81db=E6=8E=A8=E6=96=AD=E8=BF=94=E5=9B=9E=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新types.ts 测试ts静态类型 --- index.d.ts | 72 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 26 deletions(-) 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); From f32268c90236fdf1537823c09222fba894aeef7c Mon Sep 17 00:00:00 2001 From: flycran Date: Tue, 6 May 2025 17:23:36 +0800 Subject: [PATCH 8/8] =?UTF-8?q?-=20=E6=9B=B4=E6=96=B0index.d.ts=20=20=20-?= =?UTF-8?q?=20=E4=BF=AE=E5=A4=8D`search`=E7=9A=84=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=80=BC=E7=B1=BB=E5=9E=8B=EF=BC=8C=E6=A0=B9=E6=8D=AEworker?= =?UTF-8?q?=E3=80=81db=E6=8E=A8=E6=96=AD=E8=BF=94=E5=9B=9E=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 更新types.ts 测试ts静态类型 --- index.d.ts | 72 ++++++++++++++++++++++++++++++++------------------- test/types.ts | 60 +++++++++++++++++++++++++++++++----------- 2 files changed, 91 insertions(+), 41 deletions(-) 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 }