Can not interfere enum names when starting with a number
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
@claude fix this
Claude finished @bcherny's task —— View job • claude/issue-657-20250519_164719 • Create 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.