feat(generator): add force_int64_string option to prevent int64 preci… #1510
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
feat(generator): add force_int64_string option to prevent int64 precision loss
Fixes #1229
Problem
Large 64-bit integers lose precision when mapped to JavaScript's
number
type in gRPC-Web generated TypeScript definitions. This is a well-known issue affecting JavaScript/TypeScript applications that work with large integers.Example:
Generated TypeScript (current behavior):
Problem demonstration:
Solution
This PR adds a new CLI option
force_int64_string=True
that forces allint64
/uint64
fields to be generated asstring
type, preserving full precision.Usage:
Generated TypeScript (with fix):
Implementation Details
force_int64_string=True
[jstype = JS_STRING]
field-level option still worksint64
,uint64
,sint64
,fixed64
,sfixed64
)Changes Made
JSElementType
function to check bothjstype
option and CLI flagforce_int64_string
parameterGeneratorOptions
support for parsing the new CLI optionTesting
✅ Default behavior: Normal fields →
number
,[jstype = JS_STRING]
→string
✅ force_int64_string=True: All int64 fields →
string
✅ Backward compatibility: Existing behavior preserved
Migration Guide
For new projects:
# Use the new option to prevent precision loss protoc --grpc-web_out=import_style=commonjs+dts,mode=grpcwebtext,force_int64_string=True:. your_proto.proto
For existing projects:
[jstype = JS_STRING]
for specific fieldsforce_int64_string=True
for global control