Skip to content

Commit

Permalink
change return type from "BigNumber" to "this"
Browse files Browse the repository at this point in the history
The hard-coded return type "BigNumber" prevents subclasses from working correctly with TypeScript types when super class methods are called.

See MikeMcl#368, this PR closes MikeMcl#368
  • Loading branch information
ozum committed Mar 12, 2024
1 parent d795adb commit e555a49
Showing 1 changed file with 33 additions and 38 deletions.
71 changes: 33 additions & 38 deletions bignumber.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@
export default BigNumber;

export namespace BigNumber {

/** See `BigNumber.config` (alias `BigNumber.set`) and `BigNumber.clone`. */
interface Config {

/**
* An integer, 0 to 1e+9. Default value: 20.
*
Expand Down Expand Up @@ -281,7 +279,6 @@ export namespace BigNumber {

/** See `FORMAT` and `toFormat`. */
interface Format {

/** The string to prepend. */
prefix?: string;

Expand All @@ -308,7 +305,6 @@ export namespace BigNumber {
}

interface Instance {

/** The coefficient of the value of this BigNumber, an array of base 1e14 integer numbers, or null. */
readonly c: number[] | null;

Expand All @@ -328,7 +324,6 @@ export namespace BigNumber {
}

export declare class BigNumber implements BigNumber.Instance {

/** Used internally to identify a BigNumber instance. */
private readonly _isBigNumber: true;

Expand Down Expand Up @@ -450,7 +445,7 @@ export declare class BigNumber implements BigNumber.Instance {
* x.absoluteValue() // '0.8'
* ```
*/
absoluteValue(): BigNumber;
absoluteValue(): this;

/**
* Returns a BigNumber whose value is the absolute value, i.e. the magnitude, of the value of this
Expand All @@ -463,7 +458,7 @@ export declare class BigNumber implements BigNumber.Instance {
* x.abs() // '0.8'
* ```
*/
abs(): BigNumber;
abs(): this;

/**
* Returns | |
Expand Down Expand Up @@ -518,7 +513,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param [roundingMode] Rounding mode, integer, 0 to 8.
*/
decimalPlaces(): number | null;
decimalPlaces(decimalPlaces: number, roundingMode?: BigNumber.RoundingMode): BigNumber;
decimalPlaces(decimalPlaces: number, roundingMode?: BigNumber.RoundingMode): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber rounded by rounding mode
Expand Down Expand Up @@ -551,7 +546,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param [roundingMode] Rounding mode, integer, 0 to 8.
*/
dp(): number | null;
dp(decimalPlaces: number, roundingMode?: BigNumber.RoundingMode): BigNumber;
dp(decimalPlaces: number, roundingMode?: BigNumber.RoundingMode): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber divided by `n`, rounded
Expand All @@ -568,7 +563,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n A numeric value.
* @param [base] The base of n.
*/
dividedBy(n: BigNumber.Value, base?: number): BigNumber;
dividedBy(n: BigNumber.Value, base?: number): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber divided by `n`, rounded
Expand All @@ -585,7 +580,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n A numeric value.
* @param [base] The base of n.
*/
div(n: BigNumber.Value, base?: number): BigNumber;
div(n: BigNumber.Value, base?: number): this;

/**
* Returns a BigNumber whose value is the integer part of dividing the value of this BigNumber by
Expand All @@ -602,7 +597,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n A numeric value.
* @param [base] The base of n.
*/
dividedToIntegerBy(n: BigNumber.Value, base?: number): BigNumber;
dividedToIntegerBy(n: BigNumber.Value, base?: number): this;

/**
* Returns a BigNumber whose value is the integer part of dividing the value of this BigNumber by
Expand All @@ -619,7 +614,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n A numeric value.
* @param [base] The base of n.
*/
idiv(n: BigNumber.Value, base?: number): BigNumber;
idiv(n: BigNumber.Value, base?: number): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber exponentiated by `n`, i.e.
Expand Down Expand Up @@ -652,8 +647,8 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n The exponent, an integer.
* @param [m] The modulus.
*/
exponentiatedBy(n: BigNumber.Value, m?: BigNumber.Value): BigNumber;
exponentiatedBy(n: number, m?: BigNumber.Value): BigNumber;
exponentiatedBy(n: BigNumber.Value, m?: BigNumber.Value): this;
exponentiatedBy(n: number, m?: BigNumber.Value): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber exponentiated by `n`, i.e.
Expand Down Expand Up @@ -686,8 +681,8 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n The exponent, an integer.
* @param [m] The modulus.
*/
pow(n: BigNumber.Value, m?: BigNumber.Value): BigNumber;
pow(n: number, m?: BigNumber.Value): BigNumber;
pow(n: BigNumber.Value, m?: BigNumber.Value): this;
pow(n: number, m?: BigNumber.Value): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber rounded to an integer using
Expand All @@ -708,7 +703,7 @@ export declare class BigNumber implements BigNumber.Instance {
*
* @param {BigNumber.RoundingMode} [rm] The roundng mode, an integer, 0 to 8.
*/
integerValue(rm?: BigNumber.RoundingMode): BigNumber;
integerValue(rm?: BigNumber.RoundingMode): this;

/**
* Returns `true` if the value of this BigNumber is equal to the value of `n`, otherwise returns
Expand Down Expand Up @@ -977,7 +972,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n A numeric value.
* @param [base] The base of n.
*/
minus(n: BigNumber.Value, base?: number): BigNumber;
minus(n: BigNumber.Value, base?: number): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber modulo `n`, i.e. the integer
Expand All @@ -1003,7 +998,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n A numeric value.
* @param [base] The base of n.
*/
modulo(n: BigNumber.Value, base?: number): BigNumber;
modulo(n: BigNumber.Value, base?: number): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber modulo `n`, i.e. the integer
Expand All @@ -1029,7 +1024,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n A numeric value.
* @param [base] The base of n.
*/
mod(n: BigNumber.Value, base?: number): BigNumber;
mod(n: BigNumber.Value, base?: number): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber multiplied by `n`.
Expand All @@ -1047,7 +1042,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n A numeric value.
* @param [base] The base of n.
*/
multipliedBy(n: BigNumber.Value, base?: number): BigNumber;
multipliedBy(n: BigNumber.Value, base?: number): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber multiplied by `n`.
Expand All @@ -1065,7 +1060,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n A numeric value.
* @param [base] The base of n.
*/
times(n: BigNumber.Value, base?: number): BigNumber;
times(n: BigNumber.Value, base?: number): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber negated, i.e. multiplied by -1.
Expand All @@ -1077,7 +1072,7 @@ export declare class BigNumber implements BigNumber.Instance {
* y.negated() // '1.3'
* ```
*/
negated(): BigNumber;
negated(): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber plus `n`.
Expand All @@ -1095,7 +1090,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param n A numeric value.
* @param [base] The base of n.
*/
plus(n: BigNumber.Value, base?: number): BigNumber;
plus(n: BigNumber.Value, base?: number): this;

/**
* Returns the number of significant digits of the value of this BigNumber, or `null` if the value
Expand Down Expand Up @@ -1138,7 +1133,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param significantDigits Significant digits, integer, 1 to 1e+9.
* @param [roundingMode] Rounding mode, integer, 0 to 8.
*/
precision(significantDigits: number, roundingMode?: BigNumber.RoundingMode): BigNumber;
precision(significantDigits: number, roundingMode?: BigNumber.RoundingMode): this;

/**
* Returns the number of significant digits of the value of this BigNumber,
Expand Down Expand Up @@ -1182,7 +1177,7 @@ export declare class BigNumber implements BigNumber.Instance {
* @param significantDigits Significant digits, integer, 1 to 1e+9.
* @param [roundingMode] Rounding mode, integer, 0 to 8.
*/
sd(significantDigits: number, roundingMode?: BigNumber.RoundingMode): BigNumber;
sd(significantDigits: number, roundingMode?: BigNumber.RoundingMode): this;

/**
* Returns a BigNumber whose value is the value of this BigNumber shifted by `n` places.
Expand All @@ -1202,7 +1197,7 @@ export declare class BigNumber implements BigNumber.Instance {
*
* @param n The shift value, integer, -9007199254740991 to 9007199254740991.
*/
shiftedBy(n: number): BigNumber;
shiftedBy(n: number): this;

/**
* Returns a BigNumber whose value is the square root of the value of this BigNumber, rounded
Expand All @@ -1218,7 +1213,7 @@ export declare class BigNumber implements BigNumber.Instance {
* y.squareRoot() // '1.73205080756887729353'
* ```
*/
squareRoot(): BigNumber;
squareRoot(): this;

/**
* Returns a BigNumber whose value is the square root of the value of this BigNumber, rounded
Expand All @@ -1234,7 +1229,7 @@ export declare class BigNumber implements BigNumber.Instance {
* y.sqrt() // '1.73205080756887729353'
* ```
*/
sqrt(): BigNumber;
sqrt(): this;

/**
* Returns a string representing the value of this BigNumber in exponential notation rounded using
Expand Down Expand Up @@ -1398,7 +1393,7 @@ export declare class BigNumber implements BigNumber.Instance {
*
* @param [max_denominator] The maximum denominator, integer > 0, or Infinity.
*/
toFraction(max_denominator?: BigNumber.Value): [BigNumber, BigNumber];
toFraction(max_denominator?: BigNumber.Value): [this, this];

/** As `valueOf`. */
toJSON(): string;
Expand Down Expand Up @@ -1688,7 +1683,7 @@ export declare class BigNumber implements BigNumber.Instance {
*
* @param n A numeric value.
*/
static maximum(...n: BigNumber.Value[]): BigNumber;
static maximum(...n: BigNumber.Value[]): this;

/**
* Returns a BigNumber whose value is the maximum of the arguments.
Expand All @@ -1705,7 +1700,7 @@ export declare class BigNumber implements BigNumber.Instance {
*
* @param n A numeric value.
*/
static max(...n: BigNumber.Value[]): BigNumber;
static max(...n: BigNumber.Value[]): this;

/**
* Returns a BigNumber whose value is the minimum of the arguments.
Expand All @@ -1722,7 +1717,7 @@ export declare class BigNumber implements BigNumber.Instance {
*
* @param n A numeric value.
*/
static minimum(...n: BigNumber.Value[]): BigNumber;
static minimum(...n: BigNumber.Value[]): this;

/**
* Returns a BigNumber whose value is the minimum of the arguments.
Expand All @@ -1739,7 +1734,7 @@ export declare class BigNumber implements BigNumber.Instance {
*
* @param n A numeric value.
*/
static min(...n: BigNumber.Value[]): BigNumber;
static min(...n: BigNumber.Value[]): this;

/**
* Returns a new BigNumber with a pseudo-random value equal to or greater than 0 and less than 1.
Expand Down Expand Up @@ -1773,7 +1768,7 @@ export declare class BigNumber implements BigNumber.Instance {
*
* @param [decimalPlaces] Decimal places, integer, 0 to 1e+9.
*/
static random(decimalPlaces?: number): BigNumber;
static random(decimalPlaces?: number): this;

/**
* Returns a BigNumber whose value is the sum of the arguments.
Expand All @@ -1790,7 +1785,7 @@ export declare class BigNumber implements BigNumber.Instance {
*
* @param n A numeric value.
*/
static sum(...n: BigNumber.Value[]): BigNumber;
static sum(...n: BigNumber.Value[]): this;

/**
* Configures the settings that apply to this BigNumber constructor.
Expand Down Expand Up @@ -1828,4 +1823,4 @@ export declare class BigNumber implements BigNumber.Instance {
static set(object?: BigNumber.Config): BigNumber.Config;
}

export function BigNumber(n: BigNumber.Value, base?: number): BigNumber;
export function BigNumber(n: BigNumber.Value, base?: number): this;

0 comments on commit e555a49

Please sign in to comment.