-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
TypeScript: strongly typed lookup function #544
Comments
Hmm, the following is passing in the test file: protobuf.Class.create(root.lookup("Hello") as protobuf.Type, Hello); |
It's possible, but messy. And not particularly safe (in the case that it isn't in fact a Type) |
The changeset 01365ba helps specifically for Type and Service, but if I understand @paralin correctly, he was looking for a type declaration like lookup<T>(path: (string|string[]), parentAlreadyChecked?: boolean): T extends (ReflectionObject|Type); (didn't check that, just trying to generalize the type definition right here in the comment editor) This would enable him writing tree.lookup<MyMessage>('mynamespace.MyMessage').encode... |
Actually @bb that's not what I meant, however what you have posted is quite nice and I'd love it if that was in place. |
I'd favor any solution that doesn't require additional code (like it is now with |
Hi @dcodeIO, I tested the following on node v6.9.1, and seems nodejs doesn't happy with that https://github.com/dcodeIO/protobuf.js/blob/master/types/test.ts#L20 it throws error |
Try |
|
I'm finding myself doing this a lot:
The reason is that
lookup
returns aReflectionObject
which I cannot directly cast to any of its children (I can cast a Type -> ReflectionObject but not the other way around).I'm proposing doing something like:
Strongly typed with exceptions if you attempt to lookup the wrong type (or just return null)
The text was updated successfully, but these errors were encountered: