lunr-languages icon indicating copy to clipboard operation
lunr-languages copied to clipboard

TypeDefinition for this library?

Open railsstudent opened this issue 6 years ago • 4 comments
trafficstars

Will lunr-language allow typescript imports to import "lunr-languages/lunr.stemmer.support", "lunr-languages/lunr.multi" and "lunr-languages/lunr.<locale>"?

The workaround is to require(....)(lunr) to add functions to the prototype of lunr and call them in typescript code.

I am not sure how to write the corresponding typedef.d.ts files nor can they be port to typedef files at all. Please kindly advise.

railsstudent avatar Feb 15 '19 03:02 railsstudent

I have the same problem. Here are few details more from my node.js apprication:

import lunr from "lunr";
require("lunr-languages/lunr.stemmer.support")(lunr);
require("lunr-languages/lunr.multi")(lunr);
require("lunr-languages/lunr.it")(lunr);
const englishItalianSupport = lunr.multiLanguage("en", "it");

Installed lunr types with npm --save-dev @types/lunr but Typescript continue complaining kb.ts:53:36 - error TS2339: Property 'multiLanguage' does not exist on type 'typeof lunr'.

Tried to merge multilanguage() into lunr.Index (just to satisfy the compiler) but without success.

crystalfp avatar Feb 21 '19 13:02 crystalfp

I have the same problem. Here are few details more from my node.js apprication:

import lunr from "lunr";
require("lunr-languages/lunr.stemmer.support")(lunr);
require("lunr-languages/lunr.multi")(lunr);
require("lunr-languages/lunr.it")(lunr);
const englishItalianSupport = lunr.multiLanguage("en", "it");

Installed lunr types with npm --save-dev @types/lunr but Typescript continue complaining kb.ts:53:36 - error TS2339: Property 'multiLanguage' does not exist on type 'typeof lunr'.

Tried to merge multilanguage() into lunr.Index (just to satisfy the compiler) but without success.

I encountered the same problem and ended up not using @types/lunr at all. My solution is const lunr = require('lunr'); --- the rest of the require codes ----

railsstudent avatar Feb 21 '19 13:02 railsstudent

Here is what I use on my project:

// lunr-languages.d.ts
declare module 'lunr-languages/lunr.*' {
  import lunr from 'lunr';

  function register(l: typeof lunr): void;

  export = register;
}
// lunr.d.ts
import { Builder } from 'lunr';

declare module 'lunr' {
  function multiLanguage(...lang: string[]): Builder.Plugin;
}

Put those files in your project's root and it should work automatically.

ultimaweapon avatar Aug 28 '21 20:08 ultimaweapon

Fantastic! Thanks @ultimaweapon !

crystalfp avatar Aug 30 '21 12:08 crystalfp