krisk / Fuse

Lightweight fuzzy-search, in JavaScript
https://fusejs.io/
Apache License 2.0
18.16k stars 767 forks source link

The type for `Fuse.search` defaults to `unknown` #527

Closed qnighy closed 3 years ago

qnighy commented 3 years ago

Describe the bug

https://github.com/krisk/Fuse/commit/2e60bee242c7b82f0d014a3a35281b34bc6b62fb changed the signature of Fuse.prototype.search.

  search<T>(
    pattern: string | Fuse.Expression,
    options?: Fuse.FuseSearchOptions
  ): Fuse.FuseResult<T>[]

Here T isn't constrained by any of the arguments. Therefore it defaults to unknown, making it meaningless.

I guess search<T>(...) was meant to be search(...), using T from the outer Fuse<T>?

Version

Fuse 6.4.5

Is this a regression?

Yes. Previously the type parameter was named R and was defaulted to T from the outer class Fuse<T>.

🔬Minimal Reproduction

declare const fuse: Fuse<{ id: number }>;
const matchedIds = fuse.search("").map(res => res.item.id);

Additional context

nwaughachukwuma commented 3 years ago

@qnighy @krisk, to resolve the issue where the type is lost and defaults to unknown, I had to the following - using the minimal reproduction above:

type K = { id: number }
declare const fuse: Fuse<K>;
const matchedIds = fuse.search<K>("").map(res => res.item.id); // <-- notice the type parameter in `search<K>`

This was not the case before and the fix pointed by @qnighy should solve this.

I guess search(...) was meant to be search(...), using T from the outer Fuse?

krisk commented 3 years ago

Good catch. Will fix.

qnighy commented 3 years ago

Thanks!