MihaiValentin / lunr-languages

A collection of languages stemmers and stopwords for Lunr Javascript library
Other
431 stars 163 forks source link

TypeDefinition for this library? #51

Open railsstudent opened 5 years ago

railsstudent commented 5 years ago

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.

crystalfp commented 5 years ago

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.

railsstudent commented 5 years ago

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

ultimaweapon commented 3 years ago

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.

crystalfp commented 3 years ago

Fantastic! Thanks @ultimaweapon !