Generated code
Generated Protobuf-ES files are intentionally plain TypeScript: message types, schema exports, typed fields, discriminated oneofs, arrays, and objects. Running buf generate or protoc with protoc-gen-es produces one _pb file per .proto file. Treat generated files as build artifacts: regenerate them instead of editing them by hand.
A .proto file named foo/bar.proto generates one of these files, depending on the selected target:
foo/bar_pb.tsfortarget=tsfoo/bar_pb.jsfortarget=jsfoo/bar_pb.d.tsfortarget=dts
Each file starts with a generated-file preamble:
// @generated by protoc-gen-es v2.12.0 with parameter "target=ts"// @generated from file example.proto (package example, syntax proto3)/* eslint-disable */Generated imports are relative and follow the plugin options you configured. If your Protobuf file imports another file, the generated code imports the matching _pb output.
File schema
Section titled “File schema”Every generated file exports a file descriptor:
/** * Describes the file example.proto. */export const file_example: GenFile = fileDesc(/* ... */);Use this descriptor with registries and reflection.
Messages
Section titled “Messages”Each Protobuf message becomes a TypeScript type plus a schema export.
message User { string first_name = 1; string last_name = 2; bool active = 3; User manager = 4; repeated string locations = 5; map<string, string> projects = 6;}export type User = Message<"example.User"> & { firstName: string; lastName: string; active: boolean; manager?: User | undefined; locations: string[]; projects: { [key: string]: string };};
export const UserSchema: GenMessage<User> = messageDesc(file_example, 0);Pass the schema to runtime helpers such as create(), fromBinary(), toBinary(), fromJson(), and toJson().
import { create, toBinary } from "@bufbuild/protobuf";import { type User, UserSchema } from "./gen/example_pb";
const user: User = create(UserSchema, { firstName: "Alice" });const bytes = toBinary(UserSchema, user);Each Protobuf enum becomes a TypeScript enum plus a schema export.
enum PhoneType { PHONE_TYPE_UNSPECIFIED = 0; PHONE_TYPE_MOBILE = 1; PHONE_TYPE_LAND_LINE = 2;}export enum PhoneType { UNSPECIFIED = 0, MOBILE = 1, LAND_LINE = 2,}
export const PhoneTypeSchema: GenEnum<PhoneType> = enumDesc(file_example, 0);If every enum value shares a prefix that matches the enum name, Protobuf-ES drops that prefix in generated code.
Extensions and services
Section titled “Extensions and services”Extensions become typed extension descriptors:
export const age: GenExtension<User, number> = extDesc(/* ... */);Services generate schemas only. Protobuf-ES does not include an RPC transport; libraries such as connect-es use these service schemas.
export const UserService: GenService<{ createUser: { methodKind: "unary"; input: typeof CreateUserRequestSchema; output: typeof CreateUserResponseSchema; };}>;Reference pages
Section titled “Reference pages”- Field types: Scalar, message, enum, repeated, map, and well-known type field shapes.
- Features: Oneofs, required fields, optional fields, extensions, services, reserved names, nested types, comments, and packages.