From 94d46eb2a9a421a8c996077cc5ca82f456c35c91 Mon Sep 17 00:00:00 2001 From: 33431 Date: Mon, 5 May 2025 04:03:42 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E6=9B=B4=E6=96=B0index.d.ts=20=20=20-=20?= =?UTF-8?q?=E5=AF=BC=E5=87=BA=E4=BA=86=E9=83=A8=E5=88=86=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=EF=BC=8C=E8=BF=99=E6=98=AF=E4=B8=BA=E4=BA=86=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=B1=BB=E5=9E=8B=E6=B5=8B=E8=AF=95=20=20=20-=20=E4=B8=BA`Docu?= =?UTF-8?q?ment`=E6=B7=BB=E5=8A=A0=E6=B3=9B=E5=9E=8B=20=20=20-=20=E5=9C=A8?= =?UTF-8?q?`Document`=E5=AE=9E=E4=BE=8B=E5=8C=96=E6=97=B6=E4=BC=A0?= =?UTF-8?q?=E5=85=A5doc=E7=9A=84=E7=B1=BB=E5=9E=8B=EF=BC=8C=E5=AE=83?= =?UTF-8?q?=E5=B0=86=E4=B8=BA=E6=89=80=E6=9C=89=E8=BF=94=E5=9B=9Edoc?= =?UTF-8?q?=E7=9A=84=E6=96=B9=E6=B3=95=E6=8F=90=E4=BE=9B=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E7=9A=84=E7=B1=BB=E5=9E=8B=20=20=20-=20=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=8E=A8=E6=96=AD`search`,`searchCache`,`searchAsync`=E7=9A=84?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=B1=BB=E5=9E=8B=EF=BC=8C=E7=8E=B0=E5=9C=A8?= =?UTF-8?q?=E4=BB=96=E4=BB=AC=E4=B8=8D=E5=86=8D=E8=BF=94=E5=9B=9E=E8=81=94?= =?UTF-8?q?=E5=90=88=E7=B1=BB=E5=9E=8B=20=20=20-=20=E4=B8=BA=E4=B8=8A?= =?UTF-8?q?=E8=BF=B0=E6=96=B9=E6=B3=95=E7=9A=84`limit`=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E5=8D=95=E7=8B=AC=E4=BD=BF=E7=94=A8=E5=87=BD=E6=95=B0=E9=87=8D?= =?UTF-8?q?=E8=BD=BD=EF=BC=8C=E5=A2=9E=E5=8A=A0=E5=8F=AF=E8=AF=BB=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, + }); + // ... }