Skip to content

Field types

Generated field types follow the Protobuf field kind. create() supplies runtime defaults; TypeScript types describe the shape stored on the message.

Protobuf typeECMAScript typeNotesRuntime default
stringstringUTF-8""
boolbooleanfalse
bytesUint8Arraynew Uint8Array(0)
doublenumber64-bit float0
floatnumber32-bit float0
int32number32-bit signed varint0
uint32number32-bit unsigned varint0
int64bigint64-bit signed varint0n
uint64bigint64-bit unsigned varint0n
fixed32number32-bit unsigned fixed0
fixed64bigint64-bit unsigned fixed0n
sfixed32number32-bit signed fixed0
sfixed64bigint64-bit signed fixed0n
sint32number32-bit signed varint0
sint64bigint64-bit signed varint0n

If bigint is unavailable in your environment, 64-bit integer fields still round-trip safely, but use string values at runtime instead.

Use jstype = JS_STRING when you want string output for a 64-bit field:

int64 field = 1 [jstype = JS_STRING];

With Buf managed mode, apply that option automatically:

managed:
enabled: true
override:
- field_option: jstype
value: JS_STRING

Message fields become optional properties. With exactOptionalPropertyTypes, the generated type includes undefined explicitly.

User manager = 4;
manager?: User | undefined;

Message fields do not have default values in Protobuf.

Enum fields use the generated TypeScript enum and default to the first declared value.

phoneType: PhoneType;

TypeScript enums can convert between numeric values and string names:

const val: PhoneType = PhoneType.MOBILE;
const name = PhoneType[val]; // "MOBILE"

Protobuf has open and closed enums. Open enums can contain numeric values that are not declared in the generated TypeScript enum, so code that receives data from newer schemas should handle unknown enum numbers.

Repeated fields become arrays.

repeated string locations = 5;
locations: string[];

New messages created with create() initialize repeated fields to empty arrays.

Map fields become record-like objects.

map<string, string> projects = 6;
projects: { [key: string]: string };

New messages created with create() initialize map fields to empty objects. Protobuf-ES uses objects instead of ECMAScript Map because objects fit better with current JavaScript and TypeScript tooling.

Some well-known types use JavaScript-friendly representations. google.protobuf.Struct fields become JsonObject:

struct?: JsonObject | undefined;

Wrapper messages become unboxed optional primitives:

boolValueField?: boolean | undefined;

See Well-known types for the full list of precompiled exports and helper functions.