0

Check this code

interface PriceFormatOptions {
  unit: string;
}
export default class PriceHelper {
  /**
   * Adds unit and separates thousands
   */
  static format(
    price: Parameters<typeof PriceHelper.#separateThousands>[0],
    options: PriceFormatOptions = {} as PriceFormatOptions
  ) {
    let unit = options.unit || "تومان";

    const separatedPrice = this.#separateThousands(price);
    if (unit) unit = ` ${unit}`;

    return separatedPrice + unit;
  }

  /**
   * Converts numeral prices to persian words
   */
  static toWords() {}

  static #separateThousands(price: string | number) {
    return String(price || 0).replace(/\B(?=(\d{3})+(?!\d))/g, ",");
  }
}

when I wite separateThousands like this

static separateThousands(price: string | number) {
return String(price || 0).replace(/\B(?=(\d{3})+(?!\d))/g, ",");

}

and use it like this

price: Parameters<typeof PriceHelper.separateThousands>[0],

everything is fine but when I use it as private field (with #) like this

price: Parameters<typeof PriceHelper.#separateThousands>[0],

typescript complains with this error

ESLint: Parsing error: Identifier expected.

I don't have any idea how can I fix that

Ali Ehyaie
  • 1,116
  • 3
  • 13
  • 27
  • Given that [ms/TS#47595](https://github.com/microsoft/TypeScript/issues/47595) hasn't been resolved, you'll need to work around it. One approach is to define the type elsewhere, such as in `format` or in a separate type alias, and then reuse it inside `#separateThousands`, as shown [in this playground link](https://tsplay.dev/mqxyZN). Does that fully address the question? If so I'll write up an answer explaining; if not, what am I missing? – jcalz May 21 '23 at 20:46

1 Answers1

0

I think eslint, just doesn't understand the #. If you don't need the shorthand, you could type seperateThousands as private static seperateThousands

adam.k
  • 622
  • 3
  • 15