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

Allow creation without the new keyword #216

Open
frec-kenneth opened this issue Feb 9, 2023 · 4 comments
Open

Allow creation without the new keyword #216

frec-kenneth opened this issue Feb 9, 2023 · 4 comments

Comments

@frec-kenneth
Copy link

Decimal.js seems to support instance creation without the new keyword, ie:

Decimal(5)

But the Typescript definitions do not permit it. Is this an intentional choice? If not, could we add support to Typescript definitions by overloading the Decimal class?

export declare function Decimal(n: Decimal.Value): Decimal;

Adding this line to decimal.d.ts locally seems to have the desired effect.

decimal.js: 10.3.1
typescript: 4.9.3

@MikeMcl
Copy link
Owner

MikeMcl commented Feb 11, 2023

This issue has been raised before, I think.

There was some problem with it but I can't remember what it was.

@frec-kenneth
Copy link
Author

I had a search through the repos, but I didn't see a specific mention. I don't know Typescript well enough to assert that it's non-problematic, so happy to close this issue if necessary. If someone has more certainty though, would love to learn.

Thanks for taking a look.

@josdejong
Copy link

You can always create your own wrapper function and use that, like:

function decimal(value: string | number | Decimal) : Decimal {
  return new Decimal(value)
}

// now you can use:
decimal(5)

// instead of:
new Decimal(5)

@MikeMcl
Copy link
Owner

MikeMcl commented Feb 14, 2023

@frec-kenneth

Leave the issue open and I'll investigate further.

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

No branches or pull requests

3 participants