question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript plugin support

See original GitHub issue

After the final compromise.min.js update (v12.1.0) the function nlp.plugin() doesn’t work.

E.g.:

let plugin = {	
		tags:{
			Noun:
			{
			isA: 'Entity'
			},
			Verb:
			{
			isA: 'Action'
			},
		}	
	}
	nlp.plugin(plugin);

Such simple code - and it doesn’t seem to work so far. Had to roll back to 11.14.3 at least to not experience any difficulties.

Any idea?

Btw, you had an awesome project description a month ago. What happened to it?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:18 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
Drache93commented, Dec 20, 2019

One option I was looking at was to use the returned value from .extend. If plugins return a world, we can use that type to enhance the NLP type.

type Plugin<W extends DefaultWorld = DefaultWorld> = (
  Doc: DefaultDocument,
  world: DefaultWorld,
) => W;

interface NLP<World extends DefaultWorld = DefaultWorld> {}
type getWorld<N extends NLP> = N extends NLP<infer W> ? W : N;

// In real world, NLP type will be coming from `this`
const extend = <W extends DefaultWorld = DefaultWorld, N extends NLP = NLP>(
  nlp: N,
  plugin: Plugin<W>,
): NLP<getWorld<N> & W> => {
  return 'placeholder' as any; // normal extend logic here
};

const plugin = (Doc: DefaultDocument, world: DefaultWorld) => {
  return world as DefaultWorld & { b: string };
};

const plugin2 = (Doc: DefaultDocument, world: DefaultWorld) => {
  return world as DefaultWorld & { c: string };
};

const nlpx = extend({}, plugin);
const nlpx2 = extend(nlpx, plugin2);

image

Best part is by inferring the current world type stored on the NLP type, we can keep adding to it with more extends.

image

Final part, anything in NLP can then use this type…

interface NLP<World extends DefaultWorld = DefaultWorld> {
  (text: string): DefaultDocument<World>;
}

const doc = nlpx2('match this');

image

2reactions
changhzcommented, Dec 6, 2019

@spencermountain you are probably right on the .default issue. My project was initialised with npx create-react-app my-app --template typescript.

by the way, this is how i extended the original type definitions within my project to make the plugin compromise-numbers work with typescript (in which i also fixed the wrong return type that the method .has() was returning:

// nlpx.d.ts
import compromise from "compromise";

export as namespace nlpx;

declare function nlpx(text: string): nlpx.Document;

declare module nlpx {
  class Document extends compromise.Document {
    numbers(something?: any): NlpXNumber;
    has(something: string): boolean;
  }
  class NlpXNumber {
    json(x?: any): any;
    units(x?: any): any;
    isOrdinal(x?: any): any;
    isCardinal(x?: any): any;
    toNumber(x?: any): any;
    toLocaleString(x?: any): any;
    toText(x?: any): any;
    toCardinal(x?: any): any;
    toOrdinal(x?: any): any;
    isEqual(x?: any): any;
    greaterThan(x?: any): any;
    lessThan(x?: any): any;
    between(x?: any): any;
    set(x?: any): any;
    add(x?: any): any;
    subtract(x?: any): any;
    increment(x?: any): any;
    decrement(x?: any): any;
    fractions(x?: any): any;
    romanNumerals(x?: any): any;
    money(x?: any): any;
  }
}

export default nlpx;

then export an nlp constructor which will use the extended type definitions (i call it nlpx):

// nlpx.ts
import compromise from "compromise";
import nlpx from "types/nlpx";

compromise.extend(require("compromise-numbers").default);

export default function main(text: string) {
  return (compromise(text) as unknown) as nlpx.Document;
}

export const numeric = (text: string) => {
  const doc = main(text);
  doc.numbers().toNumber();
  return doc.text();
};

export type Document = nlpx.Document;
Read more comments on GitHub >

github_iconTop Results From Across the Web

plugins - TSConfig Option - TypeScript
TSConfig. plugins. Customize. Site Colours: System, Always Light, Always Dark. Code Font: Cascadia, Cascadia (ligatures), Consolas, Dank Mono, Fira Code ...
Read more >
Writing a Language Service Plugin · microsoft/TypeScript Wiki
The purpose of this guide is to help you write your own plugin. What's a Language Service Plugin? TypeScript Language Service Plugins ("plugins") ......
Read more >
How to write a Typescript plugin? - javascript - Stack Overflow
How to write a Typescript plugin? · 1. You can download github.com/microsoft/typescript, include it into your project and via Compiler API or ...
Read more >
TypeScript | IntelliJ IDEA Documentation - JetBrains
Javascript and TypeScript - The plugin is available only in IntelliJ IDEA Ultimate, ... IntelliJ IDEA supports developing, running, and debugging TypeScript ......
Read more >
@typescript-eslint/eslint-plugin - npm
TypeScript plugin for ESLint. Latest version: 5.47.1, last published: 2 days ago. Start using @typescript-eslint/eslint-plugin in your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found