Re:Full-Stack TypeScript with tRPC and React
by tczMUFlmoNk on Tuesday March 14, 2023 @06:19PM
(#35159854)
> Also, using a `oneof` field is correctly typed on the frontend as
myfield: TypeA | TypeB | TypeC
But what about when the `oneof` has multiple variants with the same base type? Like: message User {
oneof contact_info {
string email = 1;
string phone_number = 2;
}
}
Does this get compiled down to `contact_info: string | string` on the frontend? How do you tell which variant was used without encoding the discriminant?
The frontend and backend get code generated differently. The backend is all interfaces (which, through some Nestjs magic get translated into protobufs) and the frontend is all objects. So on the frontend, you're expected to write:
On the backend, you'd just write: It's not a big deal, but it's weird. The backend ends up being more/overly strict about all values being set when you might just be fine with the default zero/empty values.Also, using a `oneof` field is correctly typed on the frontend as
But the backend gives you For a future project, I'd look into buf or Connect instead of the default gRPC code generators, but for now, I just need to get stuff done.