ts-proto 2.16.0 → 2.17.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
@@ -569,6 +569,8 @@ With `--ts_proto_opt=protoJsonFormat=false`, strict proto3 compliance is disable
569
569
 
570
570
  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
571
 
572
+ - 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.
573
+
572
574
  - With `--ts_proto_opt=useReadonlyTypes=true`, the generated types will be declared as immutable using typescript's `readonly` modifier.
573
575
 
574
576
  - With `--ts_proto_opt=useSnakeTypeName=false` will remove snake casing from types.
package/build/src/main.js CHANGED
@@ -506,6 +506,9 @@ function makeDeepPartial(options, longs) {
506
506
  const maybeLong = options.forceLong === options_1.LongOption.LONG ? (0, ts_poet_1.code) ` : T extends ${longs.Long} ? string | number | Long ` : "";
507
507
  // Allow passing bigints as numbers or strings; fromPartial coerces them via BigInt()
508
508
  const maybeBigInt = options.forceLong === options_1.LongOption.BIGINT ? (0, ts_poet_1.code) `T extends bigint ? string | number | bigint : ` : "";
509
+ // Keep `unknown` (useUnknownForValue) as is; mapping over it would turn it into `{}`
510
+ const maybeUnknownT = options.useUnknownForValue ? "unknown extends T ? T : " : "";
511
+ const maybeUnknownP = options.useUnknownForValue ? "unknown extends P ? P : " : "";
509
512
  const optionalBuiltins = [];
510
513
  if (options.forceLong === options_1.LongOption.BIGINT) {
511
514
  optionalBuiltins.push("bigint");
@@ -518,7 +521,7 @@ function makeDeepPartial(options, longs) {
518
521
  const maybeExcludeType = (0, options_1.addTypeToMessages)(options) ? `| '$type'` : "";
519
522
  const Exact = (0, ts_poet_1.conditionalOutput)("Exact", (0, ts_poet_1.code) `
520
523
  type KeysOfUnion<T> = T extends T ? keyof T : never;
521
- ${maybeExport} type Exact<P, I extends P> = P extends ${Builtin}
524
+ ${maybeExport} type Exact<P, I extends P> = ${maybeUnknownP}P extends ${Builtin}
522
525
  ? P
523
526
  : P &
524
527
  { [K in keyof P]: Exact<P[K], I[K]> } & { [K in Exclude<keyof I, KeysOfUnion<P> ${maybeExcludeType}>]: never };
@@ -526,7 +529,7 @@ function makeDeepPartial(options, longs) {
526
529
  // Based on the type from ts-essentials
527
530
  const keys = (0, options_1.addTypeToMessages)(options) ? (0, ts_poet_1.code) `Exclude<keyof T, '$type'>` : (0, ts_poet_1.code) `keyof T`;
528
531
  const DeepPartial = (0, ts_poet_1.conditionalOutput)("DeepPartial", (0, ts_poet_1.code) `
529
- ${maybeExport} type DeepPartial<T> = ${maybeBigInt} T extends ${Builtin}
532
+ ${maybeExport} type DeepPartial<T> = ${maybeUnknownT}${maybeBigInt} T extends ${Builtin}
530
533
  ? T
531
534
  ${maybeLong}
532
535
  : 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.17.0",
4
4
  "description": "",
5
5
  "main": "build/src/plugin.js",
6
6
  "repository": "github:stephenh/ts-proto",