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.
npm install ts-protoVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to generate TypeScript types and interfaces from a `.proto` file using `protoc` and `ts-proto`, including basic message and service definitions.
Update imports and usage of low-level serialization utilities from `protobufjs` to `@bufbuild/protobuf/wire`. For example, `import { BinaryReader, BinaryWriter } from '@bufbuild/protobuf/wire';`.Use `protoc --plugin=protoc-gen-ts_proto=".\node_modules\.bin\protoc-gen-ts_proto.cmd" --ts_proto_out=. ./simple.proto` on Windows.
For projects requiring CommonJS compatibility, ensure your `tsconfig.json` has `"module": "CommonJS"` and potentially set `"esModuleInterop": true`. Alternatively, embrace ESM throughout your project by setting `"type": "module"` in `package.json` and using `import` statements.
Refer to the official gRPC documentation for up-to-date `protoc` installation instructions for your platform and ensure you have a recent version installed.
Update your code to import `BinaryReader` and `BinaryWriter` from `@bufbuild/protobuf/wire` instead of `protobufjs`.
Verify the path to `protoc-gen-ts_proto` in your `node_modules/.bin` directory. On Windows, use the `.cmd` extension and the specific quoting/path format: `protoc --plugin=protoc-gen-ts_proto=".\node_modules\.bin\protoc-gen-ts_proto.cmd" --ts_proto_out=. ./simple.proto`. Ensure the file is executable.
First, run the `protoc` command to generate the TypeScript files. Then, ensure your `tsconfig.json` includes the output directory (e.g., `"include": ["src", "./"]`) and that `"moduleResolution": "node"` or `"node16"` is set.