minisearch icon indicating copy to clipboard operation
minisearch copied to clipboard

fix(types): allow partial `weights` object

Open aleclarson opened this issue 6 months ago • 1 comments

This fixes the type of SearchOptions.weights to allow partial definition.

I also added @default comments for each weight type, to give users a baseline value to tweak in experiments.

Finally, I changed how the default weights are merged into the user-provided weights, ensuring that an explicit undefined value in the user-provided weights doesn‘t prevent the default weight from being used.

aleclarson avatar Jun 02 '25 18:06 aleclarson

Hi @aleclarson , thanks for your contribution! This is a good change, but at the moment the tests are failing due to this typing error:

src/MiniSearch.ts:681:7 - error TS2322: Type '{ fields?: string[]; filter?: (result: SearchResult) => boolean; boost: {}; boostTerm?: (term: string, i: number, terms: string[]) => number; weights: { fuzzy?: number; prefix?: number; }; ... 7 more ...; bm25: BM25Params; }' is not assignable to type 'SearchOptions & { boost: { [fieldName: string]: number; }; weights: { fuzzy: number; prefix: number; }; prefix: boolean | ((term: string, index: number, terms: string[]) => boolean); fuzzy: number | ... 1 more ... | ((term: string, index: number, terms: string[]) => number | boolean); maxFuzzy: number; combineWith: ...'.
      Type '{ fields?: string[]; filter?: (result: SearchResult) => boolean; boost: {}; boostTerm?: (term: string, i: number, terms: string[]) => number; weights: { fuzzy?: number; prefix?: number; }; ... 7 more ...; bm25: BM25Params; }' is not assignable to type '{ boost: { [fieldName: string]: number; }; weights: { fuzzy: number; prefix: number; }; prefix: boolean | ((term: string, index: number, terms: string[]) => boolean); fuzzy: number | boolean | ((term: string, index: number, terms: string[]) => number | boolean); maxFuzzy: number; combineWith: CombinationOperator; bm...'.
        The types of 'weights.fuzzy' are incompatible between these types.
          Type 'number | undefined' is not assignable to type 'number'.
            Type 'undefined' is not assignable to type 'number'.

    681       searchOptions: { ...defaultSearchOptions, ...(options.searchOptions || {}) },
              ~~~~~~~~~~~~~

If you can fix it, I am happy to merge your pull request.

lucaong avatar Jun 13 '25 21:06 lucaong