Typescript 有可能有一个班级扩展号吗?

Typescript 有可能有一个班级扩展号吗?,typescript,Typescript,我知道这可能是否定的,但在typescript中是否有某种方法让类从数字继承?我有很多例子,其中类是一个数值和一堆方法。所以理论上这个类可以是一个数字加上那些方法 有办法做到这一点吗 谢谢-dave简短的回答是否定的。长答案,需要让我发布堆栈溢出的答案,也是否定的。简短的回答是否定的。长答案,需要让我发布堆栈溢出的答案,也是否定的。简短的回答是否定的。长答案,需要让我发布堆栈溢出的答案,也是否定的。简短的回答是否定的。让我发布堆栈溢出的答案所需的长答案也是否定的。今天,这是可能的 /** *

我知道这可能是否定的,但在typescript中是否有某种方法让类从数字继承?我有很多例子,其中类是一个数值和一堆方法。所以理论上这个类可以是一个数字加上那些方法

有办法做到这一点吗


谢谢-dave

简短的回答是否定的。长答案,需要让我发布堆栈溢出的答案,也是否定的。

简短的回答是否定的。长答案,需要让我发布堆栈溢出的答案,也是否定的。

简短的回答是否定的。长答案,需要让我发布堆栈溢出的答案,也是否定的。

简短的回答是否定的。让我发布堆栈溢出的答案所需的长答案也是否定的。

今天,这是可能的

/**
 * Decimal class
 *
 * Represents a decimal number with a fixed precision which can be defined in the constructor.
 *
 * @export
 * @class Decimal
 * @extends {Number}
 * @implements {Number}
 */
export class Decimal extends Number implements Number {
  public precision: number;

  /**
   * Creates an instance of Decimal.
   *
   * @param {(number | string)} value
   *
   * @memberOf Decimal
   */
  constructor(value: number | string, precision: number = 2) {
    if (typeof value === 'string') {
      value = parseFloat(value);
    }

    if (typeof value !== 'number' || isNaN(value)) {
      throw new Error('Decimal constructor requires a number or the string representation of a number.');
    }

    super(parseFloat((value || 0).toFixed(2)));
    this.precision = precision;
  }

  /**
   * Returns the value of this instance as a number
   *
   * @returns {number}
   *
   * @memberOf Decimal
   */
  public valueOf(): number {
    return parseFloat(this.toFixed(2));
  }

  /**
   * Returns the string representation for this instance.
   *
   * @returns {string}
   *
   * @memberOf Decimal
   */
  public toString(): string {
    return this.toFixed(2);
  }
}
今天,这是可能的

/**
 * Decimal class
 *
 * Represents a decimal number with a fixed precision which can be defined in the constructor.
 *
 * @export
 * @class Decimal
 * @extends {Number}
 * @implements {Number}
 */
export class Decimal extends Number implements Number {
  public precision: number;

  /**
   * Creates an instance of Decimal.
   *
   * @param {(number | string)} value
   *
   * @memberOf Decimal
   */
  constructor(value: number | string, precision: number = 2) {
    if (typeof value === 'string') {
      value = parseFloat(value);
    }

    if (typeof value !== 'number' || isNaN(value)) {
      throw new Error('Decimal constructor requires a number or the string representation of a number.');
    }

    super(parseFloat((value || 0).toFixed(2)));
    this.precision = precision;
  }

  /**
   * Returns the value of this instance as a number
   *
   * @returns {number}
   *
   * @memberOf Decimal
   */
  public valueOf(): number {
    return parseFloat(this.toFixed(2));
  }

  /**
   * Returns the string representation for this instance.
   *
   * @returns {string}
   *
   * @memberOf Decimal
   */
  public toString(): string {
    return this.toFixed(2);
  }
}
今天,这是可能的

/**
 * Decimal class
 *
 * Represents a decimal number with a fixed precision which can be defined in the constructor.
 *
 * @export
 * @class Decimal
 * @extends {Number}
 * @implements {Number}
 */
export class Decimal extends Number implements Number {
  public precision: number;

  /**
   * Creates an instance of Decimal.
   *
   * @param {(number | string)} value
   *
   * @memberOf Decimal
   */
  constructor(value: number | string, precision: number = 2) {
    if (typeof value === 'string') {
      value = parseFloat(value);
    }

    if (typeof value !== 'number' || isNaN(value)) {
      throw new Error('Decimal constructor requires a number or the string representation of a number.');
    }

    super(parseFloat((value || 0).toFixed(2)));
    this.precision = precision;
  }

  /**
   * Returns the value of this instance as a number
   *
   * @returns {number}
   *
   * @memberOf Decimal
   */
  public valueOf(): number {
    return parseFloat(this.toFixed(2));
  }

  /**
   * Returns the string representation for this instance.
   *
   * @returns {string}
   *
   * @memberOf Decimal
   */
  public toString(): string {
    return this.toFixed(2);
  }
}
今天,这是可能的

/**
 * Decimal class
 *
 * Represents a decimal number with a fixed precision which can be defined in the constructor.
 *
 * @export
 * @class Decimal
 * @extends {Number}
 * @implements {Number}
 */
export class Decimal extends Number implements Number {
  public precision: number;

  /**
   * Creates an instance of Decimal.
   *
   * @param {(number | string)} value
   *
   * @memberOf Decimal
   */
  constructor(value: number | string, precision: number = 2) {
    if (typeof value === 'string') {
      value = parseFloat(value);
    }

    if (typeof value !== 'number' || isNaN(value)) {
      throw new Error('Decimal constructor requires a number or the string representation of a number.');
    }

    super(parseFloat((value || 0).toFixed(2)));
    this.precision = precision;
  }

  /**
   * Returns the value of this instance as a number
   *
   * @returns {number}
   *
   * @memberOf Decimal
   */
  public valueOf(): number {
    return parseFloat(this.toFixed(2));
  }

  /**
   * Returns the string representation for this instance.
   *
   * @returns {string}
   *
   * @memberOf Decimal
   */
  public toString(): string {
    return this.toFixed(2);
  }
}

这让我发笑(长答案部分)。检查下面的我的答案让我发笑(长答案部分)。检查下面的我的答案让我发笑(长答案部分)。检查下面的我的答案让我发笑(长答案部分)。检查下面的我的答案聪明、简洁的实现在这里:聪明、简洁的实现在这里:,这里的简明实现:在TS中,我可以
const A:Number=2
。我相信我知道答案,但我想验证一下,是否有任何黑客可以像
const b:Decimal=3
那样使用上述十进制数?在TS中,我可以
const a:Number=2
。我相信我知道答案,但我想验证一下,是否有任何黑客可以像
const b:Decimal=3
那样使用上述十进制数?在TS中,我可以
const a:Number=2
。我相信我知道答案,但我想验证一下,是否有任何黑客可以像
const b:Decimal=3
那样使用上述十进制数?在TS中,我可以
const a:Number=2
。我相信我知道答案,但我想验证一下,是否有任何黑客会允许使用上述十进制数,如
const b:Decimal=3