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
Language Service: More forgiving support of union types in templates #17953
Comments
|
This is equivalent to this playground example in which TypeScript also reports an error. As we improve type checking during AOT compilation this will be reported as an error in 5.0 as well as we will use TypeScript's type checker instead of our own. |
|
Thanks for the reply. The difference is that in regular TS code I can cast before accessing the property, which I cannot do in the template. const v = (item as Line).length || (item as Plane).area;I guess the way to do this now would be then to provide a method on the component which performs the cast and returns the appropriate value, and then just call this method in the template. |
|
That is certainly what we recommend. The templates expressions should be simple and any complicated logic should be in the component. |
|
@chuckjaz and what about tagged union types? For example: Should we do type casting in component also? Like this: |
|
Interesting problem. For this to work I need some way to declare the type relation between Following the pattern of #20702, I would then need to declare a guard for which would then allow me to generate the following type-check block: if (NgSwitchGuard.ngSwitch_member_ngSwitchCaseTypeGuard(component.paymentMethod, "type", "paypal") {
component.paymentMethod.email;
}A simplified version of this can be seen in the playground here. I currently can only get this to work for the string union types and deciding when and how to apply it is a bit tricky in general (the odd part ignored above) but this seems like we might eventually be able to do this. Unfortunately, adding a type parameter to |
|
I added #20780 to track this. |
|
Looking forward to see it landed. Thanks for the detailed response @chuckjaz |
|
This does not seem to be fixed still. Type unions are still not working as expected in templates |
|
|
|
@albv Your code looks so freakishly like mine I actually thought it was mine for a second. I modified your answer slightly, to what I think makes a little more sense if you don't want to change the object being 'switched' upon. In your case you added In my case, I have an array of pure (from JSON) data representing a list of payment methods, and couldn't / didn't want to add methods onto it. Instead I created the following method to use in the component: Component class Component template This ends up looking more like a 'cast' within a block where you've clearly established the data type. If you really think people can make a mistake you could do a check inside |
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
michaelbromley commentedJul 6, 2017
I'm submitting a ...
Current behavior
When I have a variable in my component with a union type, the language service will complain if I reference any properties which are not common to all members of the union type. Sometimes it may be valid to try to reference these non-shared properties, e.g.:
Here is a screenshot of the output from VS Code using the language service plugin:
Expected behavior
I would expect to be able to use the above construction without errors.
Minimal reproduction of the problem with instructions
@angular/cli 1.2.0@angular/language-service@4.1.0-betaWhat is the motivation / use case for changing the behavior?
The example above illustrates a simple uses case. In my app I have a situation where I have a generic component which can be passed different types of object, and uses the
||operator to display the first truthy value.Please tell us about your environment
The text was updated successfully, but these errors were encountered: