Fuse: [Typescript Question] what is the way to use this library with typescript?

Created on 20 Mar 2020  路  13Comments  路  Source: krisk/Fuse

Hi,
I am trying to use this library in my typescript project but couldn't figure it out how.
here is my code that is working:

import Fuse from "fuse.js";
const fuse = new Fuse(metaData, {
    keys: [
      { name: "title", weight: 0.3 },
      { name: "tags", weight: 0.7 }
    ]
  });
const result = fuse
            .search(inputValue)
            .find(el => el.item.key === 'something');

but at last line it's getting me error:
Property 'item' does not exist
I know the code is correct because it is working with // @ts-ignore` but what's wrong with typing?

question

Most helpful comment

I updated to v 5.0.9-beta and it's working 馃憤
thank you so much!

All 13 comments

I'll add examples to the docs. But something like this:

// Requires --esModuleInterop compiler flag
// import * as Fuse with '--allowSyntheticDefaultImport'
// or import Fuse = require('fuse.js') with neither
import Fuse from 'fuse.js';

type SimpleBookFuse = {
    title: string;
    author: {
        firstName: string;
        lastName: string;
    };
    tags: string[]
};

const books: SimpleBookFuse[] = [{
  'title': "Old Man's War",
  'author': 'John Scalzi',
  'tags': ['fiction']
}, {
  'title': 'The Lock Artist',
  'author': 'Steve',
  'tags': ['thriller']
}]

const options: Fuse.FuseOptions<SimpleBookFuse> = {
  keys: ['author', 'tags'],
};
const fuse = new Fuse(books, options)
const results = fuse.search('tion')

I did it but still

const fuseOptions: Fuse.FuseOptions<MetaData> = {
    keys: [
      { name: "title", weight: 0.3 },
      { name: "tags", weight: 0.7 }
    ]
  };
const fuse = new Fuse(metaData, fuseOptions);
const result = fuse.search(inputValue);
const a = result.map(el => el.item)

at last line, I am getting error:

This expression is not callable.
  Each member of the union type '(<U>(callbackfn: (value: MetaData, index: number, array: MetaData[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: FuseResultWithMatches<MetaData>, index: number, array: FuseResultWithMatches<...>[]) => U, thisArg?: any) => U[]) | (<U>(callbackfn: (value: FuseResultWithScore<...>, index: number, array: Fus...' has signatures, but none of those signatures are compatible with each other.ts(2349)

What version of Fuse.js are you currently using?

"fuse.js": "^5.0.7-beta",
"typescript": "^3.8.3"

I believe v5.0.7-beta typings are not entirely correct (even from before). I've updated them as part of #367. Can you try pulling that in instead and check?

I have a similar test here as part of that PR, and seems to work fine.

I plan to run a few more tests, merge the branch if all is good, and publish the new build. ETA is 1-3 days.

doesn't seem like its working for me as well in ReactJS
Error I'm getting

Property 'FuseOptions' does not exist on type 'typeof Fuse'.ts(2339)

@ericjiang97 Can you show me how you're using it, include the import statement? Thanks

this is a sandbox that shows the problem
https://codesandbox.io/s/fusejs-test-rusz9

@saostad that has been fixed in #367.

thanks 馃憤 , how or when I can use it?

The latest beta has published on npm. The CDN should be updating soon, as there's an obvious lag there which I cannot control 馃槩.

If you want to play with it immediately, the files you're looking for are in the dist folder.

I updated to v 5.0.9-beta and it's working 馃憤
thank you so much!

Yeah it work for me too when I upgraded!

Was this page helpful?
0 / 5 - 0 ratings