krisk / Fuse

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

feat: refine type includeMatches and includeScore #662

Closed ahaoboy closed 7 months ago

ahaoboy commented 2 years ago

For better type infer

const data = [{ id: '1' }]
{
  const fuse = new Fuse(data, {
    keys: ['id'],
    includeScore: true
  })
  const r = fuse.search('1')

  // ok
  r.sort((a, b) => a.score - b.score)
}
{
  const fuse = new Fuse(data, {
    keys: ['id']
  })
  const r = fuse.search('1')

  //  error: Property 'score' does not exist on type '{ item: { id: string; }; refIndex: number; }'
  r.sort((a, b) => a.score - b.score)
}

{
  const fuse = new Fuse(data, {
    keys: ['id'],
    includeMatches: true
  })
  const r = fuse.search('1')

  // ok
  r.sort((a, b) => a.matches.length - b.matches.length)
}

{
  const fuse = new Fuse(data, {
    keys: ['id'],
    includeMatches: false
  })
  const r = fuse.search('1')

  // error: Property 'matches' does not exist on type '{ item: { id: string; }; refIndex: number; }'.
  r.sort((a, b) => a.matches.length - b.matches.length)
}