diff --git a/packages/protobuf-test/src/gen/js/extra/proto2_pb.d.ts b/packages/protobuf-test/src/gen/js/extra/proto2_pb.d.ts index dd2977493..9568dd6be 100644 --- a/packages/protobuf-test/src/gen/js/extra/proto2_pb.d.ts +++ b/packages/protobuf-test/src/gen/js/extra/proto2_pb.d.ts @@ -182,17 +182,17 @@ export declare class Proto2RequiredMessage extends Message); diff --git a/packages/protobuf-test/src/gen/js/google/protobuf/map_lite_unittest_pb.d.ts b/packages/protobuf-test/src/gen/js/google/protobuf/map_lite_unittest_pb.d.ts index d072a3320..e5f546eab 100644 --- a/packages/protobuf-test/src/gen/js/google/protobuf/map_lite_unittest_pb.d.ts +++ b/packages/protobuf-test/src/gen/js/google/protobuf/map_lite_unittest_pb.d.ts @@ -414,17 +414,17 @@ export declare class TestRequiredLite extends Message { /** * @generated from field: required int32 a = 1; */ - a: number; + a?: number; /** * @generated from field: required int32 b = 2; */ - b: number; + b?: number; /** * @generated from field: required int32 c = 3; */ - c: number; + c?: number; constructor(data?: PartialMessage); diff --git a/packages/protobuf-test/src/gen/js/google/protobuf/test_messages_proto2_pb.d.ts b/packages/protobuf-test/src/gen/js/google/protobuf/test_messages_proto2_pb.d.ts index 5f2716023..fbffbd15a 100644 --- a/packages/protobuf-test/src/gen/js/google/protobuf/test_messages_proto2_pb.d.ts +++ b/packages/protobuf-test/src/gen/js/google/protobuf/test_messages_proto2_pb.d.ts @@ -1145,77 +1145,77 @@ export declare class TestAllRequiredTypesProto2 extends Message); @@ -1388,7 +1388,7 @@ export declare class TestAllRequiredTypesProto2_NestedMessage extends Message); @@ -1474,7 +1474,7 @@ export declare class TestAllRequiredTypesProto2_MessageSetCorrectExtension1 exte /** * @generated from field: required string str = 25; */ - str: string; + str?: string; constructor(data?: PartialMessage); @@ -1503,7 +1503,7 @@ export declare class TestAllRequiredTypesProto2_MessageSetCorrectExtension2 exte /** * @generated from field: required int32 i = 9; */ - i: number; + i?: number; constructor(data?: PartialMessage); diff --git a/packages/protobuf-test/src/gen/js/google/protobuf/unittest_custom_options_pb.d.ts b/packages/protobuf-test/src/gen/js/google/protobuf/unittest_custom_options_pb.d.ts index 32e53efc4..91b901f93 100644 --- a/packages/protobuf-test/src/gen/js/google/protobuf/unittest_custom_options_pb.d.ts +++ b/packages/protobuf-test/src/gen/js/google/protobuf/unittest_custom_options_pb.d.ts @@ -749,7 +749,7 @@ export declare class OldOptionType extends Message { /** * @generated from field: required protobuf_unittest.OldOptionType.TestEnum value = 1; */ - value: OldOptionType_TestEnum; + value?: OldOptionType_TestEnum; constructor(data?: PartialMessage); @@ -785,7 +785,7 @@ export declare class NewOptionType extends Message { /** * @generated from field: required protobuf_unittest.NewOptionType.TestEnum value = 1; */ - value: NewOptionType_TestEnum; + value?: NewOptionType_TestEnum; constructor(data?: PartialMessage); diff --git a/packages/protobuf-test/src/gen/js/google/protobuf/unittest_lite_pb.d.ts b/packages/protobuf-test/src/gen/js/google/protobuf/unittest_lite_pb.d.ts index f62a7101f..a6f9fba9b 100644 --- a/packages/protobuf-test/src/gen/js/google/protobuf/unittest_lite_pb.d.ts +++ b/packages/protobuf-test/src/gen/js/google/protobuf/unittest_lite_pb.d.ts @@ -875,7 +875,7 @@ export declare class TestDeprecatedLite extends Message { * @generated from field: required int32 deprecated_field2 = 2 [deprecated = true]; * @deprecated */ - deprecatedField2: number; + deprecatedField2?: number; /** * @generated from field: optional string deprecated_field3 = 3 [deprecated = true]; @@ -1187,7 +1187,7 @@ export declare class V1MessageLite extends Message { /** * @generated from field: required int32 int_field = 1; */ - intField: number; + intField?: number; /** * @generated from field: optional protobuf_unittest.V1EnumLite enum_field = 2 [default = V1_FIRST]; @@ -1216,7 +1216,7 @@ export declare class V2MessageLite extends Message { /** * @generated from field: required int32 int_field = 1; */ - intField: number; + intField?: number; /** * @generated from field: optional protobuf_unittest.V2EnumLite enum_field = 2 [default = V2_FIRST]; diff --git a/packages/protobuf-test/src/gen/js/google/protobuf/unittest_mset_pb.d.ts b/packages/protobuf-test/src/gen/js/google/protobuf/unittest_mset_pb.d.ts index 7afea157d..4bb57f686 100644 --- a/packages/protobuf-test/src/gen/js/google/protobuf/unittest_mset_pb.d.ts +++ b/packages/protobuf-test/src/gen/js/google/protobuf/unittest_mset_pb.d.ts @@ -199,7 +199,7 @@ export declare class TestMessageSetExtension3 extends Message); @@ -254,12 +254,12 @@ export declare class RawMessageSet_Item extends Message { /** * @generated from field: required int32 type_id = 2; */ - typeId: number; + typeId?: number; /** * @generated from field: required bytes message = 3; */ - message: Uint8Array; + message?: Uint8Array; constructor(data?: PartialMessage); diff --git a/packages/protobuf-test/src/gen/js/google/protobuf/unittest_optimize_for_pb.d.ts b/packages/protobuf-test/src/gen/js/google/protobuf/unittest_optimize_for_pb.d.ts index 83253e5a1..da52a7491 100644 --- a/packages/protobuf-test/src/gen/js/google/protobuf/unittest_optimize_for_pb.d.ts +++ b/packages/protobuf-test/src/gen/js/google/protobuf/unittest_optimize_for_pb.d.ts @@ -89,7 +89,7 @@ export declare class TestRequiredOptimizedForSize extends Message); diff --git a/packages/protobuf-test/src/gen/js/google/protobuf/unittest_pb.d.ts b/packages/protobuf-test/src/gen/js/google/protobuf/unittest_pb.d.ts index cbe526918..8d65db575 100644 --- a/packages/protobuf-test/src/gen/js/google/protobuf/unittest_pb.d.ts +++ b/packages/protobuf-test/src/gen/js/google/protobuf/unittest_pb.d.ts @@ -1816,7 +1816,7 @@ export declare class TestRequiredEnum extends Message { /** * @generated from field: required protobuf_unittest.ForeignEnum required_enum = 1; */ - requiredEnum: ForeignEnum; + requiredEnum?: ForeignEnum; /** * A dummy optional field. @@ -1849,7 +1849,7 @@ export declare class TestRequiredEnumNoMask extends Message); @@ -1978,22 +1978,22 @@ export declare class TestRequiredNoMaskMulti extends Message); @@ -2063,7 +2063,7 @@ export declare class TestRequired extends Message { /** * @generated from field: required int32 a = 1; */ - a: number; + a?: number; /** * @generated from field: optional int32 dummy2 = 2; @@ -2073,7 +2073,7 @@ export declare class TestRequired extends Message { /** * @generated from field: required int32 b = 3; */ - b: number; + b?: number; /** * Pad the field count to 32 so that we can test that IsInitialized() @@ -2226,7 +2226,7 @@ export declare class TestRequired extends Message { /** * @generated from field: required int32 c = 33; */ - c: number; + c?: number; /** * Add an optional child message to make this non-trivial for go/pdlazy. @@ -2810,7 +2810,7 @@ export declare class TestIsInitialized_SubMessage_SubGroup extends Message); @@ -4383,7 +4383,7 @@ export declare class TestRequiredOneof_NestedMessage extends Message); diff --git a/packages/protoc-gen-es/src/declaration.ts b/packages/protoc-gen-es/src/declaration.ts index ca0a41af8..3dfc1bec1 100644 --- a/packages/protoc-gen-es/src/declaration.ts +++ b/packages/protoc-gen-es/src/declaration.ts @@ -26,6 +26,7 @@ import type { } from "@bufbuild/protoplugin/ecmascript"; import { getFieldTyping, + getFieldIntrinsicDefaultValue, localName, reifyWkt, } from "@bufbuild/protoplugin/ecmascript"; @@ -138,8 +139,9 @@ function generateField(schema: Schema, f: GeneratedFile, field: DescField) { f.print(f.jsDoc(field, " ")); const e: Printable = []; e.push(" ", localName(field)); + const { defaultValue } = getFieldIntrinsicDefaultValue(field); const { typing, optional } = getFieldTyping(field, f); - if (optional) { + if (optional || defaultValue === undefined) { e.push("?: ", typing); } else { e.push(": ", typing);