Reputation: 613
I'm trying to use BigNumber version 4.0.2 in a TypeScript program. I've created a stripped down example of a simple Node.js program written in TypeScript that uses BigNumber for this issue https://github.com/naddison36/node-imports
I'm really struggling to get TypeScript 2.3.4 to pull in the type definitions for BigNumber. Does anyone know how to do this? Here's what I've tried so far https://github.com/naddison36/node-imports#attempts-to-get-the-bignumber-type-definitions-to-work
Ideally I'd like to simply run npm install bignumber.js --save
, npm install @types/bignumber.js --save
, use ES6 imports like import BigNumber from 'bignumber.js'
and have TypeScript just work.
Any help would be appreciated
Upvotes: 1
Views: 21798
Reputation: 3247
The import style
import BigNumber from 'bignumber.js';
Only works if the library specified a default export, but as you note in the github repository.
Changing line 10 from export = BigNumber; to export default BigNumber; fixes the above error but introduces the following error:
Error:(6, 15) TS2709:Cannot use namespace 'BigNumber' as a type.
This is because the typings for bignumber.js are declared using namespace
, instead of module
. I'm not sure you can work around the typings as they are, but changing them so the beginning goes
export default BigNumber;
declare module 'bignumber.js' {
var BigNumber: BigNumberStatic;
instead of
declare var BigNumber: BigNumber.BigNumberStatic;
export as namespace BigNumber;
export = BigNumber;
declare namespace BigNumber {
works fine for me in all the normal imports of bignumber.js
import * as b from 'bignumber.js'
new b.BigNumber(123)
import b from 'bignumber.js'
new b(123)
import {BigNumber} from 'bignumber.js'
new BigNumber(123)
Upvotes: 3
Reputation: 40604
So doing this seems to work:
import * as BigNumber from 'bignumber.js';
const result: BigNumber.BigNumber = new BigNumber(2);
Possible explanation:
The typings declare a variable and a namespace both named BigNumber
:
declare var BigNumber: BigNumber.BigNumberStatic;
export as namespace BigNumber;
In the importing code, what you imported as * as BigNumber
refers to the namespace whereas the one in new BigNumber
refers to the variable declared.
Upvotes: 0