From 21ebe443dca902d8cb0a28dff3bba893d827db2f Mon Sep 17 00:00:00 2001 From: flycran Date: Mon, 5 May 2025 10:39:38 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E6=9B=B4=E6=96=B0index.d.ts=20=20=20-=20?= =?UTF-8?q?=E9=87=8D=E6=9E=84FieldName=E4=B8=BA=E6=B3=9B=E5=9E=8B=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E8=87=AA=E5=8A=A8=E6=8E=A8=E6=96=AD=E5=AD=97?= =?UTF-8?q?=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