Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeScript Support for Compromise-Dates #1067

Open
dk4tz opened this issue Dec 18, 2023 · 5 comments
Open

Add TypeScript Support for Compromise-Dates #1067

dk4tz opened this issue Dec 18, 2023 · 5 comments

Comments

@dk4tz
Copy link

dk4tz commented Dec 18, 2023

When using TypeScript and 'compromise-dates', the plugin methods do not appear on the base 'Three' type. This breaks compilation on TSC strict projects.

image
@spencermountain
Copy link
Owner

agh, rats. Thanks @dk4tz I thought this was working.
To be honest, I'm still pretty crap at typescript, and may be already past my level of competence. You really seem to know your stuff - wanna take a look? Any changes welcome, either advice or PR
cheers

@jose-elias-alvarez
Copy link

jose-elias-alvarez commented Mar 27, 2024

I am not a TypeScript expert either, but I think the issue is that the return type annotation of nlp uses a generic, which will always default to an empty object.

I'm not sure if this the idiomatic way to handle module augmentation, but this approach seems reasonable (and, importantly, works):

// three.d.ts
export interface PluginMethods {}

declare function nlp(text: string, lexicon?: Lexicon): View & PluginMethods

Then, in the plugin's declarations:

// index.d.ts (compromise-dates)
export interface DatesMethods {
  // methods go here
}

declare module 'compromise' {
  interface PluginMethods extends DatesMethods {}
}

This approach immediately augments the module when compromise-dates is pulled in.

One alternative could be to use the return type annotation of nlp.plugin() to infer the correct return type of subsequent calls to the object:

nlp.dates("today") // error
const nlpWithDates = nlp.plugin(datePlugin);
nlpWithDates.dates("today") // no error

This is technically safer, since it would only make TypeScript aware of plugin methods when a plugin is actually registered, but it also means that code in the wild would have to be rewritten to benefit. (It's also a little beyond my current TypeScript skills to implement, though I imagine I could figure it out.)

Either way, I'm happy to put in a PR if this sounds reasonable.

@spencermountain
Copy link
Owner

you do sound like a TypeScript expert! go for it!
thank you, very much.

@SitrucL
Copy link

SitrucL commented Feb 9, 2025

Hi was there ever a PR made for this? I'm still seeing similar behaviour to the OP

Image

@spencermountain
Copy link
Owner

Yeah, I could really use some help with this stuff, I have no idea how to do this sort of stuff in typescript.

The good news is that Three doesn't have a dates method. The bad news is that we modify the prototype when we install the dates plug-in, and I don't know how to accomplish this properly in typescript config. I'm sure there's a way to do it, but it also seems to change with the seasons.

A solution could be to use the same format in the date plugin, as recently changed here.
Image
Honestly, i have no idea.

If anyone can lend hand, I'll merge anything! I don't think it's wise for me to go in and change things. haha
Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants