Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript Question on subclass extension for different DP/RM/NE configs per subclass #219

Open
capttrousers opened this issue Jan 2, 2025 · 1 comment

Comments

@capttrousers
Copy link

capttrousers commented Jan 2, 2025

What's the recommended way to create Typescript types for sub types of Big with different configs?

I'd like to have unique types for USD vs BTC, mainly to easily identify the value's expected decimal/rounding behavior, that have different configs like so:

USD.DP = 2; // Maximum 2 decimal places for USD
USD.RM = Big.roundHalfUp; // Round to nearest neighbor, away from zero if equidistant
USD.NE = -3; // Exponential notation for values < 0.01

BTC.DP = 8; // Maximum 8 decimal places for BTC
BTC.RM = Big.roundDown; // Round towards zero
BTC.NE = -9; // Exponential notation for values < .00000001.

I've tried a few different methods but none seem to work, either theres type errors or the rounding / precision configs dont seem to take effect.

@capttrousers capttrousers changed the title Question on subclass / extension for different DP/RM/NE configs per subclass Question on subclass extension for different DP/RM/NE configs per subclass Jan 2, 2025
@capttrousers capttrousers changed the title Question on subclass extension for different DP/RM/NE configs per subclass Typescript Question on subclass extension for different DP/RM/NE configs per subclass Jan 2, 2025
@MikeMcl
Copy link
Owner

MikeMcl commented Jan 20, 2025

Apologies for the slow response.

I have not used TypeScript recently but in JavaScript it would just be:

const USD = Big();
USD.DP = 2;
USD.RM = USD.roundHalfUp;
USD.NE = -3;

const BTC = Big();
BTC.DP = 8;
BTC.RM = BTC.roundDown;
BTC.NE = -9;

console.log(USD(2).div(3).toString());  // 0.67
console.log(BTC(2).div(3).toString());  // 0.66666666

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants