1
0
mirror of https://github.com/nextapps-de/flexsearch.git synced 2025-08-28 08:10:54 +02:00

- 更新index.d.ts

- 导出了部分类型,这是为了支持类型测试
  - 为`Document`添加泛型
  - 在`Document`实例化时传入doc的类型,它将为所有返回doc的方法提供正确的类型
  - 自动推断`search`,`searchCache`,`searchAsync`的返回类型,现在他们不再返回联合类型
  - 为上述方法的`limit`参数单独使用函数重载,增加可读性

- 更新types.ts
  测试ts静态类型
This commit is contained in:
33431
2025-05-05 04:03:42 +08:00
parent 7498972bca
commit 94d46eb2a9
2 changed files with 198 additions and 97 deletions

258
index.d.ts vendored
View File

@@ -77,14 +77,14 @@ declare module "flexsearch" {
* **Document:** * **Document:**
* * Search options: https://github.com/nextapps-de/flexsearch#search-options * * Search options: https://github.com/nextapps-de/flexsearch#search-options
*/ */
type SearchOptions = { export type SearchOptions<R extends boolean = false> = {
query?: string; query?: string;
limit?: number; limit?: number;
offset?: number; offset?: number;
suggest?: boolean; suggest?: boolean;
resolution?: number; resolution?: number;
context?: boolean; context?: boolean;
resolve?: boolean; resolve?: R;
}; };
/** /**
@@ -121,19 +121,19 @@ declare module "flexsearch" {
dedupe?: boolean; dedupe?: boolean;
include?: EncoderSplitOptions; include?: EncoderSplitOptions;
exclude?: EncoderSplitOptions; exclude?: EncoderSplitOptions;
split?: string|RegExp|""|false; split?: string | RegExp | "" | false;
numeric?: boolean; numeric?: boolean;
normalize?: boolean|((str: string) => string); normalize?: boolean | ((str: string) => string);
prepare?: (str: string) => string; prepare?: (str: string) => string;
finalize?: (terms: string[]) => string[]; finalize?: (terms: string[]) => string[];
filter?: Set<string>|((term: string) => boolean); filter?: Set<string> | ((term: string) => boolean);
matcher?: Map<string, string>; matcher?: Map<string, string>;
mapper?: Map<string, string>; mapper?: Map<string, string>;
stemmer?: Map<string, string>; stemmer?: Map<string, string>;
replacer?: [string|RegExp, string|""]; replacer?: [ string | RegExp, string | "" ];
minlength?: number; minlength?: number;
maxlength?: number; maxlength?: number;
cache?: boolean|number; cache?: boolean | number;
}; };
type EncoderSplitOptions = { type EncoderSplitOptions = {
@@ -142,7 +142,7 @@ declare module "flexsearch" {
symbol?: boolean; symbol?: boolean;
punctuation?: boolean; punctuation?: boolean;
control?: boolean; control?: boolean;
char?: string|string[]; char?: string | string[];
}; };
export const Charset: { export const Charset: {
@@ -183,7 +183,7 @@ declare module "flexsearch" {
term: string, term: string,
term_index: number, term_index: number,
partial: string, partial: string,
partial_index: number partial_index: number,
) => number; ) => number;
// Persistent-specific options // Persistent-specific options
@@ -222,70 +222,87 @@ declare module "flexsearch" {
export class Index { export class Index {
constructor(options?: Preset | IndexOptions); constructor(options?: Preset | IndexOptions);
add(id: Id, content: string): this | Promise<this>; add(id: Id, content: string): this | Promise<this>;
/** /**
* @deprecated The method "append" will be removed in an upcoming release, just use "add" instead * @deprecated The method "append" will be removed in an upcoming release, just use "add" instead
*/ */
append(id: Id, content: string): this | Promise<this>; append(id: Id, content: string): this | Promise<this>;
update(id: Id, content: string): this | Promise<this>; update(id: Id, content: string): this | Promise<this>;
remove(id: Id): this | Promise<this>; remove(id: Id): this | Promise<this>;
search(query: string, options?: Limit | SearchOptions): SearchResults | Promise<SearchResults>; search(query: string, options?: Limit | SearchOptions): SearchResults | Promise<SearchResults>;
search(query: string, limit: number, options: SearchOptions): SearchResults | Promise<SearchResults>; search(query: string, limit: number, options: SearchOptions): SearchResults | Promise<SearchResults>;
search(options: SearchOptions): SearchResults | Promise<SearchResults>; search(options: SearchOptions): SearchResults | Promise<SearchResults>;
searchCache(query: string, options?: Limit | SearchOptions): SearchResults | Promise<SearchResults>; searchCache(query: string, options?: Limit | SearchOptions): SearchResults | Promise<SearchResults>;
searchCache(query: string, limit: number, options: SearchOptions): SearchResults | Promise<SearchResults>; searchCache(query: string, limit: number, options: SearchOptions): SearchResults | Promise<SearchResults>;
searchCache(options: SearchOptions): SearchResults | Promise<SearchResults>; searchCache(options: SearchOptions): SearchResults | Promise<SearchResults>;
// https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids // https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids
contain(id: Id): boolean | Promise<boolean>; contain(id: Id): boolean | Promise<boolean>;
clear(): void | Promise<void>; clear(): void | Promise<void>;
cleanup(): void | Promise<void>; cleanup(): void | Promise<void>;
// Export and Import // Export and Import
export(handler: ExportHandler): void; export(handler: ExportHandler): void;
export(handler: ExportHandlerAsync): Promise<void>; export(handler: ExportHandlerAsync): Promise<void>;
import(key: string, data: string): void; import(key: string, data: string): void;
serialize(with_function_wrapper?: boolean): SerializedFunctionString; serialize(with_function_wrapper?: boolean): SerializedFunctionString;
// Persistent Index // Persistent Index
mount(db: StorageInterface): Promise<void>; mount(db: StorageInterface): Promise<void>;
commit(replace_all_contents?: boolean): Promise<void>; commit(replace_all_contents?: boolean): Promise<void>;
destroy(): Promise<void>; destroy(): Promise<void>;
// Async Methods // Async Methods
addAsync( addAsync(
id: Id, id: Id,
content: string, content: string,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
appendAsync( appendAsync(
id: Id, id: Id,
content: string, content: string,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
updateAsync( updateAsync(
id: Id, id: Id,
content: string, content: string,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
removeAsync( removeAsync(
id: Id, id: Id,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
searchAsync( searchAsync(
query: string, query: string,
options?: Limit | SearchOptions, options?: Limit | SearchOptions,
callback?: AsyncCallback<SearchResults> callback?: AsyncCallback<SearchResults>,
): Promise<SearchResults> ): Promise<SearchResults>
searchAsync( searchAsync(
query: string, query: string,
limit: Limit, limit: Limit,
options?: SearchOptions, options?: SearchOptions,
callback?: AsyncCallback<SearchResults> callback?: AsyncCallback<SearchResults>,
): Promise<SearchResults>; ): Promise<SearchResults>;
searchAsync( searchAsync(
options: SearchOptions, options: SearchOptions,
callback?: AsyncCallback<SearchResults> callback?: AsyncCallback<SearchResults>,
): Promise<SearchResults>; ): Promise<SearchResults>;
} }
@@ -298,7 +315,9 @@ declare module "flexsearch" {
export class Worker extends Index { export class Worker extends Index {
constructor(options?: Preset | WorkerIndexOptions); constructor(options?: Preset | WorkerIndexOptions);
export(): Promise<void>; export(): Promise<void>;
import(): Promise<void>; import(): Promise<void>;
} }
@@ -338,34 +357,37 @@ declare module "flexsearch" {
document?: DocumentDescriptor; document?: DocumentDescriptor;
}; };
type DefaultDocumentSearchResults = Array<{ export type DefaultDocumentSearchResults = Array<{
field?: FieldName; field?: FieldName;
tag?: FieldName; tag?: FieldName;
result: DefaultSearchResults; result: DefaultSearchResults;
}>; }>;
type EnrichedDocumentSearchResults = Array<{ export type EnrichedDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{
field?: FieldName; field?: FieldName;
tag?: FieldName; tag?: FieldName;
result: Array<{ result: Array<{
id: Id; id: Id;
doc: DocumentData | null; doc: D | null;
highlight?: string; highlight?: string;
}>; }>;
}>; }>;
type MergedDocumentSearchResults = Array<{ export type MergedDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{
id: Id; id: Id;
doc: DocumentData | null; doc: D | null;
field?: FieldName[]; field?: FieldName[];
tag?: FieldName[]; tag?: FieldName[];
}>; }>;
type DocumentSearchResults = type DocumentSearchResults<D extends DocumentData = DocumentData, R extends boolean = false, E extends boolean = false, M extends boolean = false> =
DefaultDocumentSearchResults | R extends true ? Resolver :
EnrichedDocumentSearchResults | M extends true ?
MergedDocumentSearchResults | MergedDocumentSearchResults<D> :
Resolver; E extends true ?
EnrichedDocumentSearchResults<D> :
DefaultDocumentSearchResults
/** /**
* # Document Search Result * # Document Search Result
@@ -382,14 +404,16 @@ declare module "flexsearch" {
* * Document search options: https://github.com/nextapps-de/flexsearch#document-search-options * * Document search options: https://github.com/nextapps-de/flexsearch#document-search-options
*/ */
type DocumentSearchOptions = SearchOptions & { type DocumentSearchOptions<R extends boolean = false, E extends boolean = false, M extends boolean = false> =
SearchOptions<R>
& {
tag?: Object | Array<Object>; tag?: Object | Array<Object>;
field?: Array<DocumentSearchOptions> | DocumentSearchOptions | string[] | string; field?: Array<DocumentSearchOptions<E, M>> | DocumentSearchOptions<E, M> | string[] | string;
index?: Array<DocumentSearchOptions> | DocumentSearchOptions | string[] | string; index?: Array<DocumentSearchOptions<E, M>> | DocumentSearchOptions<E, M> | string[] | string;
pluck?: FieldName | DocumentSearchOptions; pluck?: FieldName | DocumentSearchOptions;
highlight?: HighlightOptions | TemplateResultHighlighting; highlight?: HighlightOptions | TemplateResultHighlighting;
enrich?: boolean; enrich?: E;
merge?: boolean; merge?: M;
}; };
type DocumentValue = type DocumentValue =
@@ -409,97 +433,134 @@ declare module "flexsearch" {
* * API overview: https://github.com/nextapps-de/flexsearch#api-overview * * API overview: https://github.com/nextapps-de/flexsearch#api-overview
* * Document store: https://github.com/nextapps-de/flexsearch#document-store * * Document store: https://github.com/nextapps-de/flexsearch#document-store
*/ */
export class Document { export class Document<D extends DocumentData = DocumentData> {
constructor(options: DocumentOptions); constructor(options: DocumentOptions);
add(id: Id, document: DocumentData): this | Promise<this>; add(id: Id, document: D): this | Promise<this>;
add(document: DocumentData): this | Promise<this>; add(document: D): this | Promise<this>;
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
append(id: Id, document: DocumentData): this | Promise<this>; append(id: Id, document: D): this | Promise<this>;
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
append(document: DocumentData): this | Promise<this>; append(document: D): this | Promise<this>;
update(id: Id, document: DocumentData): this | Promise<this>;
update(document: DocumentData): this | Promise<this>; update(id: Id, document: D): this | Promise<this>;
update(document: D): this | Promise<this>;
remove(id: Id): this | Promise<this>; remove(id: Id): this | Promise<this>;
remove(document: DocumentData): this | Promise<this>; remove(document: D): this | Promise<this>;
// https://github.com/nextapps-de/flexsearch#field-search // https://github.com/nextapps-de/flexsearch#field-search
search(query: string, options?: Limit | DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>; search(query: string, limit: Limit): DocumentSearchResults<D> | Promise<DocumentSearchResults<D>>;
search(query: string, limit: number, options: DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>; search<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
search(options: DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>; query: string,
searchCache(query: string, options?: Limit | DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>; options?: DocumentSearchOptions<R, E, M>,
searchCache(query: string, limit: number, options: DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>; ): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
searchCache(options: DocumentSearchOptions): DocumentSearchResults | Promise<DocumentSearchResults>; search<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
query: string,
limit: Limit,
options: DocumentSearchOptions<R, E, M>,
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
search<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
options: DocumentSearchOptions<R, E, M>,
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
searchCache(query: string, limit: Limit): DocumentSearchResults<D> | Promise<DocumentSearchResults<D>>;
searchCache<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
query: string,
options?: DocumentSearchOptions<R, E, M>,
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
searchCache<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
query: string,
limit: Limit, options: DocumentSearchOptions<R, E, M>,
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
searchCache<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
options: DocumentSearchOptions<R, E, M>,
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
// https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids // https://github.com/nextapps-de/flexsearch#check-existence-of-already-indexed-ids
contain(id: Id): boolean | Promise<boolean>; contain(id: Id): boolean | Promise<boolean>;
clear(): void | Promise<void>; clear(): void | Promise<void>;
cleanup(): void | Promise<void>; cleanup(): void | Promise<void>;
get(id: Id): Promise<DocumentData> | DocumentData | null;
set(id: Id, document: DocumentData): this; get(id: Id): Promise<D> | D | null;
set(document: DocumentData): this;
set(id: Id, document: D): this;
set(document: D): this;
// Export and Import // Export and Import
export(handler: ExportHandler): void; export(handler: ExportHandler): void;
export(handler: ExportHandlerAsync): Promise<void>; export(handler: ExportHandlerAsync): Promise<void>;
import(key: string, data: string): void; import(key: string, data: string): void;
// Persistent Index // Persistent Index
mount(db: StorageInterface): Promise<void>; mount(db: StorageInterface): Promise<void>;
commit(replace_all_contents?: boolean): Promise<void>; commit(replace_all_contents?: boolean): Promise<void>;
destroy(): Promise<void>; destroy(): Promise<void>;
// Async Methods // Async Methods
addAsync( addAsync(
id: Id, id: Id,
document: DocumentData, document: D,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
addAsync( addAsync(
document: DocumentData, document: D,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
appendAsync( appendAsync(
id: Id, id: Id,
document: DocumentData, document: D,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
/** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */ /** @deprecated The method "append" will be removed in an upcoming release, just use "add" instead */
appendAsync( appendAsync(
document: DocumentData, document: D,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
updateAsync( updateAsync(
id: Id, id: Id,
document: DocumentData, document: D,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
updateAsync( updateAsync(
document: DocumentData, document: D,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
removeAsync( removeAsync(
id: Id, id: Id,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
removeAsync( removeAsync(
document: DocumentData, document: D,
callback?: AsyncCallback<void> callback?: AsyncCallback<void>,
): Promise<this>; ): Promise<this>;
searchAsync(
searchAsync(query: string, limit?: Limit): Promise<DocumentSearchResults<D>>
searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
query: string, query: string,
options?: Limit | DocumentSearchOptions, options?: DocumentSearchOptions<R, E, M>,
callback?: AsyncCallback<DocumentSearchResults> callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
): Promise<DocumentSearchResults> ): Promise<DocumentSearchResults<D, R, E, M>>
searchAsync( searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
query: string, query: string,
limit: number, limit: Limit,
options?: DocumentSearchOptions, options?: DocumentSearchOptions<R, E, M>,
callback?: AsyncCallback<DocumentSearchResults> callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
): Promise<DocumentSearchResults>; ): Promise<DocumentSearchResults<D, R, E, M>>;
searchAsync( searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
options: DocumentSearchOptions, options: DocumentSearchOptions<R, E, M>,
callback?: AsyncCallback<DocumentSearchResults> callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
): Promise<DocumentSearchResults>; ): Promise<DocumentSearchResults<D, R, E, M>>;
} }
type IdType = type IdType =
@@ -575,41 +636,64 @@ declare module "flexsearch" {
export class Encoder { export class Encoder {
constructor(options?: EncoderOptions); constructor(options?: EncoderOptions);
assign(options: EncoderOptions): this; assign(options: EncoderOptions): this;
encode(content: string): string[]; encode(content: string): string[];
addMapper(char_match: string, char_replace: string): this; addMapper(char_match: string, char_replace: string): this;
addMatcher(match: string, replace: string): this; addMatcher(match: string, replace: string): this;
addStemmer(match: string, replace: string): this; addStemmer(match: string, replace: string): this;
addFilter(term: string): this; addFilter(term: string): this;
addReplacer(match: string | RegExp, replace: string): this; addReplacer(match: string | RegExp, replace: string): this;
} }
export class Resolver { export class Resolver {
constructor(options?: ResolverOptions | IntermediateSearchResults);
result: IntermediateSearchResults; result: IntermediateSearchResults;
constructor(options?: ResolverOptions | IntermediateSearchResults);
and(options: ResolverOptions): this; and(options: ResolverOptions): this;
or(options: ResolverOptions): this; or(options: ResolverOptions): this;
xor(options: ResolverOptions): this; xor(options: ResolverOptions): this;
not(options: ResolverOptions): this; not(options: ResolverOptions): this;
limit(limit: number): this; limit(limit: number): this;
offset(offset: number): this; offset(offset: number): this;
boost(boost: number): this; boost(boost: number): this;
resolve(options?: DefaultResolve): SearchResults; resolve(options?: DefaultResolve): SearchResults;
} }
class StorageInterface { class StorageInterface {
constructor(name: string, config: PersistentOptions);
constructor(config: string | PersistentOptions);
mount(index: Index | Document) : Promise<void>;
open() : Promise<void>;
close() : Promise<void>;
destroy() : Promise<void>;
clear() : Promise<void>;
db: any; db: any;
constructor(name: string, config: PersistentOptions);
constructor(config: string | PersistentOptions);
mount(index: Index | Document): Promise<void>;
open(): Promise<void>;
close(): Promise<void>;
destroy(): Promise<void>;
clear(): Promise<void>;
} }
export class IndexedDB extends StorageInterface{ export class IndexedDB extends StorageInterface {
db: IDBDatabase db: IDBDatabase;
} }
const FlexSearch: { const FlexSearch: {
@@ -620,7 +704,7 @@ declare module "flexsearch" {
Charset: typeof Charset, Charset: typeof Charset,
Resolver: typeof Resolver, Resolver: typeof Resolver,
IndexedDB: typeof IndexedDB IndexedDB: typeof IndexedDB
} };
export default FlexSearch; export default FlexSearch;
} }

View File

@@ -1,16 +1,33 @@
import { Document } from 'flexsearch' import { DefaultDocumentSearchResults, Document, Resolver } from "flexsearch";
import '../index' import "../index";
const document = new Document<{ const document = new Document<{
title: string title: string
description: string description: string
tags: string[] tags: string[]
}>({}) }>({});
async function test() { async function test() {
const doc = await document.searchAsync('test', { // The correct type
enrich: true, const doc1 = await document.searchAsync({});
pluck: 'test', 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,
});
// ...
} }