Generated JS files use number for int64 fields, causing wrong values to be sent due to precision loss.
- Dominant language
- JavaScript
- Stars
- 9.3k
- Forks
- 802
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 5
Description
When generating a JS file for a proto file using the `protoc-gen-grpc-web` plugin, 64 bit integers are treated as the JavaScript `number` type. As numbers in JS are limited precision floating point, they can not support large 64 bit integers without precision loss. As such, it is not possible to send a number such as `9024037547368883040` over GRPC.
Example (named `foo.proto`):
```
syntax = "proto3";
message foo {
int64 bar = 1;
}
```
Compiled via:
`protoc -I=. foo.proto --js_out=import_style=commonjs:. --grpc-web_out=import_style=typescript,mode=grpcweb:.`
The generated output for the TypeScript bindings includes the following. Attempting to pass in the original number will give a Typescript error due to loss of precision.
```
getBar(): number;
setBar(value: number): foo;
```
If the type system is circumvented, such as by passing a string cast to `any`, the remote server appears to receive the wrong value due to improper loss of precision (`9024037547368883200` instead of `9024037547368883040`).
I haven't been able to find any workarounds to use to avoid this issue yet.
Contributor guide
Assessment
This issue has not been assessed yet.