protobuf-ts-types
is a tool that automatically generates TypeScript types from Protobuf schemas without requiring any code generation or compilation steps. It leverages the Protobuf runtime library to infer types directly, offering a simpler and faster workflow for TypeScript developers working with Protobuf. This eliminates the need for separate code generation tools and keeps the TypeScript types synchronized with the Protobuf schemas, reducing potential errors. The project aims to improve developer experience and efficiency when using Protobuf in TypeScript projects.
The Hacker News post titled "Show HN: Zero-codegen, no-compile TypeScript type inference from Protobufs" introduces protobuf-ts-types
, a new TypeScript library that dynamically generates TypeScript types directly from Protobuf (Protocol Buffer) schemas without requiring a separate code generation step or compilation process. This approach deviates from the traditional method of using protoc
and plugins to generate static TypeScript code from .proto
files.
The core functionality revolves around parsing Protobuf schema files (.proto
) at runtime, using the @protobuf-ts/runtime
library. This parser understands the structure and data types defined within the Protobuf schema. From this parsed schema, protobuf-ts-types
dynamically constructs corresponding TypeScript types, mirroring the Protobuf definitions. This includes complex types like nested messages, enums, and various scalar types. These generated types can then be used directly within a TypeScript project, enabling type-safe interaction with Protobuf data.
The elimination of the code generation step simplifies the development workflow. Developers no longer need to configure and run protoc
with specific plugins, nor manage the generated TypeScript code. Changes to the Protobuf schema are immediately reflected in the TypeScript types without recompilation. This facilitates rapid prototyping and iterative development with Protobufs.
The library aims to provide comprehensive type mapping, accurately representing the nuances of Protobuf schemas in the TypeScript type system. It also supports advanced features like custom Protobuf options and well-known types. The runtime approach taken by protobuf-ts-types
positions it as a flexible and efficient solution for integrating Protobufs into TypeScript projects, particularly those that benefit from a streamlined development process and dynamic type generation. The project is open-source and available on GitHub.
Summary of Comments ( 13 )
https://news.ycombinator.com/item?id=43682547
Hacker News users generally expressed interest in the project, praising its approach to Protobuf type generation in TypeScript. Several commenters highlighted the advantages of avoiding code generation and runtime dependencies, contrasting it favorably with existing solutions like
protoc
andprotobufjs
. Some questioned the handling of specific Protobuf features likeoneof
andany
, and discussions arose around potential performance implications and the project's compatibility with existing JavaScript Protobuf libraries. The author actively engaged with commenters, clarifying design choices and addressing technical questions about the project's inner workings. Overall, the reception was positive, with many seeing the project as a promising alternative for TypeScript Protobuf integration.The Hacker News post titled "Show HN: Zero-codegen, no-compile TypeScript type inference from Protobufs" (https://news.ycombinator.com/item?id=43682547) sparked a discussion with several interesting comments.
Many commenters expressed appreciation for the project's approach of avoiding code generation, which simplifies workflows and reduces potential maintenance overhead. One commenter highlighted the elegance of using generics for this purpose, contrasting it with the often cumbersome code generation processes they've encountered.
Several users brought up comparisons to other Protobuf tooling within the TypeScript ecosystem.
ts-proto
was mentioned frequently, with some users highlighting perceived advantages and disadvantages of each project. The discussion touched upon performance characteristics, the level of type safety offered, and the developer experience in terms of setup and usage. One user specifically asked about the differences between the presented project andts-proto
regarding how they handle optional fields and oneofs, indicating a desire to understand the nuances of each approach.One commenter inquired about the handling of nested messages and the generation of appropriate TypeScript types, which led to a brief discussion about the library's capabilities in this area. Another user raised the important point of how
protobuf-ts-types
manages breaking changes introduced by modifications to the.proto
files, a crucial aspect for maintaining type safety in evolving projects.The topic of runtime type checking was also raised. While the project focuses on static type safety during development, one commenter questioned whether runtime validation against the inferred types is also performed, which could add an extra layer of robustness in production environments.
Overall, the comments section reflects a generally positive reception of the project, with users expressing interest in its unique approach and engaging in productive discussions comparing its features to existing solutions. The discussion also highlights key considerations for Protobuf tooling in TypeScript, including handling optional fields, oneofs, nested messages, breaking changes, and potential runtime type checking.