Is it possible to use TypeScript to trigger a compile-time error if BigNumber.toString
is called?
Specifically, my application has experienced bugs because BigNumber.toString()
will express sufficiently large and sufficiently small numbers in scientific notation:
> x = new BigNumber('0.00000000001')
> x.toString()
'1e-10'
> x.toFixed()
'0.00000000001'
I've tried overriding the type:
import {BigNumber} from "bignumber.js";
declare module "bignumber.js" {
interface BigNumber {
toString: never
someOtherThing: number
}
}
But this doesn't seem to work (and the someOtherThing
field is added, so I know the type definitions are being loaded)
How can I trigger a compile error if BigNumber.toString
is used?
(also, note: I have set BigNumber.config({ EXPONENTIAL_AT: 1e+9 })
and overloaded BigNumber.toString
so it issues a warning, but it would be nice to have the added compile error)
Is it possible to use TypeScript to trigger a compile-time error if BigNumber.toString is called
Few options
Docs on tslint : https://palantir.github.io/tslint/develop/custom-rules/ You custom rule would be type checking rule : https://palantir.github.io/tslint/usage/type-checking/
And change toString
invocations on BigNumber
to be an error.
Remove the toString
method from BigNumber by editing the installed .d.ts
and commit it using PatchPackage : https://github.com/ds300/patch-package
Alternatively create a wrapper around BigNumber that doesn't expose anything you consider dangerous to your fellow developers 🌹
I would personally use patch package as its the easier route. If you are doing a lot of financial work with the library then consider a wrapper for your use case.