Skip to content

Commit

Permalink
feat: bigIntLiteral option for using BigInt literals (#1063)
Browse files Browse the repository at this point in the history
Fixes #928. Since I finally got around to contributing, I figured I'd
try this old issue out too, and it was surprisingly simple. Entirely up
to you whether to trash it or not - it's not a very consequential option
and the size optimization should probably be handled by
terser/terser#1535 anyway, and it could cause
some breakage for users.

Supercedes #932.
  • Loading branch information
bhollis authored Jun 15, 2024
1 parent db1b1a2 commit b89fbcb
Show file tree
Hide file tree
Showing 22 changed files with 1,710 additions and 63 deletions.
4 changes: 3 additions & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,8 @@ Generated code will be placed in the Gradle build directory.

- With `--ts_proto_opt=comments=false`, comments won't be copied from the proto files to the generated code.

- With `--ts_proto_opt=bigIntLiteral=false`, the generated code will use `BigInt("0")` instead of `0n` for BigInt literals. BigInt literals aren't supported by TypeScript when the "target" compiler option set to something older than "ES2020".

- With `--ts_proto_opt=useNullAsOptional=true`, `undefined` values will be converted to `null`, and if you use `optional` label in your `.proto` file, the field will have `undefined` type as well. for example:

```protobuf
Expand Down Expand Up @@ -603,7 +605,7 @@ export interface User {
}
```

- With `--ts_proto_opt=noDefaultsForOptionals=true`, `undefined` primitive values will not be defaulted as per the protobuf spec. Additionally unlike the standard behavior, when a field is set to it's standard default value, it *will* be encoded allowing it to be sent over the wire and distinguished from undefined values. For example if a message does not set a boolean value, ordinarily this would be defaulted to `false` which is different to it being undefined.
- With `--ts_proto_opt=noDefaultsForOptionals=true`, `undefined` primitive values will not be defaulted as per the protobuf spec. Additionally unlike the standard behavior, when a field is set to it's standard default value, it *will* be encoded allowing it to be sent over the wire and distinguished from undefined values. For example if a message does not set a boolean value, ordinarily this would be defaulted to `false` which is different to it being undefined.

This option allows the library to act in a compatible way with the [Wire implementation](https://square.github.io/wire/) maintained and used by Square/Block. Note: this option should only be used in combination with other client/server code generated using Wire or ts-proto with this option enabled.

Expand Down
2 changes: 1 addition & 1 deletion integration/extension-import/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2018",
"target": "es2020",
"lib": ["es2018"],
"module": "commonjs",
"strict": true,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions integration/map-bigint-optional/test.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions integration/pbjs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ if match "simple-long-bigint"; then
yarn run pbts --no-comments -o integration/simple-long-bigint/pbjs.d.ts integration/simple-long-bigint/pbjs.js
fi

if match "simple-long-bigint-noliteral"; then
yarn run pbjs --force-message --force-long -t static-module -o integration/simple-long-bigint-noliteral/pbjs.js integration/simple-long-bigint-noliteral/simple.proto
yarn run pbts --no-comments -o integration/simple-long-bigint-noliteral/pbjs.d.ts integration/simple-long-bigint-noliteral/pbjs.js
fi

if match "vector-tile"; then
yarn run pbjs --force-message --force-number -t static-module -o integration/vector-tile/pbjs.js integration/vector-tile/vector_tile.proto
yarn run pbts --no-comments -o integration/vector-tile/pbjs.d.ts integration/vector-tile/pbjs.js
Expand Down
2 changes: 1 addition & 1 deletion integration/simple-esmodule-interop/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es2018",
"target": "es2020",
"lib": ["es2018"],
"module": "commonjs",
"strict": true,
Expand Down
217 changes: 217 additions & 0 deletions integration/simple-long-bigint-noliteral/google/protobuf/timestamp.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b89fbcb

Please sign in to comment.