-
Notifications
You must be signed in to change notification settings - Fork 349
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
Adding Explicit Type Definitions to Factory Method Objects #1080
Comments
Hi @sam-step ! Apologies for the late reply, but the explicit annotations sounds worth trying! I was thinking it will blow up the Do you think doing an interface like Even if If you're still interested, would be great to have a PR! Thanks! |
Yes, I like this idea a lot! Addresses the compilation issue and has the nice side benefit of a much smaller (and more readable) declarations file. Took a closer look at the code and I think this should be doable. A few questions came up that I want to get feedback on before investing more time here: Naming Flag or default? Constant members interface StaticMessage<T, N extends string> {
readonly $type: N
encode(message: T, writer?: BinaryWriter): BinaryWriter
decode(input: BinaryReader | Uint8Array, length?: number): T
// Others...
}
export const FileDescriptorProto: StaticMessage<FileDescriptorProto, 'google.protobuf.FileDescriptorProto'> = {
$type: "google.protobuf.FileDescriptorProto" as const,
// ...
}
const type = FileDescriptorProto.$type // Guaranteed to be "google.protobuf.FileDescriptorProto" Does that make sense? I think this is the only way to retain the same level of specificity here, but I might be missing something. Conditional methods Assuming these are the only two conditionally added methods, one option could be just create a second interface and union them when needed. E.g.: interface StaticMessage<T> {
encode(message: T, writer?: BinaryWriter): BinaryWriter
decode(input: BinaryReader | Uint8Array, length?: number): T
// Others...
}
interface Extendable<T> {
getExtension<V>(message: T, extension: Extension<V>): V | undefined
setExtension<V>(message: T, extension: Extension<V>, value: V): void
}
const MyMessage: StaticMessage<MyMessage> & Extendable<MyMessage> = {
// ...
}
const MyOtherMessageWithoutExtensions: StaticMessage<MyOtherMessageWithoutExtensions> = {
// ...
} Another route could be to just not conditionally add methods to the object (ie always add get/setExtension methods if outputExtensions is true, regardless of whether the message has any extensions). But it seems wasteful to add methods that'll never get called. |
Good question, maybe
I'm good with default, since it's "just types" and should get stripped out of any bundled code and not affect output size.
Good catch on the
Another great catch--the Thanks for all of the research into this so far! You've found several things I would not thought of until getting ~1/2th way through the PR. :-) |
Fixes #1080 Adds an interface for the static message methods to avoid tsc errors on large messages. Since the set of static methods created may vary from message-to-message, multiple interfaces were created and are unioned as needed: - `MessageFns`. The common encode/decode methods. All the message objects implement this. - `ExtensionFns`. The get/set extension methods. Added if extensions are enabled and the message has extensions. - `ExtensionHolder`. The individual extension properties. - `[Struct|AnyValue|ListValue|FieldMask]WrapperFns`. The wrap/unwrap methods for the well-known types.
🎉 This issue has been resolved in version 2.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@stephenh can we expect this version to show up on the buf registry some time soon? the latest there is still at v1.178.0 🙏 |
Hi @novascreen ! ts-proto used to push its own artifacts to the Buf registry, but as of ~ a year ago, I believe Buf controls all publishing of the community plugins like ts-proto. @timosaikkonen does that sound right? Is there anything you can poke at, to get the latest versions pulling back into the Buf registry? Thanks! |
Hi, I recently ran into the "inferred type of this node exceeds the maximum length" error referenced in #449
It can be easily reproduced by running ts-proto on google/protobuf/descriptor.proto.
I understand turning off exact types works around the problem. However, the exact types feature is pretty nice so I'm looking to see if there are alternative solutions.
Following the compiler's suggestion to add explicit annotations seems to address the problem. E.g.
Is adding support for these explicit annotations something you'd consider (it certainly produces more verbose code...)? If so, happy to take a stab at contributing a PR for it.
The text was updated successfully, but these errors were encountered: