json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

Can not interfere enum names when starting with a number

Open n-buck opened this issue 8 months ago • 2 comments

JSON enums allow enum-values to start with numbers. Interfering the keys from the enum-values will result in an enum like i.e.

enum foo {
   0bar: "0bar",
   1baz: "1baz",
}

which is invalid in typescript.

I think the best way to handle this is to escape the keys, similar to special-characters:

enum foo {
   "0bar": "0bar",
   "1baz": "1baz",
}

I opened a PR for this Issue: https://github.com/bcherny/json-schema-to-typescript/pull/656

n-buck avatar Apr 03 '25 08:04 n-buck

@claude fix this

bcherny avatar May 19 '25 16:05 bcherny

Claude finished @bcherny's task —— View jobclaude/issue-657-20250519_164719Create PR ➔


I'll fix the issue with enum names starting with numbers. Let me analyze and implement a solution.

Tasks

  • [x] Analyze the issue with enum names starting with numbers
  • [x] Examine the codebase to locate relevant enum generation code
  • [x] Look at the existing PR (#656) mentioned in the issue
  • [x] Implement a solution to escape enum keys starting with numbers
  • [x] Test the solution with appropriate test cases
  • [x] Prepare PR for submission

I've fixed the issue and committed the changes. The problem was in the generateStandaloneEnum function in generator.ts where enum keys starting with numbers weren't being properly quoted. I added a new function startsWithNumber to check if a string starts with a number, and modified the enum key generation logic to properly quote these keys.

I also created a new test case in test/e2e/enum.numeric.ts to verify the fix works with various enum scenarios involving numeric keys.

claude[bot] avatar May 19 '25 16:05 claude[bot]