Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create raw data types as well #1058

Open
abalmos opened this issue Jan 22, 2025 · 1 comment
Open

Create raw data types as well #1058

abalmos opened this issue Jan 22, 2025 · 1 comment

Comments

@abalmos
Copy link

abalmos commented Jan 22, 2025

syntax = "proto3";
package example;
message User {
  string id = 1;
}

will produce something like

export type User = Message<"example.User"> & {
  /**
   * @generated from field: string id = 1;
   */
  id: string;
}

However, the User type is hard to use when working with the message body as data throughout the code based because of the $typeName and $unknown fileds that Message<"example.User"> adds. What if instead it generated something like:

export type UserRaw = {
  /**
   * @generated from field: string id = 1;
   */
  id: string;
};

export type User = Message<"example.User"> & UserRaw

So that UserRaw can be used to hold data in the program logic, but one can also easily do:

const user: UserRaw = { id: "123" };
create(UserSchema, user)

This would remove the need for using a helper like OmitTypeName all over the place.

@timostamm
Copy link
Member

We didn't anticipate the dislike for $typeName. We hear you, but it isn't going to be easy to fully support the removal, and there are downsides too. Because of this, we won't be generating a "raw" type in the near future.

There is an alternative, though:

The source code of protoc-gen-es is pretty compact, considering that it generates enums, types, descriptors, and JSON types. If you pick just the code to generate message types, it should be less than 500 lines of code. So my suggestion would be to create such a plugin, and give it a try. If it works out well, you could publish it for others to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants