Skip to content

fix & enhance type definition of search() #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from Oct 31, 2019
Merged

fix & enhance type definition of search() #318

merged 3 commits into from Oct 31, 2019

Conversation

ghost
Copy link

@ghost ghost commented Jun 29, 2019

TypeScript Playground Links: Current version / PR version


fuse.search should return string[] with option includes id

Current:

const fuse = new Fuse(list, { keys: ['title'], id: 'title' });
const res: string = fuse.search('keyword');
//err ^ Type 'Data' is not assignable to type 'string'.

PR:

const fuse = new Fuse(list, { keys: ['title'], id: 'title' });
const res: string[] = fuse.search('keyword'); // ok.

score: number | undefined is troublesome to handle with --strictNullChecks

Current:

const fuse = new Fuse(list, { keys: ['title'], includeScore: true });
const res = fuse.search('keyword');
console.log(res[0].score <= 0.5);
// err:     ^ Object is possibly 'undefined'.

PR:

const fuse = new Fuse(list, { keys: ['title'], includeScore: true });
const res = fuse.search('keyword');
console.log(res[0].score <= 0.5);  // ok.

with no-literal option object, TS cannot infer type of return of search() (#303)

Current:

const option: Fuse.FuseOptions<Data> = { id: 'title', keys: ['title'], includeScore: true };
const fuse = new Fuse(list, option);
const res0 = fuse.search('keyword')[0];
const id: string = res0.item;
// error:               ^ Property 'item' does not exist on type 'Data'.
const score: number = res0.score;
// error:                  ^ Property 'score' does not exist on type 'Data'.

this is not fixed, but now type inference can be overridden

PR:

const option:Fuse.FuseOptions<Data> = { id: 'title', keys: ['title'], includeScore: true };
const fuse = new Fuse(list, option);
const res0 = fuse.search<string, true, false>('keyword')[0];
const id: string = res0.item; // ok.
const score: number = res0.score; // ok.

248ee31 is not working

Current:

const options: Fuse.FuseOptions<Data> = {
  id: 'author.lastName',
//^ Type '"author.lastName"' is not assignable to type '"title" | "author"'.
  keys: ['author.lastName']
//^ Type '"author.lastName"' is not assignable to type '"title" | "author"'.
}

PR:

const options: Fuse.FuseOptions<Data> = {
  id: 'author.lastName',  // ok.
  keys: ['author.lastName']  // ok.
}

Konjac Potage added 3 commits June 29, 2019 13:45
Split FuseResult into FuseResultWithMatches & FuseResultWithScore
replace keyof T -> keyof T | string
object path like 'author.lastName' should be parmitted.
@falsyvalues
Copy link

This looks far better than current one. I hope it will get released, thanks @konjac-potage!

@krisk krisk merged commit 5e8e2cc into krisk:master Oct 31, 2019
@krisk
Copy link
Owner

krisk commented Oct 31, 2019

Well done. I like it!

@favna
Copy link
Contributor

favna commented Nov 22, 2019

It would be nice if the sample on the website can be updated to reflect these changes. Got a lot of build errors after upgrading but thankfully it was as easy as adding as V[] to my return statement (return fuzzyFuse.search(locquery) as V[];). I'm sure it won't be as easy a solution for everyone though.

@alexkvak
Copy link

Are you sure this is not breaking change? Or even minor release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants