MikeMcl / decimal.js

An arbitrary-precision Decimal type for JavaScript
http://mikemcl.github.io/decimal.js
MIT License
6.35k stars 480 forks source link

Allow creation without the `new` keyword #216

Open frec-kenneth opened 1 year ago

frec-kenneth commented 1 year ago

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 commented 1 year ago

This issue has been raised before, I think.

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

frec-kenneth commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

@frec-kenneth

Leave the issue open and I'll investigate further.