binance-api-client icon indicating copy to clipboard operation
binance-api-client copied to clipboard

Type woes

Open gemmell opened this issue 5 years ago • 2 comments

Unfortunately for typescript, it's type system is an afterthought of Javascript. And binance mostly reports it's numbers using strings in the JSON. So everywhere that a type has been declared as number, and then assigned using something like:

 this._openingPrice = json[ 1 ];

(for example Candlestick), what you end up with is typescript thinking it's got a number, when actually it's a string, because the type system doesn't do the conversion (at least not that I am aware of). Now any time you do something like openingPrice < lastPrice it's actually doing a string comparison. It's like the worst of both worlds.

I think you need to go through and parseFloat everywhere that you expect to be getting a number out of json when binance sends you a string.

gemmell avatar Jul 19 '19 09:07 gemmell

Thanks a lot for reporting. Do you think you can submit a PR for this?

luzzif avatar Jul 22 '19 19:07 luzzif

When you see strings used for prices, it is often done for the reason of avoiding the floating point errors and giving the users a choice in what kind of arithmetics they want to use (some users might want to parse directly into BigRational)

ArtemGr avatar Jul 15 '20 16:07 ArtemGr