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 +2 -0
- package/build/src/main.js +5 -2
- package/build/src/options.d.ts +1 -0
- package/build/src/options.js +1 -0
- package/build/src/types.js +9 -5
- package/package.json +1 -1
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>
|
package/build/src/options.d.ts
CHANGED
package/build/src/options.js
CHANGED
package/build/src/types.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
|
|
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`
|