ts-proto 1.137.2 → 1.138.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 +8 -4
- package/build/main.js +17 -2
- package/package.json +1 -1
package/README.markdown
CHANGED
|
@@ -167,6 +167,10 @@ Along with `encode`/`decode` factory methods:
|
|
|
167
167
|
|
|
168
168
|
```typescript
|
|
169
169
|
export const Simple = {
|
|
170
|
+
create(baseObject?: DeepPartial<Simple>): Simple {
|
|
171
|
+
...
|
|
172
|
+
},
|
|
173
|
+
|
|
170
174
|
encode(message: Simple, writer: Writer = Writer.create()): Writer {
|
|
171
175
|
...
|
|
172
176
|
},
|
|
@@ -332,7 +336,7 @@ Generated code will be placed in the Gradle build directory.
|
|
|
332
336
|
|
|
333
337
|
So, between typo-prevention, reader inconsistency, and proper initialization, ts-proto recommends using `useOptionals=none` as the "most safe" option.
|
|
334
338
|
|
|
335
|
-
All that said, this approach does require writers/creators to set every field (although `fromPartial`
|
|
339
|
+
All that said, this approach does require writers/creators to set every field (although `fromPartial` and `create` are meant to address this), so if you still want to have optional fields, you can set `useOptionals=messages` or `useOptionals=all`.
|
|
336
340
|
|
|
337
341
|
(See [this issue](https://github.com/stephenh/ts-proto/issues/120#issuecomment-678375833) and [this issue](https://github.com/stephenh/ts-proto/issues/397#issuecomment-977259118) for discussions on `useOptional`.)
|
|
338
342
|
|
|
@@ -368,7 +372,7 @@ Generated code will be placed in the Gradle build directory.
|
|
|
368
372
|
|
|
369
373
|
This is also useful if you want "only types".
|
|
370
374
|
|
|
371
|
-
- With `--ts_proto_opt=outputPartialMethods=false`, the `Message.fromPartial` methods for accepting partially-formed objects/object literals will not be output.
|
|
375
|
+
- With `--ts_proto_opt=outputPartialMethods=false`, the `Message.fromPartial` and `Message.create` methods for accepting partially-formed objects/object literals will not be output.
|
|
372
376
|
|
|
373
377
|
- With `--ts_proto_opt=stringEnums=true`, the generated enum types will be string-based instead of int-based.
|
|
374
378
|
|
|
@@ -433,9 +437,9 @@ Generated code will be placed in the Gradle build directory.
|
|
|
433
437
|
|
|
434
438
|
- With `--ts_proto_opt=enumsAsLiterals=true`, the generated enum types will be enum-ish object with `as const`.
|
|
435
439
|
|
|
436
|
-
- With `--ts_proto_opt=useExactTypes=false`, the generated `fromPartial`
|
|
440
|
+
- With `--ts_proto_opt=useExactTypes=false`, the generated `fromPartial` and `create` methods will not use Exact types.
|
|
437
441
|
|
|
438
|
-
The default behavior is `useExactTypes=true`, which makes `fromPartial` use Exact type for its argument to make TypeScript reject any unknown properties.
|
|
442
|
+
The default behavior is `useExactTypes=true`, which makes `fromPartial` and `create` use Exact type for its argument to make TypeScript reject any unknown properties.
|
|
439
443
|
|
|
440
444
|
- With `--ts_proto_opt=unknownFields=true`, all unknown fields will be parsed and output as arrays of buffers.
|
|
441
445
|
|
package/build/main.js
CHANGED
|
@@ -1389,9 +1389,24 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1389
1389
|
return (0, ts_poet_1.joinCode)(chunks, { on: "\n" });
|
|
1390
1390
|
}
|
|
1391
1391
|
function generateFromPartial(ctx, fullName, messageDesc) {
|
|
1392
|
-
const { options, utils
|
|
1392
|
+
const { options, utils } = ctx;
|
|
1393
1393
|
const chunks = [];
|
|
1394
|
-
// create the
|
|
1394
|
+
// create the create function definition
|
|
1395
|
+
if (ctx.options.useExactTypes) {
|
|
1396
|
+
chunks.push((0, ts_poet_1.code) `
|
|
1397
|
+
create<I extends ${utils.Exact}<${utils.DeepPartial}<${fullName}>, I>>(base?: I): ${fullName} {
|
|
1398
|
+
`);
|
|
1399
|
+
}
|
|
1400
|
+
else {
|
|
1401
|
+
chunks.push((0, ts_poet_1.code) `
|
|
1402
|
+
create(base?: ${utils.DeepPartial}<${fullName}>): ${fullName} {
|
|
1403
|
+
`);
|
|
1404
|
+
}
|
|
1405
|
+
chunks.push((0, ts_poet_1.code) `
|
|
1406
|
+
return ${fullName}.fromPartial(base ?? {})
|
|
1407
|
+
`);
|
|
1408
|
+
chunks.push((0, ts_poet_1.code) `},`, (0, ts_poet_1.code) ``);
|
|
1409
|
+
// create the fromPartial function declaration
|
|
1395
1410
|
const paramName = messageDesc.field.length > 0 ? "object" : "_";
|
|
1396
1411
|
if (ctx.options.useExactTypes) {
|
|
1397
1412
|
chunks.push((0, ts_poet_1.code) `
|