ts-proto 2.16.0 → 2.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.markdown CHANGED
@@ -372,6 +372,8 @@ Generated code will be placed in the Gradle build directory.
372
372
 
373
373
  - With `--ts_proto_opt=exportCommonSymbols=false`, utility types like `DeepPartial` and `protobufPackage` won't be `export`d.
374
374
 
375
+ Each generated file also exports `protobufFileName`, the path of the `.proto` file it was generated from (for example `export const protobufFileName = "company/foo.proto";`), next to `protobufPackage`; neither is exported with `exportCommonSymbols=false`.
376
+
375
377
  This should make it possible to use create barrel imports of the generated output, i.e. `import * from ./foo` and `import * from ./bar`.
376
378
 
377
379
  Note that if you have the same message name used in multiple `*.proto` files, you will still get import conflicts.
@@ -569,6 +571,8 @@ With `--ts_proto_opt=protoJsonFormat=false`, strict proto3 compliance is disable
569
571
 
570
572
  The default behavior is `useMapType=false`, which makes it generate the code for protobuf `map<key_type, value_type` with the key-value pair like `{[key: key_type]: value_type}`.
571
573
 
574
+ - With `--ts_proto_opt=useUnknownForValue=true`, `google.protobuf.Value` fields are typed as `unknown` instead of `any`, and so are the items of `google.protobuf.ListValue` and the values of `google.protobuf.Struct`. This makes TypeScript ask you to narrow the value before using it.
575
+
572
576
  - With `--ts_proto_opt=useReadonlyTypes=true`, the generated types will be declared as immutable using typescript's `readonly` modifier.
573
577
 
574
578
  - With `--ts_proto_opt=useSnakeTypeName=false` will remove snake casing from types.
package/build/src/main.js CHANGED
@@ -49,6 +49,8 @@ function generateFile(ctx, fileDesc) {
49
49
  // Indicate this file's source protobuf package for reflective use with google.protobuf.Any
50
50
  if (options.exportCommonSymbols) {
51
51
  chunks.push((0, ts_poet_1.code) `export const protobufPackage = '${fileDesc.package}';`);
52
+ // The source .proto path, e.g. for NestJS's `protoPath` or @grpc/proto-loader
53
+ chunks.push((0, ts_poet_1.code) `export const protobufFileName = '${fileDesc.name}';`);
52
54
  }
53
55
  // Syntax, unlike most fields, is not repeated and thus does not use an index
54
56
  const sourceInfo = sourceInfo_1.default.fromDescriptor(fileDesc);
@@ -506,6 +508,9 @@ function makeDeepPartial(options, longs) {
506
508
  const maybeLong = options.forceLong === options_1.LongOption.LONG ? (0, ts_poet_1.code) ` : T extends ${longs.Long} ? string | number | Long ` : "";
507
509
  // Allow passing bigints as numbers or strings; fromPartial coerces them via BigInt()
508
510
  const maybeBigInt = options.forceLong === options_1.LongOption.BIGINT ? (0, ts_poet_1.code) `T extends bigint ? string | number | bigint : ` : "";
511
+ // Keep `unknown` (useUnknownForValue) as is; mapping over it would turn it into `{}`
512
+ const maybeUnknownT = options.useUnknownForValue ? "unknown extends T ? T : " : "";
513
+ const maybeUnknownP = options.useUnknownForValue ? "unknown extends P ? P : " : "";
509
514
  const optionalBuiltins = [];
510
515
  if (options.forceLong === options_1.LongOption.BIGINT) {
511
516
  optionalBuiltins.push("bigint");
@@ -518,7 +523,7 @@ function makeDeepPartial(options, longs) {
518
523
  const maybeExcludeType = (0, options_1.addTypeToMessages)(options) ? `| '$type'` : "";
519
524
  const Exact = (0, ts_poet_1.conditionalOutput)("Exact", (0, ts_poet_1.code) `
520
525
  type KeysOfUnion<T> = T extends T ? keyof T : never;
521
- ${maybeExport} type Exact<P, I extends P> = P extends ${Builtin}
526
+ ${maybeExport} type Exact<P, I extends P> = ${maybeUnknownP}P extends ${Builtin}
522
527
  ? P
523
528
  : P &
524
529
  { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P> ${maybeExcludeType}>]: never };
@@ -526,7 +531,7 @@ function makeDeepPartial(options, longs) {
526
531
  // Based on the type from ts-essentials
527
532
  const keys = (0, options_1.addTypeToMessages)(options) ? (0, ts_poet_1.code) `Exclude<keyof T, '$type'>` : (0, ts_poet_1.code) `keyof T`;
528
533
  const DeepPartial = (0, ts_poet_1.conditionalOutput)("DeepPartial", (0, ts_poet_1.code) `
529
- ${maybeExport} type DeepPartial<T> = ${maybeBigInt} T extends ${Builtin}
534
+ ${maybeExport} type DeepPartial<T> = ${maybeUnknownT}${maybeBigInt} T extends ${Builtin}
530
535
  ? T
531
536
  ${maybeLong}
532
537
  : T extends globalThis.Array<infer U>
@@ -103,6 +103,7 @@ export type Options = {
103
103
  initializeFieldsAsUndefined: boolean;
104
104
  useMapType: boolean;
105
105
  useReadonlyTypes: boolean;
106
+ useUnknownForValue: boolean;
106
107
  useSnakeTypeName: boolean;
107
108
  outputExtensions: boolean;
108
109
  outputIndex: boolean;
@@ -119,6 +119,7 @@ function defaultOptions() {
119
119
  initializeFieldsAsUndefined: true,
120
120
  useMapType: false,
121
121
  useReadonlyTypes: false,
122
+ useUnknownForValue: false,
122
123
  useSnakeTypeName: true,
123
124
  outputExtensions: false,
124
125
  outputIndex: false,
@@ -542,12 +542,16 @@ function valueTypeName(ctx, typeName) {
542
542
  : ctx.options.useJsonWireFormat
543
543
  ? (0, ts_poet_1.code) `string`
544
544
  : (0, ts_poet_1.code) `Uint8Array`;
545
- case ".google.protobuf.ListValue":
546
- return ctx.options.useReadonlyTypes ? (0, ts_poet_1.code) `ReadonlyArray<any>` : (0, ts_poet_1.code) `Array<any>`;
545
+ case ".google.protobuf.ListValue": {
546
+ const item = ctx.options.useUnknownForValue ? "unknown" : "any";
547
+ return ctx.options.useReadonlyTypes ? (0, ts_poet_1.code) `ReadonlyArray<${item}>` : (0, ts_poet_1.code) `Array<${item}>`;
548
+ }
547
549
  case ".google.protobuf.Value":
548
- return (0, ts_poet_1.code) `any`;
549
- case ".google.protobuf.Struct":
550
- return ctx.options.useReadonlyTypes ? (0, ts_poet_1.code) `{readonly [key: string]: any}` : (0, ts_poet_1.code) `{[key: string]: any}`;
550
+ return ctx.options.useUnknownForValue ? (0, ts_poet_1.code) `unknown` : (0, ts_poet_1.code) `any`;
551
+ case ".google.protobuf.Struct": {
552
+ const item = ctx.options.useUnknownForValue ? "unknown" : "any";
553
+ return ctx.options.useReadonlyTypes ? (0, ts_poet_1.code) `{readonly [key: string]: ${item}}` : (0, ts_poet_1.code) `{[key: string]: ${item}}`;
554
+ }
551
555
  case ".google.protobuf.FieldMask":
552
556
  return ctx.options.useJsonWireFormat
553
557
  ? (0, ts_poet_1.code) `string`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ts-proto",
3
- "version": "2.16.0",
3
+ "version": "2.18.0",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",