-
Notifications
You must be signed in to change notification settings - Fork 347
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
Option for readonly types #163
Comments
My initial/short-term suggestion would be to try and make a Just as a better stop-gap than writing bespoke/hand-written types for each of your functions. But otherwise, yeah, I think some sort of |
Any news on this? |
@jonaskello nope; would be great if you wanted to submit a PR, the code to change would be right here: https://github.com/stephenh/ts-proto/blob/main/src/main.ts#L672 |
Yes, I might attempt a PR for this :-). I did some initial experimenting and changed the code you referenced to output Simple example of generated code that does not compile with readonly modifer: // ...
export interface SimpleMessage {
readonly numberField: number;
}
export const SimpleMessage = {
// ...
decode(input: Reader | Uint8Array, length?: number): SimpleMessage {
const reader = input instanceof Reader ? input : new Reader(input);
let end = length === undefined ? reader.len : reader.pos + length;
const message = createBaseSimpleMessage();
while (reader.pos < end) {
const tag = reader.uint32();
switch (tag >>> 3) {
case 1:
message.numberField = reader.int32(); // This line does not compile
break;
default:
reader.skipType(tag & 7);
break;
}
}
return message;
},
//...
fromPartial<I extends Exact<DeepPartial<SimpleMessage>, I>>(object: I): SimpleMessage {
const message = createBaseSimpleMessage();
message.numberField = object.numberField ?? 0; // This line does not compile
return message;
},
};
// ... @stephenh Do you have a suggestion how to work around this? |
@jonaskello ah sure... FWIW I'd probably just thrown in an
If we're in "readonlyTypes" mode. And personally since we're in "trusted" / generated code, I'd be fine with that "as any" white-lie to the type system. That said, looking around, it also looks like we could add a utility
Which would be conditionally output, similar to how our
...or maybe have That would be more kosher than the "as any". Fwiw if you're adding a |
🎉 This issue has been resolved in version 1.130.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Would it be possible to add an option to generate the types with readonly?
For example instead of generating this:
I would like to have this generated:
The reason behind this is that our team is working in a functional style and therefore trying to keep everything immutable as far as possible. For example if we have a function to generate an array of items, that array is always readonly. So we cannot assign our function's return values directly to ts-proto generated types today .
I'm not sure it it would be possible to have readonly types in all cases but if it is feasible and in line with the goals of this project I may be able to work on a PR for it.
The text was updated successfully, but these errors were encountered: