weixsong / elasticlunr.js

Based on lunr.js, but more flexible and customized.
http://elasticlunr.com
MIT License
2.03k stars 148 forks source link

Typescript compile issue with addField #113

Open figelwump opened 4 years ago

figelwump commented 4 years ago

I'm using elasticlunr in a typescript project. I get the following typescript compiler error when using addField:

[0] Argument of type '"url"' is not assignable to parameter of type 'never'.  TS2345

The code:

            let elasticLunrIndex = elasticlunr(function () {
                this.addField('url');
            });

I made sure all strict checking is turned off in tsconfig.json. I can workaround it for now by casting the string to never (whatever that means) like so:

            let elasticLunrIndex = elasticlunr(function () {
                this.addField('url' as never);
            });
sebastiansandqvist commented 4 years ago

Looking at the typings for elasticlunr it looks like there's a constructor argument that determines what strings are valid to pass to this.addField.

So to fix your problem:

interface Foo {
  url: string;
}

let elasticLunrIndex = elasticlunr<Foo>(function () {
  this.addField('url');
});
larrystone commented 2 years ago

Also check out a more relaxed approach here