diff --git a/integration/extensions/test.ts b/integration/extensions/test.ts index 5a6e28b78..1e63b8770 100644 --- a/integration/extensions/test.ts +++ b/integration/extensions/test.ts @@ -508,7 +508,7 @@ export const long: Extension = { packed: false, encode: (value: Long): Uint8Array[] => { const encoded: Uint8Array[] = []; - if (value !== undefined && !value.equals(undefined)) { + if (value !== undefined && value !== undefined) { const writer = new BinaryWriter(); writer.int64(value.toString()); encoded.push(writer.finish()); @@ -528,7 +528,7 @@ export const fixed: Extension = { packed: false, encode: (value: Long): Uint8Array[] => { const encoded: Uint8Array[] = []; - if (value !== undefined && !value.equals(undefined)) { + if (value !== undefined && value !== undefined) { const writer = new BinaryWriter(); writer.fixed64(value.toString()); encoded.push(writer.finish()); diff --git a/integration/map-long-optional/test.ts b/integration/map-long-optional/test.ts index 6fc11cc7f..9ac5b4f8b 100644 --- a/integration/map-long-optional/test.ts +++ b/integration/map-long-optional/test.ts @@ -126,10 +126,10 @@ function createBaseMapBigInt_MapEntry(): MapBigInt_MapEntry { export const MapBigInt_MapEntry: MessageFns = { encode(message: MapBigInt_MapEntry, writer: BinaryWriter = new BinaryWriter()): BinaryWriter { - if (!message.key.equals(undefined)) { + if (message.key !== undefined) { writer.uint32(9).fixed64(message.key.toString()); } - if (!message.value.equals(undefined)) { + if (message.value !== undefined) { writer.uint32(16).int64(message.value.toString()); } if (message._unknownFields !== undefined) { @@ -196,10 +196,10 @@ export const MapBigInt_MapEntry: MessageFns = { toJSON(message: MapBigInt_MapEntry): unknown { const obj: any = {}; - if (!message.key.equals(undefined)) { + if (message.key !== undefined) { obj.key = (message.key || undefined).toString(); } - if (!message.value.equals(undefined)) { + if (message.value !== undefined) { obj.value = (message.value || undefined).toString(); } return obj; diff --git a/integration/proto2-long/simple.ts b/integration/proto2-long/simple.ts index bbaa57933..1cef110d0 100644 --- a/integration/proto2-long/simple.ts +++ b/integration/proto2-long/simple.ts @@ -93,7 +93,8 @@ export interface OptionalsTest_TranslationsEntry { value: string; } -export interface Child {} +export interface Child { +} function createBaseOptionalsTest(): OptionalsTest { return { @@ -184,7 +185,7 @@ export const OptionalsTest: MessageFns = { if (message.optState !== undefined && message.optState !== 0) { writer.uint32(104).int32(message.optState); } - if (message.optLong !== undefined && !message.optLong.equals(undefined)) { + if (message.optLong !== undefined && message.optLong !== undefined) { writer.uint32(112).int64(message.optLong.toString()); } if (message.optTruth !== undefined && message.optTruth !== undefined) { @@ -208,7 +209,7 @@ export const OptionalsTest: MessageFns = { if (message.reqState !== 0) { writer.uint32(184).int32(message.reqState); } - if (!message.reqLong.equals(Long.ZERO)) { + if (message.reqLong !== Long.ZERO) { writer.uint32(192).int64(message.reqLong.toString()); } if (message.reqTruth !== false) { @@ -229,7 +230,7 @@ export const OptionalsTest: MessageFns = { if (message.reqDefvalState !== 2) { writer.uint32(264).int32(message.reqDefvalState); } - if (!message.reqDefvalLong.equals(Long.fromNumber(7812378193))) { + if (message.reqDefvalLong !== Long.fromNumber(7812378193)) { writer.uint32(272).int64(message.reqDefvalLong.toString()); } if (message.reqDefvalTruth !== true) { @@ -250,7 +251,7 @@ export const OptionalsTest: MessageFns = { if (message.optDefvalState !== undefined && message.optDefvalState !== 0) { writer.uint32(344).int32(message.optDefvalState); } - if (message.optDefvalLong !== undefined && !message.optDefvalLong.equals(undefined)) { + if (message.optDefvalLong !== undefined && message.optDefvalLong !== undefined) { writer.uint32(352).int64(message.optDefvalLong.toString()); } if (message.optDefvalTruth !== undefined && message.optDefvalTruth !== undefined) { @@ -702,9 +703,9 @@ export const OptionalsTest: MessageFns = { optDefvalFloat: isSet(object.optDefvalFloat) ? globalThis.Number(object.optDefvalFloat) : undefined, translations: isObject(object.translations) ? Object.entries(object.translations).reduce<{ [key: string]: string }>((acc, [key, value]) => { - acc[key] = String(value); - return acc; - }, {}) + acc[key] = String(value); + return acc; + }, {}) : {}, }; }, @@ -744,7 +745,7 @@ export const OptionalsTest: MessageFns = { if (message.optState !== undefined && message.optState !== 0) { obj.optState = stateEnumToJSON(message.optState); } - if (message.optLong !== undefined && !message.optLong.equals(undefined)) { + if (message.optLong !== undefined && message.optLong !== undefined) { obj.optLong = (message.optLong || undefined).toString(); } if (message.optTruth !== undefined && message.optTruth !== undefined) { @@ -768,7 +769,7 @@ export const OptionalsTest: MessageFns = { if (message.reqState !== 0) { obj.reqState = stateEnumToJSON(message.reqState); } - if (!message.reqLong.equals(Long.ZERO)) { + if (message.reqLong !== Long.ZERO) { obj.reqLong = (message.reqLong || Long.ZERO).toString(); } if (message.reqTruth !== false) { @@ -789,7 +790,7 @@ export const OptionalsTest: MessageFns = { if (message.reqDefvalState !== 2) { obj.reqDefvalState = stateEnumToJSON(message.reqDefvalState); } - if (!message.reqDefvalLong.equals(Long.fromNumber(7812378193))) { + if (message.reqDefvalLong !== Long.fromNumber(7812378193)) { obj.reqDefvalLong = (message.reqDefvalLong || Long.fromNumber(7812378193)).toString(); } if (message.reqDefvalTruth !== true) { @@ -810,7 +811,7 @@ export const OptionalsTest: MessageFns = { if (message.optDefvalState !== undefined && message.optDefvalState !== 0) { obj.optDefvalState = stateEnumToJSON(message.optDefvalState); } - if (message.optDefvalLong !== undefined && !message.optDefvalLong.equals(undefined)) { + if (message.optDefvalLong !== undefined && message.optDefvalLong !== undefined) { obj.optDefvalLong = (message.optDefvalLong || undefined).toString(); } if (message.optDefvalTruth !== undefined && message.optDefvalTruth !== undefined) { @@ -851,43 +852,43 @@ export const OptionalsTest: MessageFns = { message.repData = object.repData?.map((e) => e) || []; message.repFloat = object.repFloat?.map((e) => e) || []; message.optId = object.optId ?? undefined; - message.optChild = - object.optChild !== undefined && object.optChild !== null ? Child.fromPartial(object.optChild) : undefined; + message.optChild = (object.optChild !== undefined && object.optChild !== null) + ? Child.fromPartial(object.optChild) + : undefined; message.optState = object.optState ?? undefined; - message.optLong = - object.optLong !== undefined && object.optLong !== null ? Long.fromValue(object.optLong) : undefined; + message.optLong = (object.optLong !== undefined && object.optLong !== null) + ? Long.fromValue(object.optLong) + : undefined; message.optTruth = object.optTruth ?? undefined; message.optDescription = object.optDescription ?? undefined; message.optData = object.optData ?? undefined; message.optFloat = object.optFloat ?? undefined; message.reqId = object.reqId ?? 0; - message.reqChild = - object.reqChild !== undefined && object.reqChild !== null - ? Child.fromPartial(object.reqChild) - : createBaseChild(); + message.reqChild = (object.reqChild !== undefined && object.reqChild !== null) + ? Child.fromPartial(object.reqChild) + : createBaseChild(); message.reqState = object.reqState ?? 0; - message.reqLong = - object.reqLong !== undefined && object.reqLong !== null ? Long.fromValue(object.reqLong) : Long.ZERO; + message.reqLong = (object.reqLong !== undefined && object.reqLong !== null) + ? Long.fromValue(object.reqLong) + : Long.ZERO; message.reqTruth = object.reqTruth ?? false; message.reqDescription = object.reqDescription ?? ""; message.reqData = object.reqData ?? new Uint8Array(0); message.reqFloat = object.reqFloat ?? 0; message.reqDefvalId = object.reqDefvalId ?? 100; message.reqDefvalState = object.reqDefvalState ?? 2; - message.reqDefvalLong = - object.reqDefvalLong !== undefined && object.reqDefvalLong !== null - ? Long.fromValue(object.reqDefvalLong) - : Long.fromNumber(7812378193); + message.reqDefvalLong = (object.reqDefvalLong !== undefined && object.reqDefvalLong !== null) + ? Long.fromValue(object.reqDefvalLong) + : Long.fromNumber(7812378193); message.reqDefvalTruth = object.reqDefvalTruth ?? true; message.reqDefvalDescription = object.reqDefvalDescription ?? "Some description"; message.reqDefvalData = object.reqDefvalData ?? new Uint8Array(0); message.reqDefvalFloat = object.reqDefvalFloat ?? 0.12354; message.optDefvalId = object.optDefvalId ?? undefined; message.optDefvalState = object.optDefvalState ?? undefined; - message.optDefvalLong = - object.optDefvalLong !== undefined && object.optDefvalLong !== null - ? Long.fromValue(object.optDefvalLong) - : undefined; + message.optDefvalLong = (object.optDefvalLong !== undefined && object.optDefvalLong !== null) + ? Long.fromValue(object.optDefvalLong) + : undefined; message.optDefvalTruth = object.optDefvalTruth ?? undefined; message.optDefvalDescription = object.optDefvalDescription ?? undefined; message.optDefvalData = object.optDefvalData ?? undefined; @@ -1053,21 +1054,14 @@ function base64FromBytes(arr: Uint8Array): string { type Builtin = Date | Function | Uint8Array | string | number | boolean | undefined; -export type DeepPartial = T extends Builtin - ? T - : T extends Long - ? string | number | Long - : T extends globalThis.Array - ? globalThis.Array> - : T extends ReadonlyArray - ? ReadonlyArray> - : T extends {} - ? { [K in keyof T]?: DeepPartial } +export type DeepPartial = T extends Builtin ? T + : T extends Long ? string | number | Long : T extends globalThis.Array ? globalThis.Array> + : T extends ReadonlyArray ? ReadonlyArray> + : T extends {} ? { [K in keyof T]?: DeepPartial } : Partial; type KeysOfUnion = T extends T ? keyof T : never; -export type Exact = P extends Builtin - ? P +export type Exact = P extends Builtin ? P : P & { [K in keyof P]: Exact } & { [K in Exclude>]: never }; function isObject(value: any): boolean { diff --git a/src/types.ts b/src/types.ts index 0ab684bb3..16b6c893a 100644 --- a/src/types.ts +++ b/src/types.ts @@ -351,7 +351,7 @@ export function notDefaultCheck( case FieldDescriptorProto_Type.TYPE_INT64: case FieldDescriptorProto_Type.TYPE_SINT64: case FieldDescriptorProto_Type.TYPE_SFIXED64: - if (options.forceLong === LongOption.LONG && !isJsTypeFieldOption(options, field)) { + if (options.forceLong === LongOption.LONG && !isJsTypeFieldOption(options, field) && ctx.currentFile.isProto3Syntax) { return code`${maybeNotUndefinedAnd} !${place}.equals(${defaultValue(ctx, field)})`; } else { return code`${maybeNotUndefinedAnd} ${place} !== ${defaultValue(ctx, field)}`;