From 808f8a7a77d56f65dd4a4643dd66158f106ab755 Mon Sep 17 00:00:00 2001 From: Stephen Haberman Date: Sun, 28 Nov 2021 08:31:29 -0600 Subject: [PATCH] feat: Use exact types for fromPartial (#412) * Add proof-of-concept exact types. Fixes #156. * Extract Builtin. * Codegen. * Add expect error. * Codegen again. * Fix type registry. --- integration/angular/simple-message.ts | 8 +- integration/avoid-import-conflicts/simple.ts | 10 ++- integration/avoid-import-conflicts/simple2.ts | 8 +- integration/barrel-imports/bar.ts | 8 +- integration/barrel-imports/foo.ts | 8 +- integration/batching-with-context/batching.ts | 34 ++++--- integration/batching/batching.ts | 34 ++++--- integration/bytes-as-base64/message.ts | 8 +- integration/bytes-node/point.ts | 8 +- integration/const-enum/const-enum.ts | 8 +- .../generic-service-definitions/simple.ts | 8 +- integration/global-this/global-this.ts | 10 ++- integration/grpc-js/google/protobuf/empty.ts | 8 +- integration/grpc-js/google/protobuf/struct.ts | 16 ++-- .../grpc-js/google/protobuf/timestamp.ts | 8 +- .../grpc-js/google/protobuf/wrappers.ts | 24 +++-- integration/grpc-js/simple.ts | 8 +- integration/grpc-web-go-server/example.ts | 24 +++-- .../example.ts | 16 ++-- integration/grpc-web-no-streaming/example.ts | 16 ++-- integration/grpc-web/example.ts | 24 +++-- integration/lower-case-svc-methods/math.ts | 14 ++- .../no-proto-package/no-proto-package.ts | 10 ++- integration/oneof-properties/oneof.ts | 10 ++- integration/oneof-unions/oneof.ts | 12 ++- integration/point/point.ts | 10 ++- integration/return-observable/observable.ts | 10 ++- .../simple-deprecated-fields/simple.ts | 10 ++- integration/simple-esmodule-interop/simple.ts | 10 ++- .../google/protobuf/timestamp.ts | 8 +- integration/simple-json-name/simple.ts | 8 +- .../google/protobuf/timestamp.ts | 8 +- .../google/protobuf/wrappers.ts | 24 +++-- integration/simple-long-string/simple.ts | 8 +- .../simple-long/google/protobuf/wrappers.ts | 24 +++-- integration/simple-long/simple.ts | 28 ++++-- .../google/protobuf/timestamp.ts | 8 +- .../google/protobuf/wrappers.ts | 24 +++-- .../simple-optionals/import_dir/thing.ts | 8 +- integration/simple-optionals/simple.ts | 60 ++++++++----- integration/simple-optionals/thing.ts | 8 +- integration/simple-proto2/simple.ts | 8 +- .../simple-snake/google/protobuf/timestamp.ts | 8 +- .../simple-snake/google/protobuf/wrappers.ts | 24 +++-- integration/simple-snake/import_dir/thing.ts | 8 +- integration/simple-snake/simple.ts | 60 ++++++++----- integration/simple-string-enums/simple.ts | 10 ++- .../google/protobuf/timestamp.ts | 8 +- .../google/protobuf/wrappers.ts | 24 +++-- .../import_dir/thing.ts | 8 +- .../simple-unrecognized-enum/simple.ts | 60 ++++++++----- .../simple/google/protobuf/timestamp.ts | 8 +- .../simple/google/protobuf/wrappers.ts | 24 +++-- integration/simple/google/type/date.ts | 8 +- integration/simple/import_dir/thing.ts | 8 +- integration/simple/simple-test.ts | 7 ++ integration/simple/simple.ts | 88 ++++++++++++------- integration/struct/google/protobuf/struct.ts | 16 ++-- integration/struct/struct.ts | 8 +- integration/type-registry/bar/bar.ts | 8 +- integration/type-registry/foo.ts | 10 ++- .../google/protobuf/timestamp.ts | 8 +- integration/types-with-underscores/file.ts | 10 ++- .../google/protobuf/timestamp.ts | 8 +- integration/use-date-false/metadata.ts | 8 +- .../google/protobuf/timestamp.ts | 8 +- .../use-date-string/use-date-string.ts | 12 ++- .../google/protobuf/timestamp.ts | 8 +- integration/use-date-true/use-date-true.ts | 12 ++- integration/value/google/protobuf/struct.ts | 16 ++-- integration/value/google/protobuf/wrappers.ts | 24 +++-- integration/value/value.ts | 10 ++- integration/vector-tile/vector_tile.ts | 26 +++--- src/generate-type-registry.ts | 2 +- src/main.ts | 30 +++++-- 75 files changed, 845 insertions(+), 338 deletions(-) diff --git a/integration/angular/simple-message.ts b/integration/angular/simple-message.ts index 1db847db3..356091ac8 100644 --- a/integration/angular/simple-message.ts +++ b/integration/angular/simple-message.ts @@ -49,7 +49,7 @@ export const SimpleMessage = { return obj; }, - fromPartial(object: DeepPartial): SimpleMessage { + fromPartial, I>>(object: I): SimpleMessage { const message = { ...baseSimpleMessage } as SimpleMessage; message.numberField = object.numberField ?? 0; return message; @@ -57,6 +57,7 @@ export const SimpleMessage = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -67,6 +68,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/avoid-import-conflicts/simple.ts b/integration/avoid-import-conflicts/simple.ts index 4afd4c318..9b43915bc 100644 --- a/integration/avoid-import-conflicts/simple.ts +++ b/integration/avoid-import-conflicts/simple.ts @@ -110,7 +110,7 @@ export const Simple = { return obj; }, - fromPartial(object: DeepPartial): Simple { + fromPartial, I>>(object: I): Simple { const message = { ...baseSimple } as Simple; message.name = object.name ?? ''; message.otherSimple = @@ -171,7 +171,7 @@ export const SimpleEnums = { return obj; }, - fromPartial(object: DeepPartial): SimpleEnums { + fromPartial, I>>(object: I): SimpleEnums { const message = { ...baseSimpleEnums } as SimpleEnums; message.localEnum = object.localEnum ?? 0; message.importEnum = object.importEnum ?? 0; @@ -180,6 +180,7 @@ export const SimpleEnums = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -190,6 +191,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/avoid-import-conflicts/simple2.ts b/integration/avoid-import-conflicts/simple2.ts index 417895041..214706ce7 100644 --- a/integration/avoid-import-conflicts/simple2.ts +++ b/integration/avoid-import-conflicts/simple2.ts @@ -95,7 +95,7 @@ export const Simple = { return obj; }, - fromPartial(object: DeepPartial): Simple { + fromPartial, I>>(object: I): Simple { const message = { ...baseSimple } as Simple; message.name = object.name ?? ''; message.age = object.age ?? 0; @@ -104,6 +104,7 @@ export const Simple = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -114,6 +115,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/barrel-imports/bar.ts b/integration/barrel-imports/bar.ts index 96cb2248f..261cbd7c8 100644 --- a/integration/barrel-imports/bar.ts +++ b/integration/barrel-imports/bar.ts @@ -55,7 +55,7 @@ export const Bar = { return obj; }, - fromPartial(object: DeepPartial): Bar { + fromPartial, I>>(object: I): Bar { const message = { ...baseBar } as Bar; message.name = object.name ?? ''; message.age = object.age ?? 0; @@ -64,6 +64,7 @@ export const Bar = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + type DeepPartial = T extends Builtin ? T : T extends Array @@ -74,6 +75,11 @@ type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/barrel-imports/foo.ts b/integration/barrel-imports/foo.ts index 9a48e8127..548d2cff1 100644 --- a/integration/barrel-imports/foo.ts +++ b/integration/barrel-imports/foo.ts @@ -56,7 +56,7 @@ export const Foo = { return obj; }, - fromPartial(object: DeepPartial): Foo { + fromPartial, I>>(object: I): Foo { const message = { ...baseFoo } as Foo; message.name = object.name ?? ''; message.bar = object.bar !== undefined && object.bar !== null ? Bar.fromPartial(object.bar) : undefined; @@ -65,6 +65,7 @@ export const Foo = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + type DeepPartial = T extends Builtin ? T : T extends Array @@ -75,6 +76,11 @@ type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/batching-with-context/batching.ts b/integration/batching-with-context/batching.ts index 4da12298a..719bf95ea 100644 --- a/integration/batching-with-context/batching.ts +++ b/integration/batching-with-context/batching.ts @@ -91,9 +91,9 @@ export const BatchQueryRequest = { return obj; }, - fromPartial(object: DeepPartial): BatchQueryRequest { + fromPartial, I>>(object: I): BatchQueryRequest { const message = { ...baseBatchQueryRequest } as BatchQueryRequest; - message.ids = (object.ids ?? []).map((e) => e); + message.ids = object.ids?.map((e) => e) || []; return message; }, }; @@ -143,9 +143,9 @@ export const BatchQueryResponse = { return obj; }, - fromPartial(object: DeepPartial): BatchQueryResponse { + fromPartial, I>>(object: I): BatchQueryResponse { const message = { ...baseBatchQueryResponse } as BatchQueryResponse; - message.entities = (object.entities ?? []).map((e) => Entity.fromPartial(e)); + message.entities = object.entities?.map((e) => Entity.fromPartial(e)) || []; return message; }, }; @@ -195,9 +195,9 @@ export const BatchMapQueryRequest = { return obj; }, - fromPartial(object: DeepPartial): BatchMapQueryRequest { + fromPartial, I>>(object: I): BatchMapQueryRequest { const message = { ...baseBatchMapQueryRequest } as BatchMapQueryRequest; - message.ids = (object.ids ?? []).map((e) => e); + message.ids = object.ids?.map((e) => e) || []; return message; }, }; @@ -254,7 +254,7 @@ export const BatchMapQueryResponse = { return obj; }, - fromPartial(object: DeepPartial): BatchMapQueryResponse { + fromPartial, I>>(object: I): BatchMapQueryResponse { const message = { ...baseBatchMapQueryResponse } as BatchMapQueryResponse; message.entities = Object.entries(object.entities ?? {}).reduce<{ [key: string]: Entity }>((acc, [key, value]) => { if (value !== undefined) { @@ -314,7 +314,9 @@ export const BatchMapQueryResponse_EntitiesEntry = { return obj; }, - fromPartial(object: DeepPartial): BatchMapQueryResponse_EntitiesEntry { + fromPartial, I>>( + object: I + ): BatchMapQueryResponse_EntitiesEntry { const message = { ...baseBatchMapQueryResponse_EntitiesEntry } as BatchMapQueryResponse_EntitiesEntry; message.key = object.key ?? ''; message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; @@ -362,7 +364,7 @@ export const GetOnlyMethodRequest = { return obj; }, - fromPartial(object: DeepPartial): GetOnlyMethodRequest { + fromPartial, I>>(object: I): GetOnlyMethodRequest { const message = { ...baseGetOnlyMethodRequest } as GetOnlyMethodRequest; message.id = object.id ?? ''; return message; @@ -409,7 +411,7 @@ export const GetOnlyMethodResponse = { return obj; }, - fromPartial(object: DeepPartial): GetOnlyMethodResponse { + fromPartial, I>>(object: I): GetOnlyMethodResponse { const message = { ...baseGetOnlyMethodResponse } as GetOnlyMethodResponse; message.entity = object.entity !== undefined && object.entity !== null ? Entity.fromPartial(object.entity) : undefined; @@ -457,7 +459,7 @@ export const WriteMethodRequest = { return obj; }, - fromPartial(object: DeepPartial): WriteMethodRequest { + fromPartial, I>>(object: I): WriteMethodRequest { const message = { ...baseWriteMethodRequest } as WriteMethodRequest; message.id = object.id ?? ''; return message; @@ -496,7 +498,7 @@ export const WriteMethodResponse = { return obj; }, - fromPartial(_: DeepPartial): WriteMethodResponse { + fromPartial, I>>(_: I): WriteMethodResponse { const message = { ...baseWriteMethodResponse } as WriteMethodResponse; return message; }, @@ -550,7 +552,7 @@ export const Entity = { return obj; }, - fromPartial(object: DeepPartial): Entity { + fromPartial, I>>(object: I): Entity { const message = { ...baseEntity } as Entity; message.id = object.id ?? ''; message.name = object.name ?? ''; @@ -656,6 +658,7 @@ export interface DataLoaders { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -666,6 +669,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/batching/batching.ts b/integration/batching/batching.ts index 1aa547719..9a73f86e1 100644 --- a/integration/batching/batching.ts +++ b/integration/batching/batching.ts @@ -89,9 +89,9 @@ export const BatchQueryRequest = { return obj; }, - fromPartial(object: DeepPartial): BatchQueryRequest { + fromPartial, I>>(object: I): BatchQueryRequest { const message = { ...baseBatchQueryRequest } as BatchQueryRequest; - message.ids = (object.ids ?? []).map((e) => e); + message.ids = object.ids?.map((e) => e) || []; return message; }, }; @@ -141,9 +141,9 @@ export const BatchQueryResponse = { return obj; }, - fromPartial(object: DeepPartial): BatchQueryResponse { + fromPartial, I>>(object: I): BatchQueryResponse { const message = { ...baseBatchQueryResponse } as BatchQueryResponse; - message.entities = (object.entities ?? []).map((e) => Entity.fromPartial(e)); + message.entities = object.entities?.map((e) => Entity.fromPartial(e)) || []; return message; }, }; @@ -193,9 +193,9 @@ export const BatchMapQueryRequest = { return obj; }, - fromPartial(object: DeepPartial): BatchMapQueryRequest { + fromPartial, I>>(object: I): BatchMapQueryRequest { const message = { ...baseBatchMapQueryRequest } as BatchMapQueryRequest; - message.ids = (object.ids ?? []).map((e) => e); + message.ids = object.ids?.map((e) => e) || []; return message; }, }; @@ -252,7 +252,7 @@ export const BatchMapQueryResponse = { return obj; }, - fromPartial(object: DeepPartial): BatchMapQueryResponse { + fromPartial, I>>(object: I): BatchMapQueryResponse { const message = { ...baseBatchMapQueryResponse } as BatchMapQueryResponse; message.entities = Object.entries(object.entities ?? {}).reduce<{ [key: string]: Entity }>((acc, [key, value]) => { if (value !== undefined) { @@ -312,7 +312,9 @@ export const BatchMapQueryResponse_EntitiesEntry = { return obj; }, - fromPartial(object: DeepPartial): BatchMapQueryResponse_EntitiesEntry { + fromPartial, I>>( + object: I + ): BatchMapQueryResponse_EntitiesEntry { const message = { ...baseBatchMapQueryResponse_EntitiesEntry } as BatchMapQueryResponse_EntitiesEntry; message.key = object.key ?? ''; message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; @@ -360,7 +362,7 @@ export const GetOnlyMethodRequest = { return obj; }, - fromPartial(object: DeepPartial): GetOnlyMethodRequest { + fromPartial, I>>(object: I): GetOnlyMethodRequest { const message = { ...baseGetOnlyMethodRequest } as GetOnlyMethodRequest; message.id = object.id ?? ''; return message; @@ -407,7 +409,7 @@ export const GetOnlyMethodResponse = { return obj; }, - fromPartial(object: DeepPartial): GetOnlyMethodResponse { + fromPartial, I>>(object: I): GetOnlyMethodResponse { const message = { ...baseGetOnlyMethodResponse } as GetOnlyMethodResponse; message.entity = object.entity !== undefined && object.entity !== null ? Entity.fromPartial(object.entity) : undefined; @@ -455,7 +457,7 @@ export const WriteMethodRequest = { return obj; }, - fromPartial(object: DeepPartial): WriteMethodRequest { + fromPartial, I>>(object: I): WriteMethodRequest { const message = { ...baseWriteMethodRequest } as WriteMethodRequest; message.id = object.id ?? ''; return message; @@ -494,7 +496,7 @@ export const WriteMethodResponse = { return obj; }, - fromPartial(_: DeepPartial): WriteMethodResponse { + fromPartial, I>>(_: I): WriteMethodResponse { const message = { ...baseWriteMethodResponse } as WriteMethodResponse; return message; }, @@ -548,7 +550,7 @@ export const Entity = { return obj; }, - fromPartial(object: DeepPartial): Entity { + fromPartial, I>>(object: I): Entity { const message = { ...baseEntity } as Entity; message.id = object.id ?? ''; message.name = object.name ?? ''; @@ -604,6 +606,7 @@ interface Rpc { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -614,6 +617,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/bytes-as-base64/message.ts b/integration/bytes-as-base64/message.ts index 72bc4928a..bfbd42460 100644 --- a/integration/bytes-as-base64/message.ts +++ b/integration/bytes-as-base64/message.ts @@ -24,7 +24,7 @@ export const Message = { return obj; }, - fromPartial(object: DeepPartial): Message { + fromPartial, I>>(object: I): Message { const message = { ...baseMessage } as Message; message.data = object.data ?? new Uint8Array(); return message; @@ -64,6 +64,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -74,6 +75,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/bytes-node/point.ts b/integration/bytes-node/point.ts index de913dcba..14f43a529 100644 --- a/integration/bytes-node/point.ts +++ b/integration/bytes-node/point.ts @@ -51,7 +51,7 @@ export const Point = { return obj; }, - fromPartial(object: DeepPartial): Point { + fromPartial, I>>(object: I): Point { const message = { ...basePoint } as Point; message.data = object.data ?? Buffer.alloc(0); return message; @@ -91,6 +91,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -101,6 +102,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/const-enum/const-enum.ts b/integration/const-enum/const-enum.ts index 6af20d49e..428b27e15 100644 --- a/integration/const-enum/const-enum.ts +++ b/integration/const-enum/const-enum.ts @@ -110,7 +110,7 @@ export const DividerData = { return obj; }, - fromPartial(object: DeepPartial): DividerData { + fromPartial, I>>(object: I): DividerData { const message = { ...baseDividerData } as DividerData; message.type = object.type ?? DividerData_DividerType.DOUBLE; return message; @@ -118,6 +118,7 @@ export const DividerData = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -128,6 +129,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/generic-service-definitions/simple.ts b/integration/generic-service-definitions/simple.ts index 32d61ee0f..4d1894dd5 100644 --- a/integration/generic-service-definitions/simple.ts +++ b/integration/generic-service-definitions/simple.ts @@ -48,7 +48,7 @@ export const TestMessage = { return obj; }, - fromPartial(object: DeepPartial): TestMessage { + fromPartial, I>>(object: I): TestMessage { const message = { ...baseTestMessage } as TestMessage; message.value = object.value ?? ''; return message; @@ -125,6 +125,7 @@ export const TestDefinition = { } as const; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -135,6 +136,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/global-this/global-this.ts b/integration/global-this/global-this.ts index 70f23822b..f80ff78c1 100644 --- a/integration/global-this/global-this.ts +++ b/integration/global-this/global-this.ts @@ -52,7 +52,7 @@ export const Object = { return obj; }, - fromPartial(object: DeepPartial): Object { + fromPartial, I>>(object: I): Object { const message = { ...baseObject } as Object; message.name = object.name ?? ''; return message; @@ -99,7 +99,7 @@ export const Error = { return obj; }, - fromPartial(object: DeepPartial): Error { + fromPartial, I>>(object: I): Error { const message = { ...baseError } as Error; message.name = object.name ?? ''; return message; @@ -107,6 +107,7 @@ export const Error = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -117,6 +118,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/grpc-js/google/protobuf/empty.ts b/integration/grpc-js/google/protobuf/empty.ts index 185df6552..2370dce9e 100644 --- a/integration/grpc-js/google/protobuf/empty.ts +++ b/integration/grpc-js/google/protobuf/empty.ts @@ -49,13 +49,14 @@ export const Empty = { return obj; }, - fromPartial(_: DeepPartial): Empty { + fromPartial, I>>(_: I): Empty { const message = { ...baseEmpty } as Empty; return message; }, }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -66,6 +67,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/grpc-js/google/protobuf/struct.ts b/integration/grpc-js/google/protobuf/struct.ts index a2b7d6f54..28fe05424 100644 --- a/integration/grpc-js/google/protobuf/struct.ts +++ b/integration/grpc-js/google/protobuf/struct.ts @@ -147,7 +147,7 @@ export const Struct = { return obj; }, - fromPartial(object: DeepPartial): Struct { + fromPartial, I>>(object: I): Struct { const message = { ...baseStruct } as Struct; message.fields = Object.entries(object.fields ?? {}).reduce<{ [key: string]: any | undefined }>( (acc, [key, value]) => { @@ -228,7 +228,7 @@ export const Struct_FieldsEntry = { return obj; }, - fromPartial(object: DeepPartial): Struct_FieldsEntry { + fromPartial, I>>(object: I): Struct_FieldsEntry { const message = { ...baseStruct_FieldsEntry } as Struct_FieldsEntry; message.key = object.key ?? ''; message.value = object.value ?? undefined; @@ -321,7 +321,7 @@ export const Value = { return obj; }, - fromPartial(object: DeepPartial): Value { + fromPartial, I>>(object: I): Value { const message = { ...baseValue } as Value; message.nullValue = object.nullValue ?? undefined; message.numberValue = object.numberValue ?? undefined; @@ -414,9 +414,9 @@ export const ListValue = { return obj; }, - fromPartial(object: DeepPartial): ListValue { + fromPartial, I>>(object: I): ListValue { const message = { ...baseListValue } as ListValue; - message.values = (object.values ?? []).map((e) => e); + message.values = object.values?.map((e) => e) || []; return message; }, @@ -430,6 +430,7 @@ export const ListValue = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -440,6 +441,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/grpc-js/google/protobuf/timestamp.ts b/integration/grpc-js/google/protobuf/timestamp.ts index e4600a73e..0670c17fd 100644 --- a/integration/grpc-js/google/protobuf/timestamp.ts +++ b/integration/grpc-js/google/protobuf/timestamp.ts @@ -161,7 +161,7 @@ export const Timestamp = { return obj; }, - fromPartial(object: DeepPartial): Timestamp { + fromPartial, I>>(object: I): Timestamp { const message = { ...baseTimestamp } as Timestamp; message.seconds = object.seconds ?? 0; message.nanos = object.nanos ?? 0; @@ -181,6 +181,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -191,6 +192,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/grpc-js/google/protobuf/wrappers.ts b/integration/grpc-js/google/protobuf/wrappers.ts index 216285744..35b159990 100644 --- a/integration/grpc-js/google/protobuf/wrappers.ts +++ b/integration/grpc-js/google/protobuf/wrappers.ts @@ -134,7 +134,7 @@ export const DoubleValue = { return obj; }, - fromPartial(object: DeepPartial): DoubleValue { + fromPartial, I>>(object: I): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; message.value = object.value ?? 0; return message; @@ -181,7 +181,7 @@ export const FloatValue = { return obj; }, - fromPartial(object: DeepPartial): FloatValue { + fromPartial, I>>(object: I): FloatValue { const message = { ...baseFloatValue } as FloatValue; message.value = object.value ?? 0; return message; @@ -228,7 +228,7 @@ export const Int64Value = { return obj; }, - fromPartial(object: DeepPartial): Int64Value { + fromPartial, I>>(object: I): Int64Value { const message = { ...baseInt64Value } as Int64Value; message.value = object.value ?? 0; return message; @@ -275,7 +275,7 @@ export const UInt64Value = { return obj; }, - fromPartial(object: DeepPartial): UInt64Value { + fromPartial, I>>(object: I): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; message.value = object.value ?? 0; return message; @@ -322,7 +322,7 @@ export const Int32Value = { return obj; }, - fromPartial(object: DeepPartial): Int32Value { + fromPartial, I>>(object: I): Int32Value { const message = { ...baseInt32Value } as Int32Value; message.value = object.value ?? 0; return message; @@ -369,7 +369,7 @@ export const UInt32Value = { return obj; }, - fromPartial(object: DeepPartial): UInt32Value { + fromPartial, I>>(object: I): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; message.value = object.value ?? 0; return message; @@ -416,7 +416,7 @@ export const BoolValue = { return obj; }, - fromPartial(object: DeepPartial): BoolValue { + fromPartial, I>>(object: I): BoolValue { const message = { ...baseBoolValue } as BoolValue; message.value = object.value ?? false; return message; @@ -463,7 +463,7 @@ export const StringValue = { return obj; }, - fromPartial(object: DeepPartial): StringValue { + fromPartial, I>>(object: I): StringValue { const message = { ...baseStringValue } as StringValue; message.value = object.value ?? ''; return message; @@ -513,7 +513,7 @@ export const BytesValue = { return obj; }, - fromPartial(object: DeepPartial): BytesValue { + fromPartial, I>>(object: I): BytesValue { const message = { ...baseBytesValue } as BytesValue; message.value = object.value ?? new Uint8Array(); return message; @@ -553,6 +553,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -563,6 +564,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/grpc-js/simple.ts b/integration/grpc-js/simple.ts index 51193a12f..c8eb3ede9 100644 --- a/integration/grpc-js/simple.ts +++ b/integration/grpc-js/simple.ts @@ -81,7 +81,7 @@ export const TestMessage = { return obj; }, - fromPartial(object: DeepPartial): TestMessage { + fromPartial, I>>(object: I): TestMessage { const message = { ...baseTestMessage } as TestMessage; message.timestamp = object.timestamp ?? undefined; return message; @@ -622,6 +622,7 @@ export const TestClient = (makeGenericClientConstructor(TestService, 'simple.Tes }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -632,6 +633,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/grpc-web-go-server/example.ts b/integration/grpc-web-go-server/example.ts index 9c45365aa..95f4a127f 100644 --- a/integration/grpc-web-go-server/example.ts +++ b/integration/grpc-web-go-server/example.ts @@ -140,7 +140,7 @@ export const DashFlash = { return obj; }, - fromPartial(object: DeepPartial): DashFlash { + fromPartial, I>>(object: I): DashFlash { const message = { ...baseDashFlash } as DashFlash; message.msg = object.msg ?? ''; message.type = object.type ?? 0; @@ -211,14 +211,14 @@ export const DashUserSettingsState = { return obj; }, - fromPartial(object: DeepPartial): DashUserSettingsState { + fromPartial, I>>(object: I): DashUserSettingsState { const message = { ...baseDashUserSettingsState } as DashUserSettingsState; message.email = object.email ?? ''; message.urls = object.urls !== undefined && object.urls !== null ? DashUserSettingsState_URLs.fromPartial(object.urls) : undefined; - message.flashes = (object.flashes ?? []).map((e) => DashFlash.fromPartial(e)); + message.flashes = object.flashes?.map((e) => DashFlash.fromPartial(e)) || []; return message; }, }; @@ -273,7 +273,7 @@ export const DashUserSettingsState_URLs = { return obj; }, - fromPartial(object: DeepPartial): DashUserSettingsState_URLs { + fromPartial, I>>(object: I): DashUserSettingsState_URLs { const message = { ...baseDashUserSettingsState_URLs } as DashUserSettingsState_URLs; message.connectGoogle = object.connectGoogle ?? ''; message.connectGithub = object.connectGithub ?? ''; @@ -346,7 +346,7 @@ export const DashCred = { return obj; }, - fromPartial(object: DeepPartial): DashCred { + fromPartial, I>>(object: I): DashCred { const message = { ...baseDashCred } as DashCred; message.description = object.description ?? ''; message.metadata = object.metadata ?? ''; @@ -405,7 +405,7 @@ export const DashAPICredsCreateReq = { return obj; }, - fromPartial(object: DeepPartial): DashAPICredsCreateReq { + fromPartial, I>>(object: I): DashAPICredsCreateReq { const message = { ...baseDashAPICredsCreateReq } as DashAPICredsCreateReq; message.description = object.description ?? ''; message.metadata = object.metadata ?? ''; @@ -478,7 +478,7 @@ export const DashAPICredsUpdateReq = { return obj; }, - fromPartial(object: DeepPartial): DashAPICredsUpdateReq { + fromPartial, I>>(object: I): DashAPICredsUpdateReq { const message = { ...baseDashAPICredsUpdateReq } as DashAPICredsUpdateReq; message.credSid = object.credSid ?? ''; message.description = object.description ?? ''; @@ -536,7 +536,7 @@ export const DashAPICredsDeleteReq = { return obj; }, - fromPartial(object: DeepPartial): DashAPICredsDeleteReq { + fromPartial, I>>(object: I): DashAPICredsDeleteReq { const message = { ...baseDashAPICredsDeleteReq } as DashAPICredsDeleteReq; message.credSid = object.credSid ?? ''; message.id = object.id ?? ''; @@ -576,7 +576,7 @@ export const Empty = { return obj; }, - fromPartial(_: DeepPartial): Empty { + fromPartial, I>>(_: I): Empty { const message = { ...baseEmpty } as Empty; return message; }, @@ -653,6 +653,7 @@ interface Rpc { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -663,6 +664,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/grpc-web-no-streaming-observable/example.ts b/integration/grpc-web-no-streaming-observable/example.ts index 9595cea8d..01695c187 100644 --- a/integration/grpc-web-no-streaming-observable/example.ts +++ b/integration/grpc-web-no-streaming-observable/example.ts @@ -118,7 +118,7 @@ export const DashFlash = { return obj; }, - fromPartial(object: DeepPartial): DashFlash { + fromPartial, I>>(object: I): DashFlash { const message = { ...baseDashFlash } as DashFlash; message.msg = object.msg ?? ''; message.type = object.type ?? 0; @@ -189,14 +189,14 @@ export const DashUserSettingsState = { return obj; }, - fromPartial(object: DeepPartial): DashUserSettingsState { + fromPartial, I>>(object: I): DashUserSettingsState { const message = { ...baseDashUserSettingsState } as DashUserSettingsState; message.email = object.email ?? ''; message.urls = object.urls !== undefined && object.urls !== null ? DashUserSettingsState_URLs.fromPartial(object.urls) : undefined; - message.flashes = (object.flashes ?? []).map((e) => DashFlash.fromPartial(e)); + message.flashes = object.flashes?.map((e) => DashFlash.fromPartial(e)) || []; return message; }, }; @@ -251,7 +251,7 @@ export const DashUserSettingsState_URLs = { return obj; }, - fromPartial(object: DeepPartial): DashUserSettingsState_URLs { + fromPartial, I>>(object: I): DashUserSettingsState_URLs { const message = { ...baseDashUserSettingsState_URLs } as DashUserSettingsState_URLs; message.connectGoogle = object.connectGoogle ?? ''; message.connectGithub = object.connectGithub ?? ''; @@ -291,7 +291,7 @@ export const Empty = { return obj; }, - fromPartial(_: DeepPartial): Empty { + fromPartial, I>>(_: I): Empty { const message = { ...baseEmpty } as Empty; return message; }, @@ -412,6 +412,7 @@ export class GrpcWebImpl { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -422,6 +423,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/grpc-web-no-streaming/example.ts b/integration/grpc-web-no-streaming/example.ts index 44a2bb82a..54f2f3f8d 100644 --- a/integration/grpc-web-no-streaming/example.ts +++ b/integration/grpc-web-no-streaming/example.ts @@ -116,7 +116,7 @@ export const DashFlash = { return obj; }, - fromPartial(object: DeepPartial): DashFlash { + fromPartial, I>>(object: I): DashFlash { const message = { ...baseDashFlash } as DashFlash; message.msg = object.msg ?? ''; message.type = object.type ?? 0; @@ -187,14 +187,14 @@ export const DashUserSettingsState = { return obj; }, - fromPartial(object: DeepPartial): DashUserSettingsState { + fromPartial, I>>(object: I): DashUserSettingsState { const message = { ...baseDashUserSettingsState } as DashUserSettingsState; message.email = object.email ?? ''; message.urls = object.urls !== undefined && object.urls !== null ? DashUserSettingsState_URLs.fromPartial(object.urls) : undefined; - message.flashes = (object.flashes ?? []).map((e) => DashFlash.fromPartial(e)); + message.flashes = object.flashes?.map((e) => DashFlash.fromPartial(e)) || []; return message; }, }; @@ -249,7 +249,7 @@ export const DashUserSettingsState_URLs = { return obj; }, - fromPartial(object: DeepPartial): DashUserSettingsState_URLs { + fromPartial, I>>(object: I): DashUserSettingsState_URLs { const message = { ...baseDashUserSettingsState_URLs } as DashUserSettingsState_URLs; message.connectGoogle = object.connectGoogle ?? ''; message.connectGithub = object.connectGithub ?? ''; @@ -289,7 +289,7 @@ export const Empty = { return obj; }, - fromPartial(_: DeepPartial): Empty { + fromPartial, I>>(_: I): Empty { const message = { ...baseEmpty } as Empty; return message; }, @@ -412,6 +412,7 @@ export class GrpcWebImpl { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -422,6 +423,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/grpc-web/example.ts b/integration/grpc-web/example.ts index 3c45cf699..35f814528 100644 --- a/integration/grpc-web/example.ts +++ b/integration/grpc-web/example.ts @@ -142,7 +142,7 @@ export const DashFlash = { return obj; }, - fromPartial(object: DeepPartial): DashFlash { + fromPartial, I>>(object: I): DashFlash { const message = { ...baseDashFlash } as DashFlash; message.msg = object.msg ?? ''; message.type = object.type ?? 0; @@ -213,14 +213,14 @@ export const DashUserSettingsState = { return obj; }, - fromPartial(object: DeepPartial): DashUserSettingsState { + fromPartial, I>>(object: I): DashUserSettingsState { const message = { ...baseDashUserSettingsState } as DashUserSettingsState; message.email = object.email ?? ''; message.urls = object.urls !== undefined && object.urls !== null ? DashUserSettingsState_URLs.fromPartial(object.urls) : undefined; - message.flashes = (object.flashes ?? []).map((e) => DashFlash.fromPartial(e)); + message.flashes = object.flashes?.map((e) => DashFlash.fromPartial(e)) || []; return message; }, }; @@ -275,7 +275,7 @@ export const DashUserSettingsState_URLs = { return obj; }, - fromPartial(object: DeepPartial): DashUserSettingsState_URLs { + fromPartial, I>>(object: I): DashUserSettingsState_URLs { const message = { ...baseDashUserSettingsState_URLs } as DashUserSettingsState_URLs; message.connectGoogle = object.connectGoogle ?? ''; message.connectGithub = object.connectGithub ?? ''; @@ -348,7 +348,7 @@ export const DashCred = { return obj; }, - fromPartial(object: DeepPartial): DashCred { + fromPartial, I>>(object: I): DashCred { const message = { ...baseDashCred } as DashCred; message.description = object.description ?? ''; message.metadata = object.metadata ?? ''; @@ -407,7 +407,7 @@ export const DashAPICredsCreateReq = { return obj; }, - fromPartial(object: DeepPartial): DashAPICredsCreateReq { + fromPartial, I>>(object: I): DashAPICredsCreateReq { const message = { ...baseDashAPICredsCreateReq } as DashAPICredsCreateReq; message.description = object.description ?? ''; message.metadata = object.metadata ?? ''; @@ -480,7 +480,7 @@ export const DashAPICredsUpdateReq = { return obj; }, - fromPartial(object: DeepPartial): DashAPICredsUpdateReq { + fromPartial, I>>(object: I): DashAPICredsUpdateReq { const message = { ...baseDashAPICredsUpdateReq } as DashAPICredsUpdateReq; message.credSid = object.credSid ?? ''; message.description = object.description ?? ''; @@ -538,7 +538,7 @@ export const DashAPICredsDeleteReq = { return obj; }, - fromPartial(object: DeepPartial): DashAPICredsDeleteReq { + fromPartial, I>>(object: I): DashAPICredsDeleteReq { const message = { ...baseDashAPICredsDeleteReq } as DashAPICredsDeleteReq; message.credSid = object.credSid ?? ''; message.id = object.id ?? ''; @@ -578,7 +578,7 @@ export const Empty = { return obj; }, - fromPartial(_: DeepPartial): Empty { + fromPartial, I>>(_: I): Empty { const message = { ...baseEmpty } as Empty; return message; }, @@ -872,6 +872,7 @@ export class GrpcWebImpl { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -882,6 +883,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/lower-case-svc-methods/math.ts b/integration/lower-case-svc-methods/math.ts index 3d3114e4e..734260076 100644 --- a/integration/lower-case-svc-methods/math.ts +++ b/integration/lower-case-svc-methods/math.ts @@ -67,7 +67,7 @@ export const NumPair = { return obj; }, - fromPartial(object: DeepPartial): NumPair { + fromPartial, I>>(object: I): NumPair { const message = { ...baseNumPair } as NumPair; message.num1 = object.num1 ?? 0; message.num2 = object.num2 ?? 0; @@ -115,7 +115,7 @@ export const NumSingle = { return obj; }, - fromPartial(object: DeepPartial): NumSingle { + fromPartial, I>>(object: I): NumSingle { const message = { ...baseNumSingle } as NumSingle; message.num = object.num ?? 0; return message; @@ -176,9 +176,9 @@ export const Numbers = { return obj; }, - fromPartial(object: DeepPartial): Numbers { + fromPartial, I>>(object: I): Numbers { const message = { ...baseNumbers } as Numbers; - message.num = (object.num ?? []).map((e) => e); + message.num = object.num?.map((e) => e) || []; return message; }, }; @@ -244,6 +244,7 @@ export interface DataLoaders { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -254,6 +255,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/no-proto-package/no-proto-package.ts b/integration/no-proto-package/no-proto-package.ts index 2ebf4b84a..7939a0fac 100644 --- a/integration/no-proto-package/no-proto-package.ts +++ b/integration/no-proto-package/no-proto-package.ts @@ -52,7 +52,7 @@ export const User = { return obj; }, - fromPartial(object: DeepPartial): User { + fromPartial, I>>(object: I): User { const message = { ...baseUser } as User; message.name = object.name ?? ''; return message; @@ -91,7 +91,7 @@ export const Empty = { return obj; }, - fromPartial(_: DeepPartial): Empty { + fromPartial, I>>(_: I): Empty { const message = { ...baseEmpty } as Empty; return message; }, @@ -122,6 +122,7 @@ interface Rpc { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -132,6 +133,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/oneof-properties/oneof.ts b/integration/oneof-properties/oneof.ts index ba25e9a45..050d017e6 100644 --- a/integration/oneof-properties/oneof.ts +++ b/integration/oneof-properties/oneof.ts @@ -201,7 +201,7 @@ export const PleaseChoose = { return obj; }, - fromPartial(object: DeepPartial): PleaseChoose { + fromPartial, I>>(object: I): PleaseChoose { const message = { ...basePleaseChoose } as PleaseChoose; message.name = object.name ?? ''; message.aNumber = object.aNumber ?? undefined; @@ -261,7 +261,7 @@ export const PleaseChoose_Submessage = { return obj; }, - fromPartial(object: DeepPartial): PleaseChoose_Submessage { + fromPartial, I>>(object: I): PleaseChoose_Submessage { const message = { ...basePleaseChoose_Submessage } as PleaseChoose_Submessage; message.name = object.name ?? ''; return message; @@ -301,6 +301,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -311,6 +312,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/oneof-unions/oneof.ts b/integration/oneof-unions/oneof.ts index c4bb89be7..0b522ec26 100644 --- a/integration/oneof-unions/oneof.ts +++ b/integration/oneof-unions/oneof.ts @@ -225,7 +225,7 @@ export const PleaseChoose = { return obj; }, - fromPartial(object: DeepPartial): PleaseChoose { + fromPartial, I>>(object: I): PleaseChoose { const message = { ...basePleaseChoose } as PleaseChoose; message.name = object.name ?? ''; if (object.choice?.$case === 'aNumber' && object.choice?.aNumber !== undefined && object.choice?.aNumber !== null) { @@ -317,7 +317,7 @@ export const PleaseChoose_Submessage = { return obj; }, - fromPartial(object: DeepPartial): PleaseChoose_Submessage { + fromPartial, I>>(object: I): PleaseChoose_Submessage { const message = { ...basePleaseChoose_Submessage } as PleaseChoose_Submessage; message.name = object.name ?? ''; return message; @@ -372,7 +372,7 @@ export const SimpleButOptional = { return obj; }, - fromPartial(object: DeepPartial): SimpleButOptional { + fromPartial, I>>(object: I): SimpleButOptional { const message = { ...baseSimpleButOptional } as SimpleButOptional; message.name = object.name ?? undefined; message.age = object.age ?? undefined; @@ -413,6 +413,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -425,6 +426,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/point/point.ts b/integration/point/point.ts index b177a9983..065a2397f 100644 --- a/integration/point/point.ts +++ b/integration/point/point.ts @@ -62,7 +62,7 @@ export const Point = { return obj; }, - fromPartial(object: DeepPartial): Point { + fromPartial, I>>(object: I): Point { const message = { ...basePoint } as Point; message.lat = object.lat ?? 0; message.lng = object.lng ?? 0; @@ -118,7 +118,7 @@ export const Area = { return obj; }, - fromPartial(object: DeepPartial): Area { + fromPartial, I>>(object: I): Area { const message = { ...baseArea } as Area; message.nw = object.nw !== undefined && object.nw !== null ? Point.fromPartial(object.nw) : undefined; message.se = object.se !== undefined && object.se !== null ? Point.fromPartial(object.se) : undefined; @@ -127,6 +127,7 @@ export const Area = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -137,6 +138,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/return-observable/observable.ts b/integration/return-observable/observable.ts index ea41954c1..c72522a30 100644 --- a/integration/return-observable/observable.ts +++ b/integration/return-observable/observable.ts @@ -54,7 +54,7 @@ export const ProduceRequest = { return obj; }, - fromPartial(object: DeepPartial): ProduceRequest { + fromPartial, I>>(object: I): ProduceRequest { const message = { ...baseProduceRequest } as ProduceRequest; message.ingredients = object.ingredients ?? ''; return message; @@ -101,7 +101,7 @@ export const ProduceReply = { return obj; }, - fromPartial(object: DeepPartial): ProduceReply { + fromPartial, I>>(object: I): ProduceReply { const message = { ...baseProduceReply } as ProduceReply; message.result = object.result ?? ''; return message; @@ -113,6 +113,7 @@ export interface Factory { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -123,6 +124,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/simple-deprecated-fields/simple.ts b/integration/simple-deprecated-fields/simple.ts index e4e09d731..66936d91a 100644 --- a/integration/simple-deprecated-fields/simple.ts +++ b/integration/simple-deprecated-fields/simple.ts @@ -107,7 +107,7 @@ export const Simple = { return obj; }, - fromPartial(object: DeepPartial): Simple { + fromPartial, I>>(object: I): Simple { const message = { ...baseSimple } as Simple; message.name = object.name ?? ''; message.age = object.age ?? 0; @@ -158,7 +158,7 @@ export const Child = { return obj; }, - fromPartial(object: DeepPartial): Child { + fromPartial, I>>(object: I): Child { const message = { ...baseChild } as Child; message.name = object.name ?? ''; return message; @@ -166,6 +166,7 @@ export const Child = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -176,6 +177,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/simple-esmodule-interop/simple.ts b/integration/simple-esmodule-interop/simple.ts index f003ac4f7..6e71cb9ca 100644 --- a/integration/simple-esmodule-interop/simple.ts +++ b/integration/simple-esmodule-interop/simple.ts @@ -72,7 +72,7 @@ export const Simple = { return obj; }, - fromPartial(object: DeepPartial): Simple { + fromPartial, I>>(object: I): Simple { const message = { ...baseSimple } as Simple; message.name = object.name ?? ''; message.age = object.age ?? 0; @@ -221,7 +221,7 @@ export const Numbers = { return obj; }, - fromPartial(object: DeepPartial): Numbers { + fromPartial, I>>(object: I): Numbers { const message = { ...baseNumbers } as Numbers; message.double = object.double ?? 0; message.float = object.float ?? 0; @@ -251,6 +251,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -261,6 +262,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/simple-json-name/google/protobuf/timestamp.ts b/integration/simple-json-name/google/protobuf/timestamp.ts index e4600a73e..0670c17fd 100644 --- a/integration/simple-json-name/google/protobuf/timestamp.ts +++ b/integration/simple-json-name/google/protobuf/timestamp.ts @@ -161,7 +161,7 @@ export const Timestamp = { return obj; }, - fromPartial(object: DeepPartial): Timestamp { + fromPartial, I>>(object: I): Timestamp { const message = { ...baseTimestamp } as Timestamp; message.seconds = object.seconds ?? 0; message.nanos = object.nanos ?? 0; @@ -181,6 +181,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -191,6 +192,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/simple-json-name/simple.ts b/integration/simple-json-name/simple.ts index db9761249..3b9e779b3 100644 --- a/integration/simple-json-name/simple.ts +++ b/integration/simple-json-name/simple.ts @@ -68,7 +68,7 @@ export const Simple = { return obj; }, - fromPartial(object: DeepPartial): Simple { + fromPartial, I>>(object: I): Simple { const message = { ...baseSimple } as Simple; message.name = object.name ?? ''; message.age = object.age ?? undefined; @@ -78,6 +78,7 @@ export const Simple = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -88,6 +89,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/simple-long-string/google/protobuf/timestamp.ts b/integration/simple-long-string/google/protobuf/timestamp.ts index 0fc344c57..1ef0034f3 100644 --- a/integration/simple-long-string/google/protobuf/timestamp.ts +++ b/integration/simple-long-string/google/protobuf/timestamp.ts @@ -161,7 +161,7 @@ export const Timestamp = { return obj; }, - fromPartial(object: DeepPartial): Timestamp { + fromPartial, I>>(object: I): Timestamp { const message = { ...baseTimestamp } as Timestamp; message.seconds = object.seconds ?? '0'; message.nanos = object.nanos ?? 0; @@ -170,6 +170,7 @@ export const Timestamp = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -180,6 +181,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToString(long: Long) { return long.toString(); } diff --git a/integration/simple-long-string/google/protobuf/wrappers.ts b/integration/simple-long-string/google/protobuf/wrappers.ts index 1be69752e..e2af04a95 100644 --- a/integration/simple-long-string/google/protobuf/wrappers.ts +++ b/integration/simple-long-string/google/protobuf/wrappers.ts @@ -134,7 +134,7 @@ export const DoubleValue = { return obj; }, - fromPartial(object: DeepPartial): DoubleValue { + fromPartial, I>>(object: I): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; message.value = object.value ?? 0; return message; @@ -181,7 +181,7 @@ export const FloatValue = { return obj; }, - fromPartial(object: DeepPartial): FloatValue { + fromPartial, I>>(object: I): FloatValue { const message = { ...baseFloatValue } as FloatValue; message.value = object.value ?? 0; return message; @@ -228,7 +228,7 @@ export const Int64Value = { return obj; }, - fromPartial(object: DeepPartial): Int64Value { + fromPartial, I>>(object: I): Int64Value { const message = { ...baseInt64Value } as Int64Value; message.value = object.value ?? '0'; return message; @@ -275,7 +275,7 @@ export const UInt64Value = { return obj; }, - fromPartial(object: DeepPartial): UInt64Value { + fromPartial, I>>(object: I): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; message.value = object.value ?? '0'; return message; @@ -322,7 +322,7 @@ export const Int32Value = { return obj; }, - fromPartial(object: DeepPartial): Int32Value { + fromPartial, I>>(object: I): Int32Value { const message = { ...baseInt32Value } as Int32Value; message.value = object.value ?? 0; return message; @@ -369,7 +369,7 @@ export const UInt32Value = { return obj; }, - fromPartial(object: DeepPartial): UInt32Value { + fromPartial, I>>(object: I): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; message.value = object.value ?? 0; return message; @@ -416,7 +416,7 @@ export const BoolValue = { return obj; }, - fromPartial(object: DeepPartial): BoolValue { + fromPartial, I>>(object: I): BoolValue { const message = { ...baseBoolValue } as BoolValue; message.value = object.value ?? false; return message; @@ -463,7 +463,7 @@ export const StringValue = { return obj; }, - fromPartial(object: DeepPartial): StringValue { + fromPartial, I>>(object: I): StringValue { const message = { ...baseStringValue } as StringValue; message.value = object.value ?? ''; return message; @@ -513,7 +513,7 @@ export const BytesValue = { return obj; }, - fromPartial(object: DeepPartial): BytesValue { + fromPartial, I>>(object: I): BytesValue { const message = { ...baseBytesValue } as BytesValue; message.value = object.value ?? new Uint8Array(); return message; @@ -553,6 +553,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -563,6 +564,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToString(long: Long) { return long.toString(); } diff --git a/integration/simple-long-string/simple.ts b/integration/simple-long-string/simple.ts index a2cab3808..2136a5a19 100644 --- a/integration/simple-long-string/simple.ts +++ b/integration/simple-long-string/simple.ts @@ -181,7 +181,7 @@ export const Numbers = { return obj; }, - fromPartial(object: DeepPartial): Numbers { + fromPartial, I>>(object: I): Numbers { const message = { ...baseNumbers } as Numbers; message.double = object.double ?? 0; message.float = object.float ?? 0; @@ -202,6 +202,7 @@ export const Numbers = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -212,6 +213,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = Math.trunc(date.getTime() / 1_000).toString(); const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/simple-long/google/protobuf/wrappers.ts b/integration/simple-long/google/protobuf/wrappers.ts index 26ad3cd97..baaac22ba 100644 --- a/integration/simple-long/google/protobuf/wrappers.ts +++ b/integration/simple-long/google/protobuf/wrappers.ts @@ -134,7 +134,7 @@ export const DoubleValue = { return obj; }, - fromPartial(object: DeepPartial): DoubleValue { + fromPartial, I>>(object: I): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; message.value = object.value ?? 0; return message; @@ -181,7 +181,7 @@ export const FloatValue = { return obj; }, - fromPartial(object: DeepPartial): FloatValue { + fromPartial, I>>(object: I): FloatValue { const message = { ...baseFloatValue } as FloatValue; message.value = object.value ?? 0; return message; @@ -228,7 +228,7 @@ export const Int64Value = { return obj; }, - fromPartial(object: DeepPartial): Int64Value { + fromPartial, I>>(object: I): Int64Value { const message = { ...baseInt64Value } as Int64Value; message.value = object.value !== undefined && object.value !== null ? Long.fromValue(object.value) : Long.ZERO; return message; @@ -275,7 +275,7 @@ export const UInt64Value = { return obj; }, - fromPartial(object: DeepPartial): UInt64Value { + fromPartial, I>>(object: I): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; message.value = object.value !== undefined && object.value !== null ? Long.fromValue(object.value) : Long.UZERO; return message; @@ -322,7 +322,7 @@ export const Int32Value = { return obj; }, - fromPartial(object: DeepPartial): Int32Value { + fromPartial, I>>(object: I): Int32Value { const message = { ...baseInt32Value } as Int32Value; message.value = object.value ?? 0; return message; @@ -369,7 +369,7 @@ export const UInt32Value = { return obj; }, - fromPartial(object: DeepPartial): UInt32Value { + fromPartial, I>>(object: I): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; message.value = object.value ?? 0; return message; @@ -416,7 +416,7 @@ export const BoolValue = { return obj; }, - fromPartial(object: DeepPartial): BoolValue { + fromPartial, I>>(object: I): BoolValue { const message = { ...baseBoolValue } as BoolValue; message.value = object.value ?? false; return message; @@ -463,7 +463,7 @@ export const StringValue = { return obj; }, - fromPartial(object: DeepPartial): StringValue { + fromPartial, I>>(object: I): StringValue { const message = { ...baseStringValue } as StringValue; message.value = object.value ?? ''; return message; @@ -513,7 +513,7 @@ export const BytesValue = { return obj; }, - fromPartial(object: DeepPartial): BytesValue { + fromPartial, I>>(object: I): BytesValue { const message = { ...baseBytesValue } as BytesValue; message.value = object.value ?? new Uint8Array(); return message; @@ -553,6 +553,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Long @@ -565,6 +566,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/simple-long/simple.ts b/integration/simple-long/simple.ts index 87561d012..2dd555206 100644 --- a/integration/simple-long/simple.ts +++ b/integration/simple-long/simple.ts @@ -144,15 +144,15 @@ export const SimpleWithWrappers = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithWrappers { + fromPartial, I>>(object: I): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; message.name = object.name ?? undefined; message.age = object.age ?? undefined; message.enabled = object.enabled ?? undefined; message.bananas = object.bananas !== undefined && object.bananas !== null ? Long.fromValue(object.bananas) : undefined; - message.coins = (object.coins ?? []).map((e) => e); - message.snacks = (object.snacks ?? []).map((e) => e); + message.coins = object.coins?.map((e) => e) || []; + message.snacks = object.snacks?.map((e) => e) || []; return message; }, }; @@ -258,7 +258,7 @@ export const SimpleWithMap = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap { + fromPartial, I>>(object: I): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; message.nameLookup = Object.entries(object.nameLookup ?? {}).reduce<{ [key: string]: string }>( (acc, [key, value]) => { @@ -339,7 +339,9 @@ export const SimpleWithMap_NameLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_NameLookupEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_NameLookupEntry { const message = { ...baseSimpleWithMap_NameLookupEntry } as SimpleWithMap_NameLookupEntry; message.key = object.key ?? ''; message.value = object.value ?? ''; @@ -395,7 +397,7 @@ export const SimpleWithMap_IntLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_IntLookupEntry { + fromPartial, I>>(object: I): SimpleWithMap_IntLookupEntry { const message = { ...baseSimpleWithMap_IntLookupEntry } as SimpleWithMap_IntLookupEntry; message.key = object.key ?? 0; message.value = object.value ?? 0; @@ -451,7 +453,9 @@ export const SimpleWithMap_LongLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_LongLookupEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_LongLookupEntry { const message = { ...baseSimpleWithMap_LongLookupEntry } as SimpleWithMap_LongLookupEntry; message.key = object.key ?? ''; message.value = object.value !== undefined && object.value !== null ? Long.fromValue(object.value) : Long.ZERO; @@ -626,7 +630,7 @@ export const Numbers = { return obj; }, - fromPartial(object: DeepPartial): Numbers { + fromPartial, I>>(object: I): Numbers { const message = { ...baseNumbers } as Numbers; message.double = object.double ?? 0; message.float = object.float ?? 0; @@ -642,12 +646,13 @@ export const Numbers = { message.sfixed32 = object.sfixed32 ?? 0; message.sfixed64 = object.sfixed64 !== undefined && object.sfixed64 !== null ? Long.fromValue(object.sfixed64) : Long.ZERO; - message.manyUint64 = (object.manyUint64 ?? []).map((e) => Long.fromValue(e)); + message.manyUint64 = object.manyUint64?.map((e) => Long.fromValue(e)) || []; return message; }, }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Long @@ -660,6 +665,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/simple-optionals/google/protobuf/timestamp.ts b/integration/simple-optionals/google/protobuf/timestamp.ts index e4600a73e..0670c17fd 100644 --- a/integration/simple-optionals/google/protobuf/timestamp.ts +++ b/integration/simple-optionals/google/protobuf/timestamp.ts @@ -161,7 +161,7 @@ export const Timestamp = { return obj; }, - fromPartial(object: DeepPartial): Timestamp { + fromPartial, I>>(object: I): Timestamp { const message = { ...baseTimestamp } as Timestamp; message.seconds = object.seconds ?? 0; message.nanos = object.nanos ?? 0; @@ -181,6 +181,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -191,6 +192,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/simple-optionals/google/protobuf/wrappers.ts b/integration/simple-optionals/google/protobuf/wrappers.ts index 216285744..35b159990 100644 --- a/integration/simple-optionals/google/protobuf/wrappers.ts +++ b/integration/simple-optionals/google/protobuf/wrappers.ts @@ -134,7 +134,7 @@ export const DoubleValue = { return obj; }, - fromPartial(object: DeepPartial): DoubleValue { + fromPartial, I>>(object: I): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; message.value = object.value ?? 0; return message; @@ -181,7 +181,7 @@ export const FloatValue = { return obj; }, - fromPartial(object: DeepPartial): FloatValue { + fromPartial, I>>(object: I): FloatValue { const message = { ...baseFloatValue } as FloatValue; message.value = object.value ?? 0; return message; @@ -228,7 +228,7 @@ export const Int64Value = { return obj; }, - fromPartial(object: DeepPartial): Int64Value { + fromPartial, I>>(object: I): Int64Value { const message = { ...baseInt64Value } as Int64Value; message.value = object.value ?? 0; return message; @@ -275,7 +275,7 @@ export const UInt64Value = { return obj; }, - fromPartial(object: DeepPartial): UInt64Value { + fromPartial, I>>(object: I): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; message.value = object.value ?? 0; return message; @@ -322,7 +322,7 @@ export const Int32Value = { return obj; }, - fromPartial(object: DeepPartial): Int32Value { + fromPartial, I>>(object: I): Int32Value { const message = { ...baseInt32Value } as Int32Value; message.value = object.value ?? 0; return message; @@ -369,7 +369,7 @@ export const UInt32Value = { return obj; }, - fromPartial(object: DeepPartial): UInt32Value { + fromPartial, I>>(object: I): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; message.value = object.value ?? 0; return message; @@ -416,7 +416,7 @@ export const BoolValue = { return obj; }, - fromPartial(object: DeepPartial): BoolValue { + fromPartial, I>>(object: I): BoolValue { const message = { ...baseBoolValue } as BoolValue; message.value = object.value ?? false; return message; @@ -463,7 +463,7 @@ export const StringValue = { return obj; }, - fromPartial(object: DeepPartial): StringValue { + fromPartial, I>>(object: I): StringValue { const message = { ...baseStringValue } as StringValue; message.value = object.value ?? ''; return message; @@ -513,7 +513,7 @@ export const BytesValue = { return obj; }, - fromPartial(object: DeepPartial): BytesValue { + fromPartial, I>>(object: I): BytesValue { const message = { ...baseBytesValue } as BytesValue; message.value = object.value ?? new Uint8Array(); return message; @@ -553,6 +553,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -563,6 +564,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/simple-optionals/import_dir/thing.ts b/integration/simple-optionals/import_dir/thing.ts index 9dc4871d5..a1c6d81cf 100644 --- a/integration/simple-optionals/import_dir/thing.ts +++ b/integration/simple-optionals/import_dir/thing.ts @@ -50,7 +50,7 @@ export const ImportedThing = { return obj; }, - fromPartial(object: DeepPartial): ImportedThing { + fromPartial, I>>(object: I): ImportedThing { const message = { ...baseImportedThing } as ImportedThing; message.createdAt = object.createdAt ?? undefined; return message; @@ -58,6 +58,7 @@ export const ImportedThing = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -68,6 +69,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/simple-optionals/simple.ts b/integration/simple-optionals/simple.ts index 6108c6ad8..7d4bf0b3f 100644 --- a/integration/simple-optionals/simple.ts +++ b/integration/simple-optionals/simple.ts @@ -387,17 +387,17 @@ export const Simple = { return obj; }, - fromPartial(object: DeepPartial): Simple { + fromPartial, I>>(object: I): Simple { const message = { ...baseSimple } as Simple; message.name = object.name ?? ''; message.age = object.age ?? 0; message.createdAt = object.createdAt ?? undefined; message.child = object.child !== undefined && object.child !== null ? Child.fromPartial(object.child) : undefined; message.state = object.state ?? 0; - message.grandChildren = (object.grandChildren ?? []).map((e) => Child.fromPartial(e)); - message.coins = (object.coins ?? []).map((e) => e); - message.snacks = (object.snacks ?? []).map((e) => e); - message.oldStates = (object.oldStates ?? []).map((e) => e); + message.grandChildren = object.grandChildren?.map((e) => Child.fromPartial(e)) || []; + message.coins = object.coins?.map((e) => e) || []; + message.snacks = object.snacks?.map((e) => e) || []; + message.oldStates = object.oldStates?.map((e) => e) || []; message.thing = object.thing !== undefined && object.thing !== null ? ImportedThing.fromPartial(object.thing) : undefined; return message; @@ -452,7 +452,7 @@ export const Child = { return obj; }, - fromPartial(object: DeepPartial): Child { + fromPartial, I>>(object: I): Child { const message = { ...baseChild } as Child; message.name = object.name ?? ''; message.type = object.type ?? 0; @@ -520,7 +520,7 @@ export const Nested = { return obj; }, - fromPartial(object: DeepPartial): Nested { + fromPartial, I>>(object: I): Nested { const message = { ...baseNested } as Nested; message.name = object.name ?? ''; message.message = @@ -584,7 +584,7 @@ export const Nested_InnerMessage = { return obj; }, - fromPartial(object: DeepPartial): Nested_InnerMessage { + fromPartial, I>>(object: I): Nested_InnerMessage { const message = { ...baseNested_InnerMessage } as Nested_InnerMessage; message.name = object.name ?? ''; message.deep = @@ -635,7 +635,9 @@ export const Nested_InnerMessage_DeepMessage = { return obj; }, - fromPartial(object: DeepPartial): Nested_InnerMessage_DeepMessage { + fromPartial, I>>( + object: I + ): Nested_InnerMessage_DeepMessage { const message = { ...baseNested_InnerMessage_DeepMessage } as Nested_InnerMessage_DeepMessage; message.name = object.name ?? ''; return message; @@ -690,7 +692,7 @@ export const OneOfMessage = { return obj; }, - fromPartial(object: DeepPartial): OneOfMessage { + fromPartial, I>>(object: I): OneOfMessage { const message = { ...baseOneOfMessage } as OneOfMessage; message.first = object.first ?? undefined; message.last = object.last ?? undefined; @@ -780,13 +782,13 @@ export const SimpleWithWrappers = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithWrappers { + fromPartial, I>>(object: I): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; message.name = object.name ?? undefined; message.age = object.age ?? undefined; message.enabled = object.enabled ?? undefined; - message.coins = (object.coins ?? []).map((e) => e); - message.snacks = (object.snacks ?? []).map((e) => e); + message.coins = object.coins?.map((e) => e) || []; + message.snacks = object.snacks?.map((e) => e) || []; return message; }, }; @@ -831,7 +833,7 @@ export const Entity = { return obj; }, - fromPartial(object: DeepPartial): Entity { + fromPartial, I>>(object: I): Entity { const message = { ...baseEntity } as Entity; message.id = object.id ?? 0; return message; @@ -939,7 +941,7 @@ export const SimpleWithMap = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap { + fromPartial, I>>(object: I): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { @@ -1020,7 +1022,9 @@ export const SimpleWithMap_EntitiesByIdEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_EntitiesByIdEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_EntitiesByIdEntry { const message = { ...baseSimpleWithMap_EntitiesByIdEntry } as SimpleWithMap_EntitiesByIdEntry; message.key = object.key ?? 0; message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; @@ -1076,7 +1080,9 @@ export const SimpleWithMap_NameLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_NameLookupEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_NameLookupEntry { const message = { ...baseSimpleWithMap_NameLookupEntry } as SimpleWithMap_NameLookupEntry; message.key = object.key ?? ''; message.value = object.value ?? ''; @@ -1132,7 +1138,7 @@ export const SimpleWithMap_IntLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_IntLookupEntry { + fromPartial, I>>(object: I): SimpleWithMap_IntLookupEntry { const message = { ...baseSimpleWithMap_IntLookupEntry } as SimpleWithMap_IntLookupEntry; message.key = object.key ?? 0; message.value = object.value ?? 0; @@ -1195,7 +1201,7 @@ export const SimpleWithSnakeCaseMap = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap { + fromPartial, I>>(object: I): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { @@ -1258,7 +1264,9 @@ export const SimpleWithSnakeCaseMap_EntitiesByIdEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap_EntitiesByIdEntry { + fromPartial, I>>( + object: I + ): SimpleWithSnakeCaseMap_EntitiesByIdEntry { const message = { ...baseSimpleWithSnakeCaseMap_EntitiesByIdEntry } as SimpleWithSnakeCaseMap_EntitiesByIdEntry; message.key = object.key ?? 0; message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; @@ -1306,7 +1314,7 @@ export const PingRequest = { return obj; }, - fromPartial(object: DeepPartial): PingRequest { + fromPartial, I>>(object: I): PingRequest { const message = { ...basePingRequest } as PingRequest; message.input = object.input ?? ''; return message; @@ -1353,7 +1361,7 @@ export const PingResponse = { return obj; }, - fromPartial(object: DeepPartial): PingResponse { + fromPartial, I>>(object: I): PingResponse { const message = { ...basePingResponse } as PingResponse; message.output = object.output ?? ''; return message; @@ -1501,7 +1509,7 @@ export const Numbers = { return obj; }, - fromPartial(object: DeepPartial): Numbers { + fromPartial, I>>(object: I): Numbers { const message = { ...baseNumbers } as Numbers; message.double = object.double ?? 0; message.float = object.float ?? 0; @@ -1552,6 +1560,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -1562,6 +1571,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/simple-optionals/thing.ts b/integration/simple-optionals/thing.ts index 2ce036f8c..6f19102e9 100644 --- a/integration/simple-optionals/thing.ts +++ b/integration/simple-optionals/thing.ts @@ -50,7 +50,7 @@ export const ImportedThing = { return obj; }, - fromPartial(object: DeepPartial): ImportedThing { + fromPartial, I>>(object: I): ImportedThing { const message = { ...baseImportedThing } as ImportedThing; message.createdAt = object.createdAt ?? undefined; return message; @@ -58,6 +58,7 @@ export const ImportedThing = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -68,6 +69,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/simple-proto2/simple.ts b/integration/simple-proto2/simple.ts index a02b86c7c..88faab1db 100644 --- a/integration/simple-proto2/simple.ts +++ b/integration/simple-proto2/simple.ts @@ -80,7 +80,7 @@ export const Issue56 = { return obj; }, - fromPartial(object: DeepPartial): Issue56 { + fromPartial, I>>(object: I): Issue56 { const message = { ...baseIssue56 } as Issue56; message.test = object.test ?? 1; return message; @@ -88,6 +88,7 @@ export const Issue56 = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -98,6 +99,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/simple-snake/google/protobuf/timestamp.ts b/integration/simple-snake/google/protobuf/timestamp.ts index e4600a73e..0670c17fd 100644 --- a/integration/simple-snake/google/protobuf/timestamp.ts +++ b/integration/simple-snake/google/protobuf/timestamp.ts @@ -161,7 +161,7 @@ export const Timestamp = { return obj; }, - fromPartial(object: DeepPartial): Timestamp { + fromPartial, I>>(object: I): Timestamp { const message = { ...baseTimestamp } as Timestamp; message.seconds = object.seconds ?? 0; message.nanos = object.nanos ?? 0; @@ -181,6 +181,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -191,6 +192,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/simple-snake/google/protobuf/wrappers.ts b/integration/simple-snake/google/protobuf/wrappers.ts index 216285744..35b159990 100644 --- a/integration/simple-snake/google/protobuf/wrappers.ts +++ b/integration/simple-snake/google/protobuf/wrappers.ts @@ -134,7 +134,7 @@ export const DoubleValue = { return obj; }, - fromPartial(object: DeepPartial): DoubleValue { + fromPartial, I>>(object: I): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; message.value = object.value ?? 0; return message; @@ -181,7 +181,7 @@ export const FloatValue = { return obj; }, - fromPartial(object: DeepPartial): FloatValue { + fromPartial, I>>(object: I): FloatValue { const message = { ...baseFloatValue } as FloatValue; message.value = object.value ?? 0; return message; @@ -228,7 +228,7 @@ export const Int64Value = { return obj; }, - fromPartial(object: DeepPartial): Int64Value { + fromPartial, I>>(object: I): Int64Value { const message = { ...baseInt64Value } as Int64Value; message.value = object.value ?? 0; return message; @@ -275,7 +275,7 @@ export const UInt64Value = { return obj; }, - fromPartial(object: DeepPartial): UInt64Value { + fromPartial, I>>(object: I): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; message.value = object.value ?? 0; return message; @@ -322,7 +322,7 @@ export const Int32Value = { return obj; }, - fromPartial(object: DeepPartial): Int32Value { + fromPartial, I>>(object: I): Int32Value { const message = { ...baseInt32Value } as Int32Value; message.value = object.value ?? 0; return message; @@ -369,7 +369,7 @@ export const UInt32Value = { return obj; }, - fromPartial(object: DeepPartial): UInt32Value { + fromPartial, I>>(object: I): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; message.value = object.value ?? 0; return message; @@ -416,7 +416,7 @@ export const BoolValue = { return obj; }, - fromPartial(object: DeepPartial): BoolValue { + fromPartial, I>>(object: I): BoolValue { const message = { ...baseBoolValue } as BoolValue; message.value = object.value ?? false; return message; @@ -463,7 +463,7 @@ export const StringValue = { return obj; }, - fromPartial(object: DeepPartial): StringValue { + fromPartial, I>>(object: I): StringValue { const message = { ...baseStringValue } as StringValue; message.value = object.value ?? ''; return message; @@ -513,7 +513,7 @@ export const BytesValue = { return obj; }, - fromPartial(object: DeepPartial): BytesValue { + fromPartial, I>>(object: I): BytesValue { const message = { ...baseBytesValue } as BytesValue; message.value = object.value ?? new Uint8Array(); return message; @@ -553,6 +553,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -563,6 +564,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/simple-snake/import_dir/thing.ts b/integration/simple-snake/import_dir/thing.ts index 3a013304a..a5e21f0c5 100644 --- a/integration/simple-snake/import_dir/thing.ts +++ b/integration/simple-snake/import_dir/thing.ts @@ -50,7 +50,7 @@ export const ImportedThing = { return obj; }, - fromPartial(object: DeepPartial): ImportedThing { + fromPartial, I>>(object: I): ImportedThing { const message = { ...baseImportedThing } as ImportedThing; message.created_at = object.created_at ?? undefined; return message; @@ -58,6 +58,7 @@ export const ImportedThing = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -68,6 +69,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/simple-snake/simple.ts b/integration/simple-snake/simple.ts index ca2844a71..2f1d77fe9 100644 --- a/integration/simple-snake/simple.ts +++ b/integration/simple-snake/simple.ts @@ -387,17 +387,17 @@ export const Simple = { return obj; }, - fromPartial(object: DeepPartial): Simple { + fromPartial, I>>(object: I): Simple { const message = { ...baseSimple } as Simple; message.name = object.name ?? ''; message.age = object.age ?? 0; message.created_at = object.created_at ?? undefined; message.child = object.child !== undefined && object.child !== null ? Child.fromPartial(object.child) : undefined; message.state = object.state ?? 0; - message.grand_children = (object.grand_children ?? []).map((e) => Child.fromPartial(e)); - message.coins = (object.coins ?? []).map((e) => e); - message.snacks = (object.snacks ?? []).map((e) => e); - message.old_states = (object.old_states ?? []).map((e) => e); + message.grand_children = object.grand_children?.map((e) => Child.fromPartial(e)) || []; + message.coins = object.coins?.map((e) => e) || []; + message.snacks = object.snacks?.map((e) => e) || []; + message.old_states = object.old_states?.map((e) => e) || []; message.thing = object.thing !== undefined && object.thing !== null ? ImportedThing.fromPartial(object.thing) : undefined; return message; @@ -452,7 +452,7 @@ export const Child = { return obj; }, - fromPartial(object: DeepPartial): Child { + fromPartial, I>>(object: I): Child { const message = { ...baseChild } as Child; message.name = object.name ?? ''; message.type = object.type ?? 0; @@ -520,7 +520,7 @@ export const Nested = { return obj; }, - fromPartial(object: DeepPartial): Nested { + fromPartial, I>>(object: I): Nested { const message = { ...baseNested } as Nested; message.name = object.name ?? ''; message.message = @@ -584,7 +584,7 @@ export const Nested_InnerMessage = { return obj; }, - fromPartial(object: DeepPartial): Nested_InnerMessage { + fromPartial, I>>(object: I): Nested_InnerMessage { const message = { ...baseNested_InnerMessage } as Nested_InnerMessage; message.name = object.name ?? ''; message.deep = @@ -635,7 +635,9 @@ export const Nested_InnerMessage_DeepMessage = { return obj; }, - fromPartial(object: DeepPartial): Nested_InnerMessage_DeepMessage { + fromPartial, I>>( + object: I + ): Nested_InnerMessage_DeepMessage { const message = { ...baseNested_InnerMessage_DeepMessage } as Nested_InnerMessage_DeepMessage; message.name = object.name ?? ''; return message; @@ -690,7 +692,7 @@ export const OneOfMessage = { return obj; }, - fromPartial(object: DeepPartial): OneOfMessage { + fromPartial, I>>(object: I): OneOfMessage { const message = { ...baseOneOfMessage } as OneOfMessage; message.first = object.first ?? undefined; message.last = object.last ?? undefined; @@ -780,13 +782,13 @@ export const SimpleWithWrappers = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithWrappers { + fromPartial, I>>(object: I): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; message.name = object.name ?? undefined; message.age = object.age ?? undefined; message.enabled = object.enabled ?? undefined; - message.coins = (object.coins ?? []).map((e) => e); - message.snacks = (object.snacks ?? []).map((e) => e); + message.coins = object.coins?.map((e) => e) || []; + message.snacks = object.snacks?.map((e) => e) || []; return message; }, }; @@ -831,7 +833,7 @@ export const Entity = { return obj; }, - fromPartial(object: DeepPartial): Entity { + fromPartial, I>>(object: I): Entity { const message = { ...baseEntity } as Entity; message.id = object.id ?? 0; return message; @@ -939,7 +941,7 @@ export const SimpleWithMap = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap { + fromPartial, I>>(object: I): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { @@ -1020,7 +1022,9 @@ export const SimpleWithMap_EntitiesByIdEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_EntitiesByIdEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_EntitiesByIdEntry { const message = { ...baseSimpleWithMap_EntitiesByIdEntry } as SimpleWithMap_EntitiesByIdEntry; message.key = object.key ?? 0; message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; @@ -1076,7 +1080,9 @@ export const SimpleWithMap_NameLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_NameLookupEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_NameLookupEntry { const message = { ...baseSimpleWithMap_NameLookupEntry } as SimpleWithMap_NameLookupEntry; message.key = object.key ?? ''; message.value = object.value ?? ''; @@ -1132,7 +1138,7 @@ export const SimpleWithMap_IntLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_IntLookupEntry { + fromPartial, I>>(object: I): SimpleWithMap_IntLookupEntry { const message = { ...baseSimpleWithMap_IntLookupEntry } as SimpleWithMap_IntLookupEntry; message.key = object.key ?? 0; message.value = object.value ?? 0; @@ -1195,7 +1201,7 @@ export const SimpleWithSnakeCaseMap = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap { + fromPartial, I>>(object: I): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; message.entities_by_id = Object.entries(object.entities_by_id ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { @@ -1258,7 +1264,9 @@ export const SimpleWithSnakeCaseMap_EntitiesByIdEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap_EntitiesByIdEntry { + fromPartial, I>>( + object: I + ): SimpleWithSnakeCaseMap_EntitiesByIdEntry { const message = { ...baseSimpleWithSnakeCaseMap_EntitiesByIdEntry } as SimpleWithSnakeCaseMap_EntitiesByIdEntry; message.key = object.key ?? 0; message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; @@ -1306,7 +1314,7 @@ export const PingRequest = { return obj; }, - fromPartial(object: DeepPartial): PingRequest { + fromPartial, I>>(object: I): PingRequest { const message = { ...basePingRequest } as PingRequest; message.input = object.input ?? ''; return message; @@ -1353,7 +1361,7 @@ export const PingResponse = { return obj; }, - fromPartial(object: DeepPartial): PingResponse { + fromPartial, I>>(object: I): PingResponse { const message = { ...basePingResponse } as PingResponse; message.output = object.output ?? ''; return message; @@ -1501,7 +1509,7 @@ export const Numbers = { return obj; }, - fromPartial(object: DeepPartial): Numbers { + fromPartial, I>>(object: I): Numbers { const message = { ...baseNumbers } as Numbers; message.double = object.double ?? 0; message.float = object.float ?? 0; @@ -1552,6 +1560,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -1562,6 +1571,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/simple-string-enums/simple.ts b/integration/simple-string-enums/simple.ts index 655d8d239..41b0b1ee3 100644 --- a/integration/simple-string-enums/simple.ts +++ b/integration/simple-string-enums/simple.ts @@ -132,16 +132,17 @@ export const Simple = { return obj; }, - fromPartial(object: DeepPartial): Simple { + fromPartial, I>>(object: I): Simple { const message = { ...baseSimple } as Simple; message.name = object.name ?? ''; message.state = object.state ?? StateEnum.UNKNOWN; - message.states = (object.states ?? []).map((e) => e); + message.states = object.states?.map((e) => e) || []; return message; }, }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -152,6 +153,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts b/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts index e4600a73e..0670c17fd 100644 --- a/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts +++ b/integration/simple-unrecognized-enum/google/protobuf/timestamp.ts @@ -161,7 +161,7 @@ export const Timestamp = { return obj; }, - fromPartial(object: DeepPartial): Timestamp { + fromPartial, I>>(object: I): Timestamp { const message = { ...baseTimestamp } as Timestamp; message.seconds = object.seconds ?? 0; message.nanos = object.nanos ?? 0; @@ -181,6 +181,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -191,6 +192,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts b/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts index 216285744..35b159990 100644 --- a/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts +++ b/integration/simple-unrecognized-enum/google/protobuf/wrappers.ts @@ -134,7 +134,7 @@ export const DoubleValue = { return obj; }, - fromPartial(object: DeepPartial): DoubleValue { + fromPartial, I>>(object: I): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; message.value = object.value ?? 0; return message; @@ -181,7 +181,7 @@ export const FloatValue = { return obj; }, - fromPartial(object: DeepPartial): FloatValue { + fromPartial, I>>(object: I): FloatValue { const message = { ...baseFloatValue } as FloatValue; message.value = object.value ?? 0; return message; @@ -228,7 +228,7 @@ export const Int64Value = { return obj; }, - fromPartial(object: DeepPartial): Int64Value { + fromPartial, I>>(object: I): Int64Value { const message = { ...baseInt64Value } as Int64Value; message.value = object.value ?? 0; return message; @@ -275,7 +275,7 @@ export const UInt64Value = { return obj; }, - fromPartial(object: DeepPartial): UInt64Value { + fromPartial, I>>(object: I): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; message.value = object.value ?? 0; return message; @@ -322,7 +322,7 @@ export const Int32Value = { return obj; }, - fromPartial(object: DeepPartial): Int32Value { + fromPartial, I>>(object: I): Int32Value { const message = { ...baseInt32Value } as Int32Value; message.value = object.value ?? 0; return message; @@ -369,7 +369,7 @@ export const UInt32Value = { return obj; }, - fromPartial(object: DeepPartial): UInt32Value { + fromPartial, I>>(object: I): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; message.value = object.value ?? 0; return message; @@ -416,7 +416,7 @@ export const BoolValue = { return obj; }, - fromPartial(object: DeepPartial): BoolValue { + fromPartial, I>>(object: I): BoolValue { const message = { ...baseBoolValue } as BoolValue; message.value = object.value ?? false; return message; @@ -463,7 +463,7 @@ export const StringValue = { return obj; }, - fromPartial(object: DeepPartial): StringValue { + fromPartial, I>>(object: I): StringValue { const message = { ...baseStringValue } as StringValue; message.value = object.value ?? ''; return message; @@ -513,7 +513,7 @@ export const BytesValue = { return obj; }, - fromPartial(object: DeepPartial): BytesValue { + fromPartial, I>>(object: I): BytesValue { const message = { ...baseBytesValue } as BytesValue; message.value = object.value ?? new Uint8Array(); return message; @@ -553,6 +553,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -563,6 +564,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/simple-unrecognized-enum/import_dir/thing.ts b/integration/simple-unrecognized-enum/import_dir/thing.ts index 276e91e85..6a1771dcc 100644 --- a/integration/simple-unrecognized-enum/import_dir/thing.ts +++ b/integration/simple-unrecognized-enum/import_dir/thing.ts @@ -50,7 +50,7 @@ export const ImportedThing = { return obj; }, - fromPartial(object: DeepPartial): ImportedThing { + fromPartial, I>>(object: I): ImportedThing { const message = { ...baseImportedThing } as ImportedThing; message.createdAt = object.createdAt ?? undefined; return message; @@ -58,6 +58,7 @@ export const ImportedThing = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -68,6 +69,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/simple-unrecognized-enum/simple.ts b/integration/simple-unrecognized-enum/simple.ts index d5d6ac01b..9cd1b6cc1 100644 --- a/integration/simple-unrecognized-enum/simple.ts +++ b/integration/simple-unrecognized-enum/simple.ts @@ -378,17 +378,17 @@ export const Simple = { return obj; }, - fromPartial(object: DeepPartial): Simple { + fromPartial, I>>(object: I): Simple { const message = { ...baseSimple } as Simple; message.name = object.name ?? ''; message.age = object.age ?? 0; message.createdAt = object.createdAt ?? undefined; message.child = object.child !== undefined && object.child !== null ? Child.fromPartial(object.child) : undefined; message.state = object.state ?? 0; - message.grandChildren = (object.grandChildren ?? []).map((e) => Child.fromPartial(e)); - message.coins = (object.coins ?? []).map((e) => e); - message.snacks = (object.snacks ?? []).map((e) => e); - message.oldStates = (object.oldStates ?? []).map((e) => e); + message.grandChildren = object.grandChildren?.map((e) => Child.fromPartial(e)) || []; + message.coins = object.coins?.map((e) => e) || []; + message.snacks = object.snacks?.map((e) => e) || []; + message.oldStates = object.oldStates?.map((e) => e) || []; message.thing = object.thing !== undefined && object.thing !== null ? ImportedThing.fromPartial(object.thing) : undefined; return message; @@ -443,7 +443,7 @@ export const Child = { return obj; }, - fromPartial(object: DeepPartial): Child { + fromPartial, I>>(object: I): Child { const message = { ...baseChild } as Child; message.name = object.name ?? ''; message.type = object.type ?? 0; @@ -511,7 +511,7 @@ export const Nested = { return obj; }, - fromPartial(object: DeepPartial): Nested { + fromPartial, I>>(object: I): Nested { const message = { ...baseNested } as Nested; message.name = object.name ?? ''; message.message = @@ -575,7 +575,7 @@ export const Nested_InnerMessage = { return obj; }, - fromPartial(object: DeepPartial): Nested_InnerMessage { + fromPartial, I>>(object: I): Nested_InnerMessage { const message = { ...baseNested_InnerMessage } as Nested_InnerMessage; message.name = object.name ?? ''; message.deep = @@ -626,7 +626,9 @@ export const Nested_InnerMessage_DeepMessage = { return obj; }, - fromPartial(object: DeepPartial): Nested_InnerMessage_DeepMessage { + fromPartial, I>>( + object: I + ): Nested_InnerMessage_DeepMessage { const message = { ...baseNested_InnerMessage_DeepMessage } as Nested_InnerMessage_DeepMessage; message.name = object.name ?? ''; return message; @@ -681,7 +683,7 @@ export const OneOfMessage = { return obj; }, - fromPartial(object: DeepPartial): OneOfMessage { + fromPartial, I>>(object: I): OneOfMessage { const message = { ...baseOneOfMessage } as OneOfMessage; message.first = object.first ?? undefined; message.last = object.last ?? undefined; @@ -771,13 +773,13 @@ export const SimpleWithWrappers = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithWrappers { + fromPartial, I>>(object: I): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; message.name = object.name ?? undefined; message.age = object.age ?? undefined; message.enabled = object.enabled ?? undefined; - message.coins = (object.coins ?? []).map((e) => e); - message.snacks = (object.snacks ?? []).map((e) => e); + message.coins = object.coins?.map((e) => e) || []; + message.snacks = object.snacks?.map((e) => e) || []; return message; }, }; @@ -822,7 +824,7 @@ export const Entity = { return obj; }, - fromPartial(object: DeepPartial): Entity { + fromPartial, I>>(object: I): Entity { const message = { ...baseEntity } as Entity; message.id = object.id ?? 0; return message; @@ -930,7 +932,7 @@ export const SimpleWithMap = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap { + fromPartial, I>>(object: I): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { @@ -1011,7 +1013,9 @@ export const SimpleWithMap_EntitiesByIdEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_EntitiesByIdEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_EntitiesByIdEntry { const message = { ...baseSimpleWithMap_EntitiesByIdEntry } as SimpleWithMap_EntitiesByIdEntry; message.key = object.key ?? 0; message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; @@ -1067,7 +1071,9 @@ export const SimpleWithMap_NameLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_NameLookupEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_NameLookupEntry { const message = { ...baseSimpleWithMap_NameLookupEntry } as SimpleWithMap_NameLookupEntry; message.key = object.key ?? ''; message.value = object.value ?? ''; @@ -1123,7 +1129,7 @@ export const SimpleWithMap_IntLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_IntLookupEntry { + fromPartial, I>>(object: I): SimpleWithMap_IntLookupEntry { const message = { ...baseSimpleWithMap_IntLookupEntry } as SimpleWithMap_IntLookupEntry; message.key = object.key ?? 0; message.value = object.value ?? 0; @@ -1186,7 +1192,7 @@ export const SimpleWithSnakeCaseMap = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap { + fromPartial, I>>(object: I): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { @@ -1249,7 +1255,9 @@ export const SimpleWithSnakeCaseMap_EntitiesByIdEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap_EntitiesByIdEntry { + fromPartial, I>>( + object: I + ): SimpleWithSnakeCaseMap_EntitiesByIdEntry { const message = { ...baseSimpleWithSnakeCaseMap_EntitiesByIdEntry } as SimpleWithSnakeCaseMap_EntitiesByIdEntry; message.key = object.key ?? 0; message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; @@ -1297,7 +1305,7 @@ export const PingRequest = { return obj; }, - fromPartial(object: DeepPartial): PingRequest { + fromPartial, I>>(object: I): PingRequest { const message = { ...basePingRequest } as PingRequest; message.input = object.input ?? ''; return message; @@ -1344,7 +1352,7 @@ export const PingResponse = { return obj; }, - fromPartial(object: DeepPartial): PingResponse { + fromPartial, I>>(object: I): PingResponse { const message = { ...basePingResponse } as PingResponse; message.output = object.output ?? ''; return message; @@ -1492,7 +1500,7 @@ export const Numbers = { return obj; }, - fromPartial(object: DeepPartial): Numbers { + fromPartial, I>>(object: I): Numbers { const message = { ...baseNumbers } as Numbers; message.double = object.double ?? 0; message.float = object.float ?? 0; @@ -1543,6 +1551,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -1553,6 +1562,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/simple/google/protobuf/timestamp.ts b/integration/simple/google/protobuf/timestamp.ts index e4600a73e..0670c17fd 100644 --- a/integration/simple/google/protobuf/timestamp.ts +++ b/integration/simple/google/protobuf/timestamp.ts @@ -161,7 +161,7 @@ export const Timestamp = { return obj; }, - fromPartial(object: DeepPartial): Timestamp { + fromPartial, I>>(object: I): Timestamp { const message = { ...baseTimestamp } as Timestamp; message.seconds = object.seconds ?? 0; message.nanos = object.nanos ?? 0; @@ -181,6 +181,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -191,6 +192,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/simple/google/protobuf/wrappers.ts b/integration/simple/google/protobuf/wrappers.ts index 216285744..35b159990 100644 --- a/integration/simple/google/protobuf/wrappers.ts +++ b/integration/simple/google/protobuf/wrappers.ts @@ -134,7 +134,7 @@ export const DoubleValue = { return obj; }, - fromPartial(object: DeepPartial): DoubleValue { + fromPartial, I>>(object: I): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; message.value = object.value ?? 0; return message; @@ -181,7 +181,7 @@ export const FloatValue = { return obj; }, - fromPartial(object: DeepPartial): FloatValue { + fromPartial, I>>(object: I): FloatValue { const message = { ...baseFloatValue } as FloatValue; message.value = object.value ?? 0; return message; @@ -228,7 +228,7 @@ export const Int64Value = { return obj; }, - fromPartial(object: DeepPartial): Int64Value { + fromPartial, I>>(object: I): Int64Value { const message = { ...baseInt64Value } as Int64Value; message.value = object.value ?? 0; return message; @@ -275,7 +275,7 @@ export const UInt64Value = { return obj; }, - fromPartial(object: DeepPartial): UInt64Value { + fromPartial, I>>(object: I): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; message.value = object.value ?? 0; return message; @@ -322,7 +322,7 @@ export const Int32Value = { return obj; }, - fromPartial(object: DeepPartial): Int32Value { + fromPartial, I>>(object: I): Int32Value { const message = { ...baseInt32Value } as Int32Value; message.value = object.value ?? 0; return message; @@ -369,7 +369,7 @@ export const UInt32Value = { return obj; }, - fromPartial(object: DeepPartial): UInt32Value { + fromPartial, I>>(object: I): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; message.value = object.value ?? 0; return message; @@ -416,7 +416,7 @@ export const BoolValue = { return obj; }, - fromPartial(object: DeepPartial): BoolValue { + fromPartial, I>>(object: I): BoolValue { const message = { ...baseBoolValue } as BoolValue; message.value = object.value ?? false; return message; @@ -463,7 +463,7 @@ export const StringValue = { return obj; }, - fromPartial(object: DeepPartial): StringValue { + fromPartial, I>>(object: I): StringValue { const message = { ...baseStringValue } as StringValue; message.value = object.value ?? ''; return message; @@ -513,7 +513,7 @@ export const BytesValue = { return obj; }, - fromPartial(object: DeepPartial): BytesValue { + fromPartial, I>>(object: I): BytesValue { const message = { ...baseBytesValue } as BytesValue; message.value = object.value ?? new Uint8Array(); return message; @@ -553,6 +553,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -563,6 +564,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/simple/google/type/date.ts b/integration/simple/google/type/date.ts index 88b392a00..d29072413 100644 --- a/integration/simple/google/type/date.ts +++ b/integration/simple/google/type/date.ts @@ -91,7 +91,7 @@ export const DateMessage = { return obj; }, - fromPartial(object: DeepPartial): DateMessage { + fromPartial, I>>(object: I): DateMessage { const message = { ...baseDateMessage } as DateMessage; message.year = object.year ?? 0; message.month = object.month ?? 0; @@ -101,6 +101,7 @@ export const DateMessage = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -111,6 +112,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/simple/import_dir/thing.ts b/integration/simple/import_dir/thing.ts index 276e91e85..6a1771dcc 100644 --- a/integration/simple/import_dir/thing.ts +++ b/integration/simple/import_dir/thing.ts @@ -50,7 +50,7 @@ export const ImportedThing = { return obj; }, - fromPartial(object: DeepPartial): ImportedThing { + fromPartial, I>>(object: I): ImportedThing { const message = { ...baseImportedThing } as ImportedThing; message.createdAt = object.createdAt ?? undefined; return message; @@ -58,6 +58,7 @@ export const ImportedThing = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -68,6 +69,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/simple/simple-test.ts b/integration/simple/simple-test.ts index f87be3048..e57c21cad 100644 --- a/integration/simple/simple-test.ts +++ b/integration/simple/simple-test.ts @@ -320,6 +320,13 @@ describe('simple', () => { `); }); + it('has fromPartial uses exact types', () => { + const s1 = Simple.fromPartial({ + // @ts-expect-error + grandChildren: ['a', 'b'].map((name) => ({ name, typ: null })), + }); + }); + it('can fromPartial on maps with falsey values', () => { const s1 = SimpleWithMap.fromPartial({ intLookup: { 1: 2, 2: 0 }, diff --git a/integration/simple/simple.ts b/integration/simple/simple.ts index 93d273821..19369e188 100644 --- a/integration/simple/simple.ts +++ b/integration/simple/simple.ts @@ -475,20 +475,20 @@ export const Simple = { return obj; }, - fromPartial(object: DeepPartial): Simple { + fromPartial, I>>(object: I): Simple { const message = { ...baseSimple } as Simple; message.name = object.name ?? ''; message.age = object.age ?? 0; message.createdAt = object.createdAt ?? undefined; message.child = object.child !== undefined && object.child !== null ? Child.fromPartial(object.child) : undefined; message.state = object.state ?? 0; - message.grandChildren = (object.grandChildren ?? []).map((e) => Child.fromPartial(e)); - message.coins = (object.coins ?? []).map((e) => e); - message.snacks = (object.snacks ?? []).map((e) => e); - message.oldStates = (object.oldStates ?? []).map((e) => e); + message.grandChildren = object.grandChildren?.map((e) => Child.fromPartial(e)) || []; + message.coins = object.coins?.map((e) => e) || []; + message.snacks = object.snacks?.map((e) => e) || []; + message.oldStates = object.oldStates?.map((e) => e) || []; message.thing = object.thing !== undefined && object.thing !== null ? ImportedThing.fromPartial(object.thing) : undefined; - message.blobs = (object.blobs ?? []).map((e) => e); + message.blobs = object.blobs?.map((e) => e) || []; message.birthday = object.birthday !== undefined && object.birthday !== null ? DateMessage.fromPartial(object.birthday) : undefined; message.blob = object.blob ?? new Uint8Array(); @@ -544,7 +544,7 @@ export const Child = { return obj; }, - fromPartial(object: DeepPartial): Child { + fromPartial, I>>(object: I): Child { const message = { ...baseChild } as Child; message.name = object.name ?? ''; message.type = object.type ?? 0; @@ -612,7 +612,7 @@ export const Nested = { return obj; }, - fromPartial(object: DeepPartial): Nested { + fromPartial, I>>(object: I): Nested { const message = { ...baseNested } as Nested; message.name = object.name ?? ''; message.message = @@ -676,7 +676,7 @@ export const Nested_InnerMessage = { return obj; }, - fromPartial(object: DeepPartial): Nested_InnerMessage { + fromPartial, I>>(object: I): Nested_InnerMessage { const message = { ...baseNested_InnerMessage } as Nested_InnerMessage; message.name = object.name ?? ''; message.deep = @@ -727,7 +727,9 @@ export const Nested_InnerMessage_DeepMessage = { return obj; }, - fromPartial(object: DeepPartial): Nested_InnerMessage_DeepMessage { + fromPartial, I>>( + object: I + ): Nested_InnerMessage_DeepMessage { const message = { ...baseNested_InnerMessage_DeepMessage } as Nested_InnerMessage_DeepMessage; message.name = object.name ?? ''; return message; @@ -782,7 +784,7 @@ export const OneOfMessage = { return obj; }, - fromPartial(object: DeepPartial): OneOfMessage { + fromPartial, I>>(object: I): OneOfMessage { const message = { ...baseOneOfMessage } as OneOfMessage; message.first = object.first ?? undefined; message.last = object.last ?? undefined; @@ -880,13 +882,13 @@ export const SimpleWithWrappers = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithWrappers { + fromPartial, I>>(object: I): SimpleWithWrappers { const message = { ...baseSimpleWithWrappers } as SimpleWithWrappers; message.name = object.name ?? undefined; message.age = object.age ?? undefined; message.enabled = object.enabled ?? undefined; - message.coins = (object.coins ?? []).map((e) => e); - message.snacks = (object.snacks ?? []).map((e) => e); + message.coins = object.coins?.map((e) => e) || []; + message.snacks = object.snacks?.map((e) => e) || []; message.id = object.id ?? undefined; return message; }, @@ -932,7 +934,7 @@ export const Entity = { return obj; }, - fromPartial(object: DeepPartial): Entity { + fromPartial, I>>(object: I): Entity { const message = { ...baseEntity } as Entity; message.id = object.id ?? 0; return message; @@ -1133,7 +1135,7 @@ export const SimpleWithMap = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap { + fromPartial, I>>(object: I): SimpleWithMap { const message = { ...baseSimpleWithMap } as SimpleWithMap; message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { @@ -1249,7 +1251,9 @@ export const SimpleWithMap_EntitiesByIdEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_EntitiesByIdEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_EntitiesByIdEntry { const message = { ...baseSimpleWithMap_EntitiesByIdEntry } as SimpleWithMap_EntitiesByIdEntry; message.key = object.key ?? 0; message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; @@ -1305,7 +1309,9 @@ export const SimpleWithMap_NameLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_NameLookupEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_NameLookupEntry { const message = { ...baseSimpleWithMap_NameLookupEntry } as SimpleWithMap_NameLookupEntry; message.key = object.key ?? ''; message.value = object.value ?? ''; @@ -1361,7 +1367,7 @@ export const SimpleWithMap_IntLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_IntLookupEntry { + fromPartial, I>>(object: I): SimpleWithMap_IntLookupEntry { const message = { ...baseSimpleWithMap_IntLookupEntry } as SimpleWithMap_IntLookupEntry; message.key = object.key ?? 0; message.value = object.value ?? 0; @@ -1417,7 +1423,9 @@ export const SimpleWithMap_MapOfTimestampsEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_MapOfTimestampsEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_MapOfTimestampsEntry { const message = { ...baseSimpleWithMap_MapOfTimestampsEntry } as SimpleWithMap_MapOfTimestampsEntry; message.key = object.key ?? ''; message.value = object.value ?? undefined; @@ -1476,7 +1484,9 @@ export const SimpleWithMap_MapOfBytesEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_MapOfBytesEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_MapOfBytesEntry { const message = { ...baseSimpleWithMap_MapOfBytesEntry } as SimpleWithMap_MapOfBytesEntry; message.key = object.key ?? ''; message.value = object.value ?? new Uint8Array(); @@ -1532,7 +1542,9 @@ export const SimpleWithMap_MapOfStringValuesEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_MapOfStringValuesEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_MapOfStringValuesEntry { const message = { ...baseSimpleWithMap_MapOfStringValuesEntry } as SimpleWithMap_MapOfStringValuesEntry; message.key = object.key ?? ''; message.value = object.value ?? undefined; @@ -1588,7 +1600,9 @@ export const SimpleWithMap_LongLookupEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMap_LongLookupEntry { + fromPartial, I>>( + object: I + ): SimpleWithMap_LongLookupEntry { const message = { ...baseSimpleWithMap_LongLookupEntry } as SimpleWithMap_LongLookupEntry; message.key = object.key ?? 0; message.value = object.value ?? 0; @@ -1651,7 +1665,7 @@ export const SimpleWithSnakeCaseMap = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap { + fromPartial, I>>(object: I): SimpleWithSnakeCaseMap { const message = { ...baseSimpleWithSnakeCaseMap } as SimpleWithSnakeCaseMap; message.entitiesById = Object.entries(object.entitiesById ?? {}).reduce<{ [key: number]: Entity }>( (acc, [key, value]) => { @@ -1714,7 +1728,9 @@ export const SimpleWithSnakeCaseMap_EntitiesByIdEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithSnakeCaseMap_EntitiesByIdEntry { + fromPartial, I>>( + object: I + ): SimpleWithSnakeCaseMap_EntitiesByIdEntry { const message = { ...baseSimpleWithSnakeCaseMap_EntitiesByIdEntry } as SimpleWithSnakeCaseMap_EntitiesByIdEntry; message.key = object.key ?? 0; message.value = object.value !== undefined && object.value !== null ? Entity.fromPartial(object.value) : undefined; @@ -1777,7 +1793,7 @@ export const SimpleWithMapOfEnums = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMapOfEnums { + fromPartial, I>>(object: I): SimpleWithMapOfEnums { const message = { ...baseSimpleWithMapOfEnums } as SimpleWithMapOfEnums; message.enumsById = Object.entries(object.enumsById ?? {}).reduce<{ [key: number]: StateEnum }>( (acc, [key, value]) => { @@ -1840,7 +1856,9 @@ export const SimpleWithMapOfEnums_EnumsByIdEntry = { return obj; }, - fromPartial(object: DeepPartial): SimpleWithMapOfEnums_EnumsByIdEntry { + fromPartial, I>>( + object: I + ): SimpleWithMapOfEnums_EnumsByIdEntry { const message = { ...baseSimpleWithMapOfEnums_EnumsByIdEntry } as SimpleWithMapOfEnums_EnumsByIdEntry; message.key = object.key ?? 0; message.value = object.value ?? 0; @@ -1888,7 +1906,7 @@ export const PingRequest = { return obj; }, - fromPartial(object: DeepPartial): PingRequest { + fromPartial, I>>(object: I): PingRequest { const message = { ...basePingRequest } as PingRequest; message.input = object.input ?? ''; return message; @@ -1935,7 +1953,7 @@ export const PingResponse = { return obj; }, - fromPartial(object: DeepPartial): PingResponse { + fromPartial, I>>(object: I): PingResponse { const message = { ...basePingResponse } as PingResponse; message.output = object.output ?? ''; return message; @@ -2083,7 +2101,7 @@ export const Numbers = { return obj; }, - fromPartial(object: DeepPartial): Numbers { + fromPartial, I>>(object: I): Numbers { const message = { ...baseNumbers } as Numbers; message.double = object.double ?? 0; message.float = object.float ?? 0; @@ -2194,7 +2212,7 @@ export const SimpleButOptional = { return obj; }, - fromPartial(object: DeepPartial): SimpleButOptional { + fromPartial, I>>(object: I): SimpleButOptional { const message = { ...baseSimpleButOptional } as SimpleButOptional; message.name = object.name ?? undefined; message.age = object.age ?? undefined; @@ -2241,7 +2259,7 @@ export const Empty = { return obj; }, - fromPartial(_: DeepPartial): Empty { + fromPartial, I>>(_: I): Empty { const message = { ...baseEmpty } as Empty; return message; }, @@ -2301,6 +2319,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -2311,6 +2330,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/struct/google/protobuf/struct.ts b/integration/struct/google/protobuf/struct.ts index a2b7d6f54..28fe05424 100644 --- a/integration/struct/google/protobuf/struct.ts +++ b/integration/struct/google/protobuf/struct.ts @@ -147,7 +147,7 @@ export const Struct = { return obj; }, - fromPartial(object: DeepPartial): Struct { + fromPartial, I>>(object: I): Struct { const message = { ...baseStruct } as Struct; message.fields = Object.entries(object.fields ?? {}).reduce<{ [key: string]: any | undefined }>( (acc, [key, value]) => { @@ -228,7 +228,7 @@ export const Struct_FieldsEntry = { return obj; }, - fromPartial(object: DeepPartial): Struct_FieldsEntry { + fromPartial, I>>(object: I): Struct_FieldsEntry { const message = { ...baseStruct_FieldsEntry } as Struct_FieldsEntry; message.key = object.key ?? ''; message.value = object.value ?? undefined; @@ -321,7 +321,7 @@ export const Value = { return obj; }, - fromPartial(object: DeepPartial): Value { + fromPartial, I>>(object: I): Value { const message = { ...baseValue } as Value; message.nullValue = object.nullValue ?? undefined; message.numberValue = object.numberValue ?? undefined; @@ -414,9 +414,9 @@ export const ListValue = { return obj; }, - fromPartial(object: DeepPartial): ListValue { + fromPartial, I>>(object: I): ListValue { const message = { ...baseListValue } as ListValue; - message.values = (object.values ?? []).map((e) => e); + message.values = object.values?.map((e) => e) || []; return message; }, @@ -430,6 +430,7 @@ export const ListValue = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -440,6 +441,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/struct/struct.ts b/integration/struct/struct.ts index b2bf7dbc4..7ed1f3ed5 100644 --- a/integration/struct/struct.ts +++ b/integration/struct/struct.ts @@ -49,7 +49,7 @@ export const StructMessage = { return obj; }, - fromPartial(object: DeepPartial): StructMessage { + fromPartial, I>>(object: I): StructMessage { const message = { ...baseStructMessage } as StructMessage; message.value = object.value ?? undefined; return message; @@ -57,6 +57,7 @@ export const StructMessage = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -67,6 +68,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/type-registry/bar/bar.ts b/integration/type-registry/bar/bar.ts index 5df3681c5..60cafe6fa 100644 --- a/integration/type-registry/bar/bar.ts +++ b/integration/type-registry/bar/bar.ts @@ -53,7 +53,7 @@ export const Bar = { return obj; }, - fromPartial(object: DeepPartial): Bar { + fromPartial, I>>(object: I): Bar { const message = { ...baseBar } as Bar; message.foo = object.foo !== undefined && object.foo !== null ? Foo.fromPartial(object.foo) : undefined; return message; @@ -63,6 +63,7 @@ export const Bar = { messageTypeRegistry.set(Bar.$type, Bar); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -73,6 +74,11 @@ export type DeepPartial = T extends Builtin ? { [K in Exclude]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record | '$type'>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/type-registry/foo.ts b/integration/type-registry/foo.ts index 1f7edbc2e..2f9d88546 100644 --- a/integration/type-registry/foo.ts +++ b/integration/type-registry/foo.ts @@ -59,7 +59,7 @@ export const Foo = { return obj; }, - fromPartial(object: DeepPartial): Foo { + fromPartial, I>>(object: I): Foo { const message = { ...baseFoo } as Foo; message.timestamp = object.timestamp ?? undefined; return message; @@ -111,7 +111,7 @@ export const Foo2 = { return obj; }, - fromPartial(object: DeepPartial): Foo2 { + fromPartial, I>>(object: I): Foo2 { const message = { ...baseFoo2 } as Foo2; message.timestamp = object.timestamp ?? undefined; return message; @@ -121,6 +121,7 @@ export const Foo2 = { messageTypeRegistry.set(Foo2.$type, Foo2); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -131,6 +132,11 @@ export type DeepPartial = T extends Builtin ? { [K in Exclude]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record | '$type'>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/type-registry/google/protobuf/timestamp.ts b/integration/type-registry/google/protobuf/timestamp.ts index 2f723335c..17a482643 100644 --- a/integration/type-registry/google/protobuf/timestamp.ts +++ b/integration/type-registry/google/protobuf/timestamp.ts @@ -165,7 +165,7 @@ export const Timestamp = { return obj; }, - fromPartial(object: DeepPartial): Timestamp { + fromPartial, I>>(object: I): Timestamp { const message = { ...baseTimestamp } as Timestamp; message.seconds = object.seconds ?? 0; message.nanos = object.nanos ?? 0; @@ -187,6 +187,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -197,6 +198,11 @@ export type DeepPartial = T extends Builtin ? { [K in Exclude]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record | '$type'>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/types-with-underscores/file.ts b/integration/types-with-underscores/file.ts index 1b9aff8cb..d74b8eb1f 100644 --- a/integration/types-with-underscores/file.ts +++ b/integration/types-with-underscores/file.ts @@ -50,7 +50,7 @@ export const Baz = { return obj; }, - fromPartial(object: DeepPartial): Baz { + fromPartial, I>>(object: I): Baz { const message = { ...baseBaz } as Baz; message.foo = object.foo !== undefined && object.foo !== null ? FooBar.fromPartial(object.foo) : undefined; return message; @@ -89,13 +89,14 @@ export const FooBar = { return obj; }, - fromPartial(_: DeepPartial): FooBar { + fromPartial, I>>(_: I): FooBar { const message = { ...baseFooBar } as FooBar; return message; }, }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -106,6 +107,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/use-date-false/google/protobuf/timestamp.ts b/integration/use-date-false/google/protobuf/timestamp.ts index e4600a73e..0670c17fd 100644 --- a/integration/use-date-false/google/protobuf/timestamp.ts +++ b/integration/use-date-false/google/protobuf/timestamp.ts @@ -161,7 +161,7 @@ export const Timestamp = { return obj; }, - fromPartial(object: DeepPartial): Timestamp { + fromPartial, I>>(object: I): Timestamp { const message = { ...baseTimestamp } as Timestamp; message.seconds = object.seconds ?? 0; message.nanos = object.nanos ?? 0; @@ -181,6 +181,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -191,6 +192,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/use-date-false/metadata.ts b/integration/use-date-false/metadata.ts index 1005399b6..e97264378 100644 --- a/integration/use-date-false/metadata.ts +++ b/integration/use-date-false/metadata.ts @@ -50,7 +50,7 @@ export const Metadata = { return obj; }, - fromPartial(object: DeepPartial): Metadata { + fromPartial, I>>(object: I): Metadata { const message = { ...baseMetadata } as Metadata; message.lastEdited = object.lastEdited !== undefined && object.lastEdited !== null @@ -61,6 +61,7 @@ export const Metadata = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -71,6 +72,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/use-date-string/google/protobuf/timestamp.ts b/integration/use-date-string/google/protobuf/timestamp.ts index e4600a73e..0670c17fd 100644 --- a/integration/use-date-string/google/protobuf/timestamp.ts +++ b/integration/use-date-string/google/protobuf/timestamp.ts @@ -161,7 +161,7 @@ export const Timestamp = { return obj; }, - fromPartial(object: DeepPartial): Timestamp { + fromPartial, I>>(object: I): Timestamp { const message = { ...baseTimestamp } as Timestamp; message.seconds = object.seconds ?? 0; message.nanos = object.nanos ?? 0; @@ -181,6 +181,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -191,6 +192,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/use-date-string/use-date-string.ts b/integration/use-date-string/use-date-string.ts index d7e15656f..84b08b4cc 100644 --- a/integration/use-date-string/use-date-string.ts +++ b/integration/use-date-string/use-date-string.ts @@ -114,11 +114,11 @@ export const Todo = { return obj; }, - fromPartial(object: DeepPartial): Todo { + fromPartial, I>>(object: I): Todo { const message = { ...baseTodo } as Todo; message.id = object.id ?? ''; message.timestamp = object.timestamp ?? undefined; - message.repeatedTimestamp = (object.repeatedTimestamp ?? []).map((e) => e); + message.repeatedTimestamp = object.repeatedTimestamp?.map((e) => e) || []; message.optionalTimestamp = object.optionalTimestamp ?? undefined; message.mapOfTimestamps = Object.entries(object.mapOfTimestamps ?? {}).reduce<{ [key: string]: string }>( (acc, [key, value]) => { @@ -181,7 +181,7 @@ export const Todo_MapOfTimestampsEntry = { return obj; }, - fromPartial(object: DeepPartial): Todo_MapOfTimestampsEntry { + fromPartial, I>>(object: I): Todo_MapOfTimestampsEntry { const message = { ...baseTodo_MapOfTimestampsEntry } as Todo_MapOfTimestampsEntry; message.key = object.key ?? ''; message.value = object.value ?? undefined; @@ -190,6 +190,7 @@ export const Todo_MapOfTimestampsEntry = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -200,6 +201,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(dateStr: string): Timestamp { const date = new Date(dateStr); const seconds = date.getTime() / 1_000; diff --git a/integration/use-date-true/google/protobuf/timestamp.ts b/integration/use-date-true/google/protobuf/timestamp.ts index e4600a73e..0670c17fd 100644 --- a/integration/use-date-true/google/protobuf/timestamp.ts +++ b/integration/use-date-true/google/protobuf/timestamp.ts @@ -161,7 +161,7 @@ export const Timestamp = { return obj; }, - fromPartial(object: DeepPartial): Timestamp { + fromPartial, I>>(object: I): Timestamp { const message = { ...baseTimestamp } as Timestamp; message.seconds = object.seconds ?? 0; message.nanos = object.nanos ?? 0; @@ -181,6 +181,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -191,6 +192,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/use-date-true/use-date-true.ts b/integration/use-date-true/use-date-true.ts index 172e251c7..2ce6aa5b6 100644 --- a/integration/use-date-true/use-date-true.ts +++ b/integration/use-date-true/use-date-true.ts @@ -114,11 +114,11 @@ export const Todo = { return obj; }, - fromPartial(object: DeepPartial): Todo { + fromPartial, I>>(object: I): Todo { const message = { ...baseTodo } as Todo; message.id = object.id ?? ''; message.timestamp = object.timestamp ?? undefined; - message.repeatedTimestamp = (object.repeatedTimestamp ?? []).map((e) => e); + message.repeatedTimestamp = object.repeatedTimestamp?.map((e) => e) || []; message.optionalTimestamp = object.optionalTimestamp ?? undefined; message.mapOfTimestamps = Object.entries(object.mapOfTimestamps ?? {}).reduce<{ [key: string]: Date }>( (acc, [key, value]) => { @@ -181,7 +181,7 @@ export const Todo_MapOfTimestampsEntry = { return obj; }, - fromPartial(object: DeepPartial): Todo_MapOfTimestampsEntry { + fromPartial, I>>(object: I): Todo_MapOfTimestampsEntry { const message = { ...baseTodo_MapOfTimestampsEntry } as Todo_MapOfTimestampsEntry; message.key = object.key ?? ''; message.value = object.value ?? undefined; @@ -190,6 +190,7 @@ export const Todo_MapOfTimestampsEntry = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -200,6 +201,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function toTimestamp(date: Date): Timestamp { const seconds = date.getTime() / 1_000; const nanos = (date.getTime() % 1_000) * 1_000_000; diff --git a/integration/value/google/protobuf/struct.ts b/integration/value/google/protobuf/struct.ts index a2b7d6f54..28fe05424 100644 --- a/integration/value/google/protobuf/struct.ts +++ b/integration/value/google/protobuf/struct.ts @@ -147,7 +147,7 @@ export const Struct = { return obj; }, - fromPartial(object: DeepPartial): Struct { + fromPartial, I>>(object: I): Struct { const message = { ...baseStruct } as Struct; message.fields = Object.entries(object.fields ?? {}).reduce<{ [key: string]: any | undefined }>( (acc, [key, value]) => { @@ -228,7 +228,7 @@ export const Struct_FieldsEntry = { return obj; }, - fromPartial(object: DeepPartial): Struct_FieldsEntry { + fromPartial, I>>(object: I): Struct_FieldsEntry { const message = { ...baseStruct_FieldsEntry } as Struct_FieldsEntry; message.key = object.key ?? ''; message.value = object.value ?? undefined; @@ -321,7 +321,7 @@ export const Value = { return obj; }, - fromPartial(object: DeepPartial): Value { + fromPartial, I>>(object: I): Value { const message = { ...baseValue } as Value; message.nullValue = object.nullValue ?? undefined; message.numberValue = object.numberValue ?? undefined; @@ -414,9 +414,9 @@ export const ListValue = { return obj; }, - fromPartial(object: DeepPartial): ListValue { + fromPartial, I>>(object: I): ListValue { const message = { ...baseListValue } as ListValue; - message.values = (object.values ?? []).map((e) => e); + message.values = object.values?.map((e) => e) || []; return message; }, @@ -430,6 +430,7 @@ export const ListValue = { }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -440,6 +441,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/value/google/protobuf/wrappers.ts b/integration/value/google/protobuf/wrappers.ts index 216285744..35b159990 100644 --- a/integration/value/google/protobuf/wrappers.ts +++ b/integration/value/google/protobuf/wrappers.ts @@ -134,7 +134,7 @@ export const DoubleValue = { return obj; }, - fromPartial(object: DeepPartial): DoubleValue { + fromPartial, I>>(object: I): DoubleValue { const message = { ...baseDoubleValue } as DoubleValue; message.value = object.value ?? 0; return message; @@ -181,7 +181,7 @@ export const FloatValue = { return obj; }, - fromPartial(object: DeepPartial): FloatValue { + fromPartial, I>>(object: I): FloatValue { const message = { ...baseFloatValue } as FloatValue; message.value = object.value ?? 0; return message; @@ -228,7 +228,7 @@ export const Int64Value = { return obj; }, - fromPartial(object: DeepPartial): Int64Value { + fromPartial, I>>(object: I): Int64Value { const message = { ...baseInt64Value } as Int64Value; message.value = object.value ?? 0; return message; @@ -275,7 +275,7 @@ export const UInt64Value = { return obj; }, - fromPartial(object: DeepPartial): UInt64Value { + fromPartial, I>>(object: I): UInt64Value { const message = { ...baseUInt64Value } as UInt64Value; message.value = object.value ?? 0; return message; @@ -322,7 +322,7 @@ export const Int32Value = { return obj; }, - fromPartial(object: DeepPartial): Int32Value { + fromPartial, I>>(object: I): Int32Value { const message = { ...baseInt32Value } as Int32Value; message.value = object.value ?? 0; return message; @@ -369,7 +369,7 @@ export const UInt32Value = { return obj; }, - fromPartial(object: DeepPartial): UInt32Value { + fromPartial, I>>(object: I): UInt32Value { const message = { ...baseUInt32Value } as UInt32Value; message.value = object.value ?? 0; return message; @@ -416,7 +416,7 @@ export const BoolValue = { return obj; }, - fromPartial(object: DeepPartial): BoolValue { + fromPartial, I>>(object: I): BoolValue { const message = { ...baseBoolValue } as BoolValue; message.value = object.value ?? false; return message; @@ -463,7 +463,7 @@ export const StringValue = { return obj; }, - fromPartial(object: DeepPartial): StringValue { + fromPartial, I>>(object: I): StringValue { const message = { ...baseStringValue } as StringValue; message.value = object.value ?? ''; return message; @@ -513,7 +513,7 @@ export const BytesValue = { return obj; }, - fromPartial(object: DeepPartial): BytesValue { + fromPartial, I>>(object: I): BytesValue { const message = { ...baseBytesValue } as BytesValue; message.value = object.value ?? new Uint8Array(); return message; @@ -553,6 +553,7 @@ function base64FromBytes(arr: Uint8Array): string { } type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -563,6 +564,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/integration/value/value.ts b/integration/value/value.ts index 0b7df9ffb..80f89d0d6 100644 --- a/integration/value/value.ts +++ b/integration/value/value.ts @@ -72,16 +72,17 @@ export const ValueMessage = { return obj; }, - fromPartial(object: DeepPartial): ValueMessage { + fromPartial, I>>(object: I): ValueMessage { const message = { ...baseValueMessage } as ValueMessage; message.value = object.value ?? undefined; message.anyList = object.anyList ?? undefined; - message.repeatedAny = (object.repeatedAny ?? []).map((e) => e); + message.repeatedAny = object.repeatedAny?.map((e) => e) || []; return message; }, }; type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -92,6 +93,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + // If you get a compile-error about 'Constructor and ... have no overlap', // add '--ts_proto_opt=esModuleInterop=true' as a flag when calling 'protoc'. if (util.Long !== Long) { diff --git a/integration/vector-tile/vector_tile.ts b/integration/vector-tile/vector_tile.ts index 703a38a34..3ad16effe 100644 --- a/integration/vector-tile/vector_tile.ts +++ b/integration/vector-tile/vector_tile.ts @@ -123,9 +123,9 @@ export const Tile = { return obj; }, - fromPartial(object: DeepPartial): Tile { + fromPartial, I>>(object: I): Tile { const message = { ...baseTile } as Tile; - message.layers = (object.layers ?? []).map((e) => Tile_Layer.fromPartial(e)); + message.layers = object.layers?.map((e) => Tile_Layer.fromPartial(e)) || []; return message; }, }; @@ -228,7 +228,7 @@ export const Tile_Value = { return obj; }, - fromPartial(object: DeepPartial): Tile_Value { + fromPartial, I>>(object: I): Tile_Value { const message = { ...baseTile_Value } as Tile_Value; message.stringValue = object.stringValue ?? ''; message.floatValue = object.floatValue ?? 0; @@ -333,12 +333,12 @@ export const Tile_Feature = { return obj; }, - fromPartial(object: DeepPartial): Tile_Feature { + fromPartial, I>>(object: I): Tile_Feature { const message = { ...baseTile_Feature } as Tile_Feature; message.id = object.id ?? 0; - message.tags = (object.tags ?? []).map((e) => e); + message.tags = object.tags?.map((e) => e) || []; message.type = object.type ?? 0; - message.geometry = (object.geometry ?? []).map((e) => e); + message.geometry = object.geometry?.map((e) => e) || []; return message; }, }; @@ -438,13 +438,13 @@ export const Tile_Layer = { return obj; }, - fromPartial(object: DeepPartial): Tile_Layer { + fromPartial, I>>(object: I): Tile_Layer { const message = { ...baseTile_Layer } as Tile_Layer; message.version = object.version ?? 0; message.name = object.name ?? ''; - message.features = (object.features ?? []).map((e) => Tile_Feature.fromPartial(e)); - message.keys = (object.keys ?? []).map((e) => e); - message.values = (object.values ?? []).map((e) => Tile_Value.fromPartial(e)); + message.features = object.features?.map((e) => Tile_Feature.fromPartial(e)) || []; + message.keys = object.keys?.map((e) => e) || []; + message.values = object.values?.map((e) => Tile_Value.fromPartial(e)) || []; message.extent = object.extent ?? 0; return message; }, @@ -462,6 +462,7 @@ var globalThis: any = (() => { })(); type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; + export type DeepPartial = T extends Builtin ? T : T extends Array @@ -472,6 +473,11 @@ export type DeepPartial = T extends Builtin ? { [K in keyof T]?: DeepPartial } : Partial; +type KeysOfUnion = T extends T ? keyof T : never; +export type Exact = P extends Builtin + ? P + : P & { [K in keyof P]: Exact } & Record>, never>; + function longToNumber(long: Long): number { if (long.gt(Number.MAX_SAFE_INTEGER)) { throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER'); diff --git a/src/generate-type-registry.ts b/src/generate-type-registry.ts index 4f1c0a7a8..1ee8fd999 100644 --- a/src/generate-type-registry.ts +++ b/src/generate-type-registry.ts @@ -17,7 +17,7 @@ export function generateTypeRegistry(ctx: Context): Code { export const messageTypeRegistry = new Map(); `); - chunks.push(code`${ctx.utils.DeepPartial.ifUsed}`); + chunks.push(code` ${ctx.utils.Builtin.ifUsed} ${ctx.utils.DeepPartial.ifUsed}`); return joinCode(chunks, { on: '\n\n' }); } diff --git a/src/main.ts b/src/main.ts index 8b7ddb02c..0f1fa60d3 100644 --- a/src/main.ts +++ b/src/main.ts @@ -417,14 +417,31 @@ function makeDeepPartial(options: Options, longs: ReturnType` : code`keyof T`; + + const Builtin = conditionalOutput( + 'Builtin', + code`type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined;` + ); + + // Based on https://github.com/sindresorhus/type-fest/pull/259 + const maybeExcludeType = options.outputTypeRegistry ? `| '$type'` : ''; + const Exact = conditionalOutput( + 'Exact', + code` + type KeysOfUnion = T extends T ? keyof T : never; + ${maybeExport} type Exact = P extends ${Builtin} + ? P + : P & + { [K in keyof P]: Exact } & Record ${maybeExcludeType}>, never>; + ` + ); // Based on the type from ts-essentials + const keys = options.outputTypeRegistry ? code`Exclude` : code`keyof T`; const DeepPartial = conditionalOutput( 'DeepPartial', code` - type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; - ${maybeExport} type DeepPartial = T extends Builtin + ${maybeExport} type DeepPartial = T extends ${Builtin} ? T ${maybeLong} : T extends Array @@ -437,7 +454,7 @@ function makeDeepPartial(options: Options, longs: ReturnType) { @@ -1187,8 +1204,9 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri const Timestamp = imp('Timestamp@./google/protobuf/timestamp'); // create the basic function declaration + const paramName = messageDesc.field.length > 0 ? 'object' : '_'; chunks.push(code` - fromPartial(${messageDesc.field.length > 0 ? 'object' : '_'}: ${utils.DeepPartial}<${fullName}>): ${fullName} { + fromPartial, I>>(${paramName}: I): ${fullName} { const message = { ...base${fullName} } as ${fullName}; `); @@ -1258,7 +1276,7 @@ function generateFromPartial(ctx: Context, fullName: string, messageDesc: Descri `); } else { chunks.push(code` - message.${fieldName} = (object.${fieldName} ?? []).map((e) => ${readSnippet('e')}); + message.${fieldName} = object.${fieldName}?.map((e) => ${readSnippet('e')}) || []; `); } } else if (isWithinOneOfThatShouldBeUnion(options, field)) {