scala-ts icon indicating copy to clipboard operation
scala-ts copied to clipboard

Generated TypeScript identifiers starting with digits (e.g., 2FA) are invalid

Open m3di opened this issue 1 year ago • 1 comments

Description
The scala-ts generator produces invalid TypeScript code when Scala case objects have names starting with digits (e.g., `2FA`). While such names are valid in Scala (due to backtick escaping), TypeScript does not allow identifiers to start with a digit, causing a syntax error in the generated output.


To Reproduce
Steps to reproduce the behavior:

  1. Define an enum in Scala with a case object named using a digit prefix, such as case object `2FA` extends CredentialsType
  2. Run scala-ts to generate TypeScript bindings
  3. Inspect the generated .ts file
  4. See invalid TypeScript class/variable name like export class 2FA { ... }, which causes a syntax error

Expected behavior
The generator should handle invalid TypeScript identifiers gracefully by either:

  • Renaming them to valid alternatives (e.g., TwoFA, _2FA) and preserving the original string value for serialization if necessary,
  • Or allowing customization of the TypeScript identifier mapping (e.g., via annotations or configuration) to generate valid code while keeping the semantic string value.

Additional context
This issue arises from a difference in naming rules between Scala and TypeScript. In Scala, backticks allow identifiers like `2FA`, but these translate directly into invalid TypeScript class names. A common use case is when enum values are serialized as strings (e.g., "2FA") in JSON, and developers want to preserve this semantic meaning while keeping the generated TypeScript valid and usable.

Image Image

m3di avatar Apr 16 '25 11:04 m3di

Hi, will have a look. Meanwhile (and anyway) I would suggest to not to use names that require to be backticked, but rather string representation mechanism (like custom entryName with Enumeratum).

cchantep avatar Apr 16 '25 13:04 cchantep