MikeMcl / bignumber.js

A JavaScript library for arbitrary-precision decimal and non-decimal arithmetic
http://mikemcl.github.io/bignumber.js
MIT License
6.68k stars 742 forks source link

BigNumber TypeError: Not a function #270

Closed claytn closed 4 years ago

claytn commented 4 years ago

BigNumber.js Version: 9.0.0

In my Typescript+React Native project I'm trying to pull in the BigNumber library, but none of the helper functions are available. When doing something like...

const a = new BigNumber(5)
a.plus(1)

I get an error saying: TypeError: a.plus is not a function (In a.plus(1), a.plus is undefined)

I have run across old issues where other users have said I need to update my import statement. I've tried all of the following imports and none of them have fixed the above issue.

import BigNumber from 'bignumber.js'
import { BigNumber } from 'bignumber.js';
import BigNumber from 'bignumber.js/bignumber'
import { BigNumber } from 'bignumber.js/bignumber';
import BigNumber from 'bignumber.js/bignumber.js'
import { BigNumber } from 'bignumber.js/bignumber.js';

Has anyone else run into this problem? Thanks 😊

MikeMcl commented 4 years ago

It depends on the location of the library and the typescript options used, but try:

import BigNumber from "./bignumber.js/bignumber";
claytn commented 4 years ago

I've imported the library as an npm module, so that won't work in my case

MikeMcl commented 4 years ago
import BigNumber from "bignumber.js";

should work if the library is in a node_modules directory and the moduleResolution option is "Node".

Module resolution

claytn commented 4 years ago

moduleResolution is set to "node" in my tsconfig. The problem seems to be that no matter what import I go with its only importing the bignumber.d.ts file which only contains the type definitions and not the implementation.

claytn commented 4 years ago

I can get around this issue if I directly use require('bignumber.js'), but I don't get the typescript support with it obviously.

claytn commented 4 years ago

I didn't realize this library had it's types available in DefinitelyTyped. Once I installed the types as a dev dependency using the regular import seems to be working just fine.

yarn add -D @types/bignumber.js

// index.ts

import { BigNumber } from 'bignumber.js'

(new BigNumber(5)).plus(10).toString() // "15"

^ this works just fine now 😄

claytn commented 4 years ago

thanks for the help! I'll update the repo README soon with a Typescript specific section that includes these instructions 👍

MikeMcl commented 4 years ago

This library works just fine with Typescript as it is without the @types/bignumber.js, as can be verified by creating a simple test project.

When installed from npm the library already contains the type declarations, bignumber.d.ts, and the package.json correctly references them with "types": "bignumber.d.ts".

I do not know what the problem was with your project configuration but it is not an issue with this library.

I will add to the README presently to make it explicit that after a npm install the library can be imported using

import BigNumber from "bignumber.js";