Fuse icon indicating copy to clipboard operation
Fuse copied to clipboard

feat: refine type includeMatches and includeScore

Open ahaoboy opened this issue 3 years ago • 0 comments

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)
}

ahaoboy avatar May 13 '22 08:05 ahaoboy