ts-proto: TypeScript Protobuf Generator

2.11.6 · active · verified Tue Apr 21

ts-proto is a TypeScript code generation tool that transforms Protocol Buffer (`.proto`) schemas into strongly-typed, idiomatic TypeScript files. It provides robust type definitions for messages and services, along with utilities for encoding, decoding, and JSON serialization. Currently at version 2.11.6, the project maintains an active release cadence, with frequent bug fixes and feature enhancements, as seen in the recent 2.11.x releases addressing issues like `globalThis.Buffer` casting, `isolatedDeclarations` compatibility, and `NullValue` handling. A significant differentiator for ts-proto v2.x is its migration from the `protobufjs` library to `@bufbuild/protobuf` for low-level serialization, aiming for improved performance and maintainability. It also supports generating client implementations for various RPC frameworks including Twirp, gRPC-web, gRPC-js, and NestJS, offering a comprehensive solution for integrating Protobuf with TypeScript applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to generate TypeScript types and interfaces from a `.proto` file using `protoc` and `ts-proto`, including basic message and service definitions.

/* simple.proto */
syntax = "proto3";

package example;

message Person {
  string name = 1;
  int32 id = 2;
  string email = 3;
}

message PingRequest {
  string message = 1;
}

message PingResponse {
  string message = 1;
}

service PingService {
  rpc Ping(PingRequest) returns (PingResponse);
}

// Terminal commands
// 1. Install ts-proto
npm install ts-proto

// 2. Install protoc (if not already installed, see grpc.io/docs/protoc-installation)

// 3. Generate TypeScript files
protoc \
  --plugin=./node_modules/.bin/protoc-gen-ts_proto \
  --ts_proto_out=. \
  ./simple.proto

// Generated usage example (e.g., in index.ts)
// import { Person, PingService } from './simple';
//
// const person: Person = { name: 'Alice', id: 123, email: 'alice@example.com' };
// console.log('Person:', Person.toJSON(person));
//
// const pingRequest: PingRequest = { message: 'Hello, gRPC!' };
// // In a real app, you would have a client implementation for PingService
// // For example, a mock client:
// class MockPingServiceClient implements PingService {
//   ping(request: PingRequest): Promise<PingResponse> {
//     console.log('Mock Ping request:', request.message);
//     return Promise.resolve({ message: `Pong: ${request.message}` });
//   }
// }
//
// const client = new MockPingServiceClient();
// client.ping(pingRequest).then(response => {
//   console.log('Mock Ping response:', response.message);
// });

view raw JSON →