{"library":"protoc-gen-ts","title":"Protocol Buffer Compiler for TypeScript","description":"protoc-gen-ts is a `protoc` plugin that generates plain TypeScript source files from Protocol Buffer `.proto` definitions, effectively replacing separate `.d.ts` declaration files. It is actively maintained, with the current stable version being `0.8.7`, and new features/fixes are delivered through frequent minor releases. A key differentiator is its direct TypeScript output which eliminates common prefixes (e.g., `getField`) and exposes fields as standard getters/setters, along with `fromObject` and `toObject` methods for robust bidirectional mapping between JSON and message instances, supporting deep structures without runtime type reflection. It offers native support for gRPC Node (`@grpc/grpc-js`) and gRPC Web, including options for promise-based RPC calls. Messages defined within a `package` directive in the `.proto` file are by default encapsulated within a TypeScript namespace, though this behavior can be toggled.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install protoc-gen-ts"],"cli":{"name":"protoc-gen-ts","version":null}},"imports":["import { Change } from './myproto_pb';","import { Kind } from './myproto_pb';","import { MyServiceClient } from './myproto_pb';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Change, Kind, Author } from './myproto_pb'; // Assumes protoc-gen-ts generated './myproto_pb.ts'\n\n// Construct a message instance\nconst author = new Author({\n    name: 'mary poppins',\n    role: 'maintainer'\n});\n\nconst change = new Change({\n    kind: Kind.UPDATED,\n    patch: '@@ -7,11 +7,15 @@',\n    tags: ['no prefix', 'as is'],\n    name: 'patch for typescript 4.5',\n    author: author\n});\n\n// Serialize to bytes (Uint8Array)\nconst bytes: Uint8Array = change.serialize();\n\nconsole.log('Serialized bytes:', bytes);\n\n// Deserialize from bytes back into a message instance\nconst receivedChange: Change = Change.deserialize(bytes);\n\nconsole.log('Deserialized Change object:', receivedChange);\nconsole.log('Kind:', receivedChange.kind === Kind.UPDATED); // true\nconsole.log('Author name:', receivedChange.author?.name); // mary poppins\n\n// Using fromObject for easier JSON-to-message mapping\nconst jsonChange = Change.fromObject({\n    kind: Kind.DELETED,\n    patch: 'deleted line',\n    tags: ['cleanup'],\n    id: '12345',\n    author: {\n        name: 'john doe',\n        role: 'contributor'\n    }\n});\n\nconsole.log('Change from JSON:', jsonChange.toObject());\nconsole.log('Author from JSON is an Author instance:', jsonChange.author instanceof Author);","lang":"typescript","description":"This quickstart demonstrates how to create, serialize, and deserialize a Protocol Buffer message using the generated TypeScript classes. It also illustrates the convenient `fromObject` and `toObject` methods for JSON interoperability.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}