mirror of
https://github.com/nextapps-de/flexsearch.git
synced 2025-08-27 07:44:35 +02:00
- 更新index.d.ts
- 修复`search`的返回值类型,根据worker、db推断返回类型 - 更新types.ts 测试ts静态类型
This commit is contained in:
72
index.d.ts
vendored
72
index.d.ts
vendored
@@ -177,7 +177,7 @@ declare module "flexsearch" {
|
|||||||
* * Right-To-Left: https://github.com/nextapps-de/flexsearch/doc/encoder.md#right-to-left-support
|
* * 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
|
* * Language: https://github.com/nextapps-de/flexsearch/doc/encoder.md#built-in-language-packs
|
||||||
*/
|
*/
|
||||||
type IndexOptions = {
|
type IndexOptions<D extends StorageInterface = undefined> = {
|
||||||
preset?: Preset;
|
preset?: Preset;
|
||||||
tokenize?: Tokenizer;
|
tokenize?: Tokenizer;
|
||||||
cache?: boolean | number;
|
cache?: boolean | number;
|
||||||
@@ -195,7 +195,7 @@ declare module "flexsearch" {
|
|||||||
) => number;
|
) => number;
|
||||||
|
|
||||||
// Persistent-specific options
|
// Persistent-specific options
|
||||||
db?: StorageInterface;
|
db?: D;
|
||||||
commit?: boolean;
|
commit?: boolean;
|
||||||
|
|
||||||
// Language-specific Options and Encoding
|
// Language-specific Options and Encoding
|
||||||
@@ -228,8 +228,11 @@ declare module "flexsearch" {
|
|||||||
* * Usage: https://github.com/nextapps-de/flexsearch#usage
|
* * Usage: https://github.com/nextapps-de/flexsearch#usage
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export class Index {
|
type IndexSearchResultsWrapper<W extends boolean = false, D extends StorageInterface = undefined, R extends boolean = false> =
|
||||||
constructor(options?: Preset | IndexOptions);
|
W extends false ? D extends undefined ? SearchResults<R> : Promise<SearchResults<R>> : Promise<SearchResults<R>>
|
||||||
|
|
||||||
|
export class Index<W extends boolean = false, D extends StorageInterface = undefined> {
|
||||||
|
constructor(options?: Preset | IndexOptions<D>);
|
||||||
|
|
||||||
add(id: Id, content: string): this | Promise<this>;
|
add(id: Id, content: string): this | Promise<this>;
|
||||||
|
|
||||||
@@ -242,15 +245,15 @@ declare module "flexsearch" {
|
|||||||
|
|
||||||
remove(id: Id): this | Promise<this>;
|
remove(id: Id): this | Promise<this>;
|
||||||
|
|
||||||
search(query: string, limit?: Limit): SearchResults | Promise<SearchResults>;
|
search(query: string, limit?: Limit): IndexSearchResultsWrapper<W, D>;
|
||||||
search<R extends boolean = false>(query: string, options?: SearchOptions<R>): SearchResults<R> | Promise<SearchResults<R>>;
|
search<R extends boolean = false>(query: string, options?: SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
search<R extends boolean = false>(query: string, limit: Limit, options: SearchOptions<R>): SearchResults<R> | Promise<SearchResults<R>>;
|
search<R extends boolean = false>(query: string, limit: Limit, options: SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
search<R extends boolean = false>(options: SearchOptions<R>): SearchResults<R> | Promise<SearchResults<R>>;
|
search<R extends boolean = false>(options: SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
|
|
||||||
searchCache(query: string, limit?: Limit): SearchResults | Promise<SearchResults>;
|
searchCache(query: string, limit?: Limit): W extends false ? SearchResults : Promise<SearchResults>;
|
||||||
searchCache<R extends boolean = false>(query: string, options?: Limit | SearchOptions<R>): SearchResults<R> | Promise<SearchResults<R>>;
|
searchCache<R extends boolean = false>(query: string, options?: Limit | SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
searchCache<R extends boolean = false>(query: string, limit: Limit, options: SearchOptions<R>): SearchResults<R> | Promise<SearchResults<R>>;
|
searchCache<R extends boolean = false>(query: string, limit: Limit, options: SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
searchCache<R extends boolean = false>(options: SearchOptions<R>): SearchResults<R> | Promise<SearchResults<R>>;
|
searchCache<R extends boolean = false>(options: SearchOptions<R>): IndexSearchResultsWrapper<W, D, R>;
|
||||||
|
|
||||||
// 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>;
|
||||||
@@ -328,7 +331,7 @@ declare module "flexsearch" {
|
|||||||
* * Worker index: https://github.com/nextapps-de/flexsearch#worker-index
|
* * Worker index: https://github.com/nextapps-de/flexsearch#worker-index
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export class Worker extends Index {
|
export class Worker extends Index<true> {
|
||||||
constructor(options?: Preset | WorkerIndexOptions);
|
constructor(options?: Preset | WorkerIndexOptions);
|
||||||
|
|
||||||
export(): Promise<void>;
|
export(): Promise<void>;
|
||||||
@@ -366,8 +369,12 @@ 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<D extends DocumentData = DocumentData> = IndexOptions & {
|
type WorkerType = boolean | WorkerURL | WorkerPath
|
||||||
worker?: boolean | WorkerURL | WorkerPath;
|
|
||||||
|
type DocumentOptions<D extends DocumentData = DocumentData, W extends WorkerType = false, B extends StorageInterface = undefined> =
|
||||||
|
IndexOptions<B>
|
||||||
|
& {
|
||||||
|
worker?: W;
|
||||||
doc?: DocumentDescriptor<D>;
|
doc?: DocumentDescriptor<D>;
|
||||||
document?: DocumentDescriptor<D>;
|
document?: DocumentDescriptor<D>;
|
||||||
};
|
};
|
||||||
@@ -442,14 +449,27 @@ declare module "flexsearch" {
|
|||||||
[key: string]: DocumentValue | DocumentValue[];
|
[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<D, R, E, M>
|
||||||
|
: Promise<DocumentSearchResults<D, R, E, M>>
|
||||||
|
: Promise<DocumentSearchResults<D, R, E, M>>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* **Document:**
|
* **Document:**
|
||||||
* * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants
|
* * Basic usage and variants: https://github.com/nextapps-de/flexsearch#basic-usage-and-variants
|
||||||
* * 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<D extends DocumentData = DocumentData> {
|
export class Document<D extends DocumentData = DocumentData, W extends WorkerType = false, B extends StorageInterface = undefined> {
|
||||||
constructor(options: DocumentOptions<D>);
|
constructor(options: DocumentOptions<D, W, B>);
|
||||||
|
|
||||||
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>;
|
||||||
@@ -466,32 +486,32 @@ declare module "flexsearch" {
|
|||||||
remove(document: D): 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, limit: Limit): DocumentSearchResults<D> | Promise<DocumentSearchResults<D>>;
|
search(query: string, limit: Limit): DocumentSearchResultsWrapper<D, W, B>;
|
||||||
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<D, R, E, M>,
|
options?: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
|
): DocumentSearchResultsWrapper<D, W, B, 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<D, R, E, M>,
|
options: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
|
): DocumentSearchResultsWrapper<D, W, B, 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<D, R, E, M>,
|
options: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
|
): DocumentSearchResultsWrapper<D, W, B, R, E, M>;
|
||||||
|
|
||||||
searchCache(query: string, limit: Limit): DocumentSearchResults<D> | Promise<DocumentSearchResults<D>>;
|
searchCache(query: string, limit: Limit): W extends false ? 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<D, R, E, M>,
|
options?: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
|
): DocumentSearchResultsWrapper<D, W, B, 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<D, R, E, M>,
|
limit: Limit, options: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
|
): DocumentSearchResultsWrapper<D, W, B, 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<D, R, E, M>,
|
options: DocumentSearchOptions<D, R, E, M>,
|
||||||
): DocumentSearchResults<D, R, E, M> | Promise<DocumentSearchResults<D, R, E, M>>;
|
): DocumentSearchResultsWrapper<D, W, B, 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>;
|
||||||
@@ -689,7 +709,7 @@ declare module "flexsearch" {
|
|||||||
resolve(options?: DefaultResolve): SearchResults;
|
resolve(options?: DefaultResolve): SearchResults;
|
||||||
}
|
}
|
||||||
|
|
||||||
class StorageInterface {
|
export class StorageInterface {
|
||||||
db: any;
|
db: any;
|
||||||
|
|
||||||
constructor(name: string, config: PersistentOptions);
|
constructor(name: string, config: PersistentOptions);
|
||||||
|
@@ -1,6 +1,14 @@
|
|||||||
import { DefaultDocumentSearchResults, Document, Resolver } from "flexsearch";
|
import {
|
||||||
|
DefaultDocumentSearchResults,
|
||||||
|
Document,
|
||||||
|
Index,
|
||||||
|
Worker,
|
||||||
|
Resolver,
|
||||||
|
StorageInterface,
|
||||||
|
} from "flexsearch";
|
||||||
import "../index";
|
import "../index";
|
||||||
|
|
||||||
|
async function test() {
|
||||||
const document = new Document<{
|
const document = new Document<{
|
||||||
title: string
|
title: string
|
||||||
description: string
|
description: string
|
||||||
@@ -13,8 +21,6 @@ const document = new Document<{
|
|||||||
index: [ "tags" ],
|
index: [ "tags" ],
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
async function test() {
|
|
||||||
// The correct type
|
// The correct type
|
||||||
const doc1 = await document.searchAsync({});
|
const doc1 = await document.searchAsync({});
|
||||||
const doc2: Resolver = await document.searchAsync({
|
const doc2: Resolver = await document.searchAsync({
|
||||||
@@ -35,6 +41,30 @@ async function test() {
|
|||||||
const docw2: DefaultDocumentSearchResults = await document.searchAsync({
|
const docw2: DefaultDocumentSearchResults = await document.searchAsync({
|
||||||
enrich: true,
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user