Using a private field (#) as the typeof for an element

46 Views Asked by At

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

1

There are 1 best solutions below

0
adam.k On

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