-
-
Notifications
You must be signed in to change notification settings - Fork 75
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
Fields can be undefined but return type of getters can't #80
Comments
It's even worse. Let's say, I know that the Typescript Compiler can't help me here because of the wrong typings in the generated code but it's OK, because I will do the check manually in the code. Then ESLint tells me that I'm doing something wrong: // Unnecessary conditional, the types have no overlap. (eslint@typescript-eslint/no-unnecessary-condition)
if (b.item === undefined) {
...
}
// Unnecessary conditional, value is always truthy. (eslint@typescript-eslint/no-unnecessary-condition)
if (b.item) {
...
} |
I am not sure if that is the correct approach here as we tried that one before. The best would be optional getters which is not supported by typescript yet. |
I don't think that this is an issue of TypeScript. The typings should just reflect the types that can occur at runtime, otherwise the typings at compile time don't make any sense. What I can see:
I would say that all cases above are properly covered by the current feature set of TypeScript. What do you think? |
I have no problem whatsoever with the approach above. there are some typescript limitations that prevent us from doing this. The problem with getter setter types is that they can’t have different types. hence, since |
it is going to take a while this gets stabilized. then we can revisit the options. |
Ok, that's true, the type of getters and setters must be equal. If I look at my check list above then only point 7 ( The main reason for this ticket is point 2 ( To make it short: Accept |
You are right. This definitely should be fixed. Would be happy to see a PR if you would like to take it. Otherwise, I will try to take this, this week. |
Though this is going to be a breaking change. so I have to find a way to land this without annoying everyone. |
with proto3 optionals fields also get a getter called |
What is the idea behind the current implementation to generate code from proto files where the return type of the field getters is always cast to the data type of the field?
The cast in the generated typescript code is realized with an
as
expression, which can be dangerous. The Typescript compiler is possibly happy with the cast while the data type has nothing to do with the real data type available at runtime in JavaScript.This is exactly the case when a field of a proto message, here
B
, is of type of another proto message, hereA
:If I use the generated typescript code to create an instance of B like:
then the generated typescript code tells me that
b.item
is always of typeA
but in fact,b.item
isundefined
. This can be checked easily by printingb.item
toconsole
. So, the correct typings forb.item
would beA | undefined
. Or am I wrong? Do I miss something here?The text was updated successfully, but these errors were encountered: