ts-proto 1.159.1 → 1.159.3
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 +4 -4
- package/build/main.js +5 -5
- package/package.json +1 -1
package/README.markdown
CHANGED
|
@@ -325,8 +325,8 @@ Generated code will be placed in the Gradle build directory.
|
|
|
325
325
|
|
|
326
326
|
```typescript
|
|
327
327
|
interface SomeMessage {
|
|
328
|
-
firstName: string
|
|
329
|
-
lastName: string
|
|
328
|
+
firstName: string;
|
|
329
|
+
lastName: string;
|
|
330
330
|
}
|
|
331
331
|
// Declared with a typo
|
|
332
332
|
const data = { firstName: "a", lastTypo: "b" };
|
|
@@ -334,13 +334,13 @@ Generated code will be placed in the Gradle build directory.
|
|
|
334
334
|
const message: SomeMessage = { ...data };
|
|
335
335
|
```
|
|
336
336
|
|
|
337
|
-
For a consistent API, if `SomeMessage.lastName` is optional `lastName?`, then readers have to check _two_ empty conditions: a) is `lastName` `undefined` (b/c it was created in-memory and left unset), or b) is `lastName` empty string (b/c we read `SomeMessage` off the wire and
|
|
337
|
+
For a consistent API, if `SomeMessage.lastName` is optional `lastName?`, then readers have to check _two_ empty conditions: a) is `lastName` `undefined` (b/c it was created in-memory and left unset), or b) is `lastName` empty string (b/c we read `SomeMessage` off the wire and, per the proto3 spec, initialized `lastName` to empty string)?
|
|
338
338
|
|
|
339
339
|
For ensuring proper initialization, if later `SomeMessage.middleInitial` is added, but it's marked as optional `middleInitial?`, you may have many call sites in production code that _should_ now be passing `middleInitial` to create a valid `SomeMessage`, but are not.
|
|
340
340
|
|
|
341
341
|
So, between typo-prevention, reader inconsistency, and proper initialization, ts-proto recommends using `useOptionals=none` as the "most safe" option.
|
|
342
342
|
|
|
343
|
-
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
|
|
343
|
+
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 keys, you can set `useOptionals=messages` or `useOptionals=all`.
|
|
344
344
|
|
|
345
345
|
(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`.)
|
|
346
346
|
|
package/build/main.js
CHANGED
|
@@ -424,8 +424,8 @@ function makeDeepPartial(options, longs) {
|
|
|
424
424
|
${maybeExport} type DeepPartial<T> = T extends ${Builtin}
|
|
425
425
|
? T
|
|
426
426
|
${maybeLong}
|
|
427
|
-
: T extends Array<infer U>
|
|
428
|
-
? Array<DeepPartial<U>>
|
|
427
|
+
: T extends globalThis.Array<infer U>
|
|
428
|
+
? globalThis.Array<DeepPartial<U>>
|
|
429
429
|
: T extends ReadonlyArray<infer U>
|
|
430
430
|
? ReadonlyArray<DeepPartial<U>>${oneofCase}
|
|
431
431
|
: T extends {}
|
|
@@ -1438,7 +1438,7 @@ function generateFromJson(ctx, fullName, fullTypeName, messageDesc) {
|
|
|
1438
1438
|
}
|
|
1439
1439
|
else {
|
|
1440
1440
|
const cstr = (0, case_1.capitalize)((0, types_1.basicTypeName)(ctx, field, { keepValueType: true }).toCodeString([]));
|
|
1441
|
-
return (0, ts_poet_1.code) `${cstr}(${from})`;
|
|
1441
|
+
return (0, ts_poet_1.code) `${utils.globalThis}.${cstr}(${from})`;
|
|
1442
1442
|
}
|
|
1443
1443
|
}
|
|
1444
1444
|
else if ((0, types_1.isObjectId)(field) && options.useMongoObjectId) {
|
|
@@ -1669,7 +1669,7 @@ function generateToJson(ctx, fullName, fullProtobufTypeName, messageDesc) {
|
|
|
1669
1669
|
return (0, ts_poet_1.code) `${from}`;
|
|
1670
1670
|
}
|
|
1671
1671
|
else if ((0, types_1.isTimestamp)(field) && options.useDate === options_1.DateOption.TIMESTAMP) {
|
|
1672
|
-
return (0, ts_poet_1.code) `${
|
|
1672
|
+
return (0, ts_poet_1.code) `${from}`;
|
|
1673
1673
|
}
|
|
1674
1674
|
else if ((0, types_1.isMapType)(ctx, messageDesc, field)) {
|
|
1675
1675
|
// For map types, drill-in and then admittedly re-hard-code our per-value-type logic
|
|
@@ -1870,7 +1870,7 @@ function generateFromPartial(ctx, fullName, messageDesc) {
|
|
|
1870
1870
|
}
|
|
1871
1871
|
else {
|
|
1872
1872
|
const cstr = (0, case_1.capitalize)(valueType.toCodeString([]));
|
|
1873
|
-
return (0, ts_poet_1.code) `${cstr}(${from})`;
|
|
1873
|
+
return (0, ts_poet_1.code) `${utils.globalThis}.${cstr}(${from})`;
|
|
1874
1874
|
}
|
|
1875
1875
|
}
|
|
1876
1876
|
else if ((0, types_1.isAnyValueType)(valueField)) {
|