5

I tried to import fuse.js in a angular2 app, but I get an exception (Fuse is not defined). I also check definitions file (index.d.ts) in the package.

my class in typescript is :

import 'fuse.js';

export class Myclass{

  /**some code*/

  public mySearchFunction(text, list, Opts){
    let fuseSearch  = new Fuse(list, Opts);
    let fuseResults: any[] = fuseSearch.search(text);
    return fuseResults
  }

}

I tried also with

import * as Fuse from 'fuse.js';

What is the right way to use this library?

ilDug
  • 553
  • 1
  • 4
  • 11
  • 1
    I did an angular + fusejs integration that supports highlighting matches. This post is almost 1 year old, so I guess you already found a solution, but I'm dropping this here in case somebody else need it: https://www.npmjs.com/package/angular-fusejs – leblma Oct 22 '17 at 18:32

1 Answers1

4

The typings were only added to the project 8 days ago. If you look at the latest NPM release from here you'll see the typings aren't included.

If you either pull the package from master or grab files package.json and index.d.ts, then you'll be able to use the package with import * as Fuse from 'fuse.js';

Looking at the console, I'm still getting error Cannot find module 'fuse.js', but it's working. Not sure what that's all about.

Version 2.6.1 got pushed to NPM yesterday, which supports typings - see here

onetwothree
  • 672
  • 1
  • 10
  • 20
  • 1
    it doesn't work for me, I solved rewriting fuse.js as typescript class. it is a temporary solution. – ilDug Dec 04 '16 at 09:27
  • Mind posting your class? Also, an alternate fix until a new version is to pushed to NPM is to add `declare var Fuse: any;` You won't get intellisense but at least it'll stop TS from throwing compilation errors. – onetwothree Dec 06 '16 at 17:15
  • 1
    you can find the class [here](https://codepad.co/snippet/HrkAikGa) . variables have not correct type but it works. As I said , it is a temp solution – ilDug Dec 06 '16 at 19:36
  • 1
    See also https://github.com/krisk/Fuse/pull/124 with a workaround in its description, @Dag – Arjan Dec 24 '16 at 22:49
  • With @Dag's [temporary TypeScript class](https://codepad.co/snippet/HrkAikGa) I ran into `Cannot read property 'isArray' of undefined` when using it for nested paths (`Fuse.deepValue`). Changing line 102 to use a fat arrow fixes that: `getFn: (obj, path, list) => this.deepValue(obj, path, list),` The workaround from the GitHub issue in my previous comment didn't work for me either; sorry, but I forgot why. The pull request from that same GitHub issue is giving me trouble too. Oh well :-) – Arjan Dec 27 '16 at 08:01
  • A lot later, I've finally replaced @Dag's workaround with the latest from NPM, and that works fine with `import * as Fuse from 'fuse.js'`. (Minor problems with [reporting circular dependencies in my objects](https://github.com/krisk/Fuse/issues/196), but that's unrelated.) – Arjan May 16 '18 at 07:03