-
Notifications
You must be signed in to change notification settings - Fork 742
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
'BN' refers to a value, but is being used as a type here. Did you mean 'BN'? #315
Comments
I don't see Anyway, presumably you need to define the Maybe you can just use: function clone(n: typeof WholeNumber){ |
I tried that, it didn't seem to work. While this may not be the place to learn typescript, if others run into a similar issue at least they may find that the issue already exists. I can use BigNumber as a type no problem. And I imagine that WholeNumber is a class that extends it? Can I just extend BigNumber and pass it the precision to the super? That would be convenient |
Just wanted to say that |
Just using class WholeNumber extends BigNumber {} will declare the Bear in mind though that If class WholeNumber extends BigNumber {
type = WholeNumber;
} |
@shuckster Should I rename my title? In the documentation they set the clone to BN so I figured it would be a good way to try to adhere to the example they provided // Set DECIMAL_PLACES for the original BigNumber constructor
BigNumber.set({ DECIMAL_PLACES: 10 })
// Create another BigNumber constructor, optionally passing in a configuration object
BN = BigNumber.clone({ DECIMAL_PLACES: 5 })
x = new BigNumber(1)
y = new BN(1)
x.div(3) // '0.3333333333'
y.div(3) // '0.33333' So, looking at the source code I found
BigNumber.set(configObject); So I suppose I can extend BigNumber, then set the config immediately afterward. after installing // file = ./src/ExtendedNumber.ts
import BigNumber from "bignumber.js";
export class ExtendedNumber extends BigNumber {
type = ExtendedNumber;
}
// I don't understand the export system perfectly, expecially when it comes to tree shaking
// But I expect that any methods run on exported files are run before the value gets exported
// Just in case I run the config function underneath the class to see if it works as I hope
ExtendedNumber.config({
EXPONENTIAL_AT: 1e+9,
DECIMAL_PLACES: 1
}) // file = ./index.ts
import BigNumber from "bignumber.js";
import { ExtendedNumber } from "./ExtendedNumber";
var one = new ExtendedNumber(1);
var frac = one.dividedBy(3);
console.log("should return 0.3:", frac.toString());
ExtendedNumber.config({
EXPONENTIAL_AT: 1e+9,
DECIMAL_PLACES: 2
});
frac = one.dividedBy(3);
console.log("should return 0.33:", frac.toString());
delete ExtendedNumber.config
delete ExtendedNumber.set
try {
ExtendedNumber.config({
EXPONENTIAL_AT: 1e+9,
DECIMAL_PLACES: 3
})
throw new Error("shouldn't be able to config")
}catch(e){
console.log("can't config again")
}
try {
ExtendedNumber.set({
EXPONENTIAL_AT: 1e+9,
DECIMAL_PLACES: 3
})
throw new Error("shouldn't be able to set")
}catch(e){
console.log("can't set again")
}
BigNumber.config.call(ExtendedNumber, {
EXPONENTIAL_AT: 1e+9,
DECIMAL_PLACES: 3
})
frac = one.dividedBy(3);
console.log("should return 0.333:", frac.toString());
console.log("is one a ExtendedNumber?", one instanceof ExtendedNumber);
console.log("is one a BigNumber?", one instanceof BigNumber);
console.log("is frac a ExtendedNumber?", frac instanceof ExtendedNumber);
console.log("is frac a BigNumber?", frac instanceof BigNumber); I did the initial config in the Something interesting is that
I don't know how to explain it since technically one is a But we can consider this issue closed since I got what I wanted. Btw @shuckster, Thanks for showing me However, I don't think it's a perfect fit for my application. For the library I've been working on, I want to give the users the option to "simplify a result" as well as export it to json which is meant to keep all the intricacies of the equation intact. This may mean division, irrational numbers or both which means decimals will be important. As a result, it's nice to see |
@MikeMcl Sorry about that, didn't mean to leave you hanging. So, While its all well and good extending the library, I wanted to set the config and the example way created a class that I couldn't use as a type. While your example was a good start, the config file still was not getting set in the constructor or anything. After a bit of research, I found i can just run the |
Just in case anyone is interested, I can use const two = new ExtendedNumber(2);
const three = new BigNumber(3);
function runLog(arg: ExtendedNumber){
console.log("running in a function:", arg);
}
runLog(two);
runLog(three);
Pretty much what I was looking for note: like @MikeMcl mentioned, you need to add the type parameter to your class definition. Otherwise Typescript will allow you to use big numbers as extended numbers which I don't think is desirable |
Apologies! I did not realise this was copy-pasted from the README. I remember that BN.js was being used alongside BigNumber seemingly unknowingly in those other threads, and just thought that might also be your own case. Pay no attention to the man behind the curtain. 👀 |
Was just running some more tests and ran into another issue. It seems as though config function is updating the base BigNumber class instead of the child class. Looking at the sourcecode I don't even see where the class is getting mutated. So I have no idea how config really works. Am I on the wrong branch? There also seems to be a typescript bug or maybe it's expected behavior but I'm going to create an issue over there as well. in the below example, here is the log
here is the code import BigNumber from "bignumber.js";
BigNumber.config({
DECIMAL_PLACES: 5,
})
class WholeNumber extends BigNumber {}
WholeNumber.config({
DECIMAL_PLACES: 0
})
class DecNumber extends BigNumber {}
DecNumber.config({
DECIMAL_PLACES: 3
})
const wholeOne = new WholeNumber(1);
console.log("wN is Whole?", wholeOne instanceof WholeNumber); // true
console.log("wN is Dec?", wholeOne instanceof DecNumber); // false
console.log("wN is Big?", wholeOne instanceof BigNumber); // true
const decOne = new DecNumber(1);
console.log("dN is Whole?", decOne instanceof WholeNumber); // false
console.log("dN is Dec?", decOne instanceof DecNumber); // true
console.log("dN is Big?", decOne instanceof BigNumber); // true
const bigOne = new BigNumber(1);
console.log("bN is Whole?", bigOne instanceof WholeNumber); // false
console.log("bN is Dec?", bigOne instanceof DecNumber); // false
console.log("bN is Big?", bigOne instanceof BigNumber); // true
function divideByThree(n: WholeNumber): WholeNumber {
return n.div(3);
}
console.log("wN div 3:", divideByThree(wholeOne).toString()); // 0.333
console.log("dN div 3:", divideByThree(decOne).toString()) // 0.333
console.log("bN div 3:", divideByThree(bigOne).toString()); // 0.333 |
Use class WholeNumber extends BigNumber.clone() {}
class DecNumber extends BigNumber.clone() {}
|
@shuckster Solid. |
I am curious, is that expected behaviour though? Shouldn't the setting the config of the child class not affect the parent or siblings? |
I can't speak for the author, but with a method like I've certainly used |
Reproduction
Results in
My Environment
My Editor
The text was updated successfully, but these errors were encountered: