mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-08-30 01:00:11 +02:00
- 更新index.d.ts
- 重构FieldName为泛型,支持自动推断字段定义 - 更新types.ts 测试ts静态类型
This commit is contained in:
72
index.d.ts
vendored
72
index.d.ts
vendored
@@ -91,12 +91,12 @@ declare module "flexsearch" {
|
|||||||
* **Document:**
|
* **Document:**
|
||||||
* * The document descriptor: https://github.com/nextapps-de/flexsearch#the-document-descriptor
|
* * The document descriptor: https://github.com/nextapps-de/flexsearch#the-document-descriptor
|
||||||
*/
|
*/
|
||||||
type DocumentDescriptor = {
|
type DocumentDescriptor<D extends DocumentData = DocumentData> = {
|
||||||
id?: string | "id";
|
id?: string | "id";
|
||||||
field?: FieldName | FieldName[] | FieldOptions | Array<FieldOptions>;
|
field?: FieldName<D> | FieldName<D>[] | FieldOptions<D> | Array<FieldOptions<D>>;
|
||||||
index?: FieldName | FieldName[] | FieldOptions | Array<FieldOptions>;
|
index?: FieldName<D> | FieldName<D>[] | FieldOptions<D> | Array<FieldOptions<D>>;
|
||||||
tag?: FieldName | FieldName[] | TagOptions | Array<TagOptions>;
|
tag?: FieldName<D> | FieldName<D>[] | TagOptions<D> | Array<TagOptions<D>>;
|
||||||
store?: FieldName | FieldName[] | StoreOptions | Array<StoreOptions> | boolean;
|
store?: FieldName<D> | FieldName<D>[] | StoreOptions | Array<StoreOptions> | boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
type WorkerURL = string;
|
type WorkerURL = string;
|
||||||
@@ -104,7 +104,15 @@ declare module "flexsearch" {
|
|||||||
type WorkerConfigURL = string;
|
type WorkerConfigURL = string;
|
||||||
type WorkerConfigPath = string;
|
type WorkerConfigPath = string;
|
||||||
type SerializedFunctionString = string;
|
type SerializedFunctionString = string;
|
||||||
type FieldName = string;
|
type FieldName<D = DocumentData> = D extends object
|
||||||
|
? {
|
||||||
|
[K in keyof D]: K extends string
|
||||||
|
? D[K] extends Array<infer U>
|
||||||
|
? `${K}` | `${K}[]:${FieldName<U> & string}`
|
||||||
|
: K | `${K}:${FieldName<D[K]> & string}`
|
||||||
|
: never
|
||||||
|
}[keyof D]
|
||||||
|
: never
|
||||||
/**
|
/**
|
||||||
* The template to be applied on matches (e.g. <code>"\<b>$1\</b>"</code>), where <code>\$1</code> is a placeholder for the matched partial
|
* The template to be applied on matches (e.g. <code>"\<b>$1\</b>"</code>), where <code>\$1</code> is a placeholder for the matched partial
|
||||||
*/
|
*/
|
||||||
@@ -325,15 +333,15 @@ declare module "flexsearch" {
|
|||||||
/* Document Search */
|
/* Document Search */
|
||||||
/************************************/
|
/************************************/
|
||||||
|
|
||||||
type FieldOptions = IndexOptions & {
|
type FieldOptions<D extends DocumentData = DocumentData> = IndexOptions & {
|
||||||
field: FieldName,
|
field: FieldName<D>,
|
||||||
filter?: (content: string) => boolean;
|
filter?: (content: string) => boolean;
|
||||||
custom?: (content: string) => string;
|
custom?: (content: string) => string;
|
||||||
config?: WorkerConfigURL | WorkerConfigPath;
|
config?: WorkerConfigURL | WorkerConfigPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
type TagOptions = {
|
type TagOptions<D extends DocumentData> = {
|
||||||
field: FieldName;
|
field: FieldName<D>;
|
||||||
filter?: (content: string) => boolean;
|
filter?: (content: string) => boolean;
|
||||||
custom?: (content: string) => string;
|
custom?: (content: string) => string;
|
||||||
db?: StorageInterface;
|
db?: StorageInterface;
|
||||||
@@ -351,21 +359,21 @@ declare module "flexsearch" {
|
|||||||
* * Document options: https://github.com/nextapps-de/flexsearch#document-options
|
* * Document options: https://github.com/nextapps-de/flexsearch#document-options
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type DocumentOptions = IndexOptions & {
|
type DocumentOptions<D extends DocumentData = DocumentData> = IndexOptions & {
|
||||||
worker?: boolean | WorkerURL | WorkerPath;
|
worker?: boolean | WorkerURL | WorkerPath;
|
||||||
doc?: DocumentDescriptor;
|
doc?: DocumentDescriptor<D>;
|
||||||
document?: DocumentDescriptor;
|
document?: DocumentDescriptor<D>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type DefaultDocumentSearchResults = Array<{
|
export type DefaultDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{
|
||||||
field?: FieldName;
|
field?: FieldName<D>;
|
||||||
tag?: FieldName;
|
tag?: FieldName<D>;
|
||||||
result: DefaultSearchResults;
|
result: DefaultSearchResults;
|
||||||
}>;
|
}>;
|
||||||
|
|
||||||
export type EnrichedDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{
|
export type EnrichedDocumentSearchResults<D extends DocumentData = DocumentData> = Array<{
|
||||||
field?: FieldName;
|
field?: FieldName<D>;
|
||||||
tag?: FieldName;
|
tag?: FieldName<D>;
|
||||||
result: Array<{
|
result: Array<{
|
||||||
id: Id;
|
id: Id;
|
||||||
doc: D | null;
|
doc: D | null;
|
||||||
@@ -404,13 +412,13 @@ 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<R extends boolean = false, E extends boolean = false, M extends boolean = false> =
|
type DocumentSearchOptions<D extends DocumentData = DocumentData, R extends boolean = false, E extends boolean = false, M extends boolean = false> =
|
||||||
SearchOptions<R>
|
SearchOptions<R>
|
||||||
& {
|
& {
|
||||||
tag?: Object | Array<Object>;
|
tag?: Object | Array<Object>;
|
||||||
field?: Array<DocumentSearchOptions<E, M>> | DocumentSearchOptions<E, M> | string[] | string;
|
field?: Array<DocumentSearchOptions<D, R, E, M>> | DocumentSearchOptions<D, R, E, M> | string[] | string;
|
||||||
index?: Array<DocumentSearchOptions<E, M>> | DocumentSearchOptions<E, M> | string[] | string;
|
index?: Array<DocumentSearchOptions<D, R, E, M>> | DocumentSearchOptions<D, R, E, M> | string[] | string;
|
||||||
pluck?: FieldName | DocumentSearchOptions;
|
pluck?: FieldName | DocumentSearchOptions<D, R, E, M>;
|
||||||
highlight?: HighlightOptions | TemplateResultHighlighting;
|
highlight?: HighlightOptions | TemplateResultHighlighting;
|
||||||
enrich?: E;
|
enrich?: E;
|
||||||
merge?: M;
|
merge?: M;
|
||||||
@@ -434,7 +442,7 @@ declare module "flexsearch" {
|
|||||||
* * Document store: https://github.com/nextapps-de/flexsearch#document-store
|
* * Document store: https://github.com/nextapps-de/flexsearch#document-store
|
||||||
*/
|
*/
|
||||||
export class Document<D extends DocumentData = DocumentData> {
|
export class Document<D extends DocumentData = DocumentData> {
|
||||||
constructor(options: DocumentOptions);
|
constructor(options: DocumentOptions<D>);
|
||||||
|
|
||||||
add(id: Id, document: D): this | Promise<this>;
|
add(id: Id, document: D): this | Promise<this>;
|
||||||
add(document: D): this | Promise<this>;
|
add(document: D): this | Promise<this>;
|
||||||
@@ -454,28 +462,28 @@ declare module "flexsearch" {
|
|||||||
search(query: string, limit: Limit): DocumentSearchResults<D> | Promise<DocumentSearchResults<D>>;
|
search(query: string, limit: Limit): DocumentSearchResults<D> | Promise<DocumentSearchResults<D>>;
|
||||||
search<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
search<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
query: string,
|
query: string,
|
||||||
options?: DocumentSearchOptions<R, E, M>,
|
options?: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, 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>(
|
search<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
query: string,
|
query: string,
|
||||||
limit: Limit,
|
limit: Limit,
|
||||||
options: DocumentSearchOptions<R, E, M>,
|
options: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, 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>(
|
search<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
options: DocumentSearchOptions<R, E, M>,
|
options: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, 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(query: string, limit: Limit): DocumentSearchResults<D> | Promise<DocumentSearchResults<D>>;
|
||||||
searchCache<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
searchCache<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
query: string,
|
query: string,
|
||||||
options?: DocumentSearchOptions<R, E, M>,
|
options?: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, 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>(
|
searchCache<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
query: string,
|
query: string,
|
||||||
limit: Limit, options: DocumentSearchOptions<R, E, M>,
|
limit: Limit, options: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, 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>(
|
searchCache<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
options: DocumentSearchOptions<R, E, M>,
|
options: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, 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
|
||||||
@@ -548,17 +556,17 @@ declare module "flexsearch" {
|
|||||||
searchAsync(query: string, limit?: Limit): Promise<DocumentSearchResults<D>>
|
searchAsync(query: string, limit?: Limit): Promise<DocumentSearchResults<D>>
|
||||||
searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
query: string,
|
query: string,
|
||||||
options?: DocumentSearchOptions<R, E, M>,
|
options?: DocumentSearchOptions<D, R, E, M>,
|
||||||
callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
|
callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
|
||||||
): Promise<DocumentSearchResults<D, R, E, M>>
|
): Promise<DocumentSearchResults<D, R, E, M>>
|
||||||
searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
query: string,
|
query: string,
|
||||||
limit: Limit,
|
limit: Limit,
|
||||||
options?: DocumentSearchOptions<R, E, M>,
|
options?: DocumentSearchOptions<D, R, E, M>,
|
||||||
callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
|
callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
|
||||||
): Promise<DocumentSearchResults<D, R, E, M>>;
|
): Promise<DocumentSearchResults<D, R, E, M>>;
|
||||||
searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
searchAsync<R extends boolean = false, E extends boolean = false, M extends boolean = false>(
|
||||||
options: DocumentSearchOptions<R, E, M>,
|
options: DocumentSearchOptions<D, R, E, M>,
|
||||||
callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
|
callback?: AsyncCallback<DocumentSearchResults<D, R, E, M>>,
|
||||||
): Promise<DocumentSearchResults<D, R, E, M>>;
|
): Promise<DocumentSearchResults<D, R, E, M>>;
|
||||||
}
|
}
|
||||||
|
@@ -4,8 +4,15 @@ import "../index";
|
|||||||
const document = new Document<{
|
const document = new Document<{
|
||||||
title: string
|
title: string
|
||||||
description: string
|
description: string
|
||||||
tags: string[]
|
tags: {
|
||||||
}>({});
|
name: string
|
||||||
|
id: number
|
||||||
|
}[]
|
||||||
|
}>({
|
||||||
|
document: {
|
||||||
|
index: ["tags"],
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
async function test() {
|
async function test() {
|
||||||
// The correct type
|
// The correct type
|
||||||
|
Reference in New Issue
Block a user