Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Guidelines suggest to use protected for template properties. #61166

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

Open
ice-blaze opened this issue May 7, 2025 · 7 comments
Open

Guidelines suggest to use protected for template properties. #61166

ice-blaze opened this issue May 7, 2025 · 7 comments
Labels
area: docs Related to the documentation
Milestone

Comments

@ice-blaze
Copy link
Contributor

ice-blaze commented May 7, 2025

Describe the problem that you experienced

After reviewing google's typescript guidelines (the one that is used as reference for contributing in Angular), it's written that we should use the visibility protected in Angular for template properties. https://google.github.io/styleguide/tsguide.html#properties-used-outside-of-class-lexical-scope.

But we can agreed that most of the exemples found in Angular's documentation examples use the public visibility. (e.g. the advancedSettings should be protected https://angular.dev/guide/components/programmatic-rendering#lazy-loading-components)

I would be willing to change the whole documentation to add the protected but first I wanted to be sure that everyones agree with it. If not, then I have the feeling either we should change google's guidelines or write something to override the contribution page.

@e-oz
Copy link

e-oz commented May 7, 2025

protected helps a lot during refactoring to safely remove unused code or rename some variables, methods, change their signatures.

@JeanMeche JeanMeche added the area: docs Related to the documentation label May 7, 2025
@ngbot ngbot bot added this to the needsTriage milestone May 7, 2025
@ice-blaze
Copy link
Contributor Author

ice-blaze commented May 7, 2025

If I challenge just the idea of pushing protected: I never had a case where I have to inherit components. Also had some feedbacks of devs saying to avoid inheritance when you migrate Angular. Then I guess people just go with the least effort solution and leave the default visibility (public) for components properties.

But my inner strict persona would say "follow the rule of: use the minimum visibility possible private < protected < public". If my code still works with protected visibility, then I should use it, but if it works with private visibility, then I should use private.

And on the side note, services are not "template properties" and for services I could see cases where inheritance make more sens. And also you can't use a service function if it's protected :)

@e-oz
Copy link

e-oz commented May 7, 2025

It is not related to inheritance at all. I also never extend components.

@JeanMeche
Copy link
Member

The new style guide has a dedicated entry about this : https://next.angular.dev/style-guide#use-protected-on-class-members-that-are-only-used-by-a-components-template

The idea behind this recomendation is that component properties that are only meant to be uses in the templates, should not be exposed as part of the public API of the component. This is why Angular templates have acces to protected properties.

@ice-blaze
Copy link
Contributor Author

@JeanMeche thank you for the link I missed that one. But then I think I find an issue because outputs seems to work with protected but not inputs, do you know a reason why? (e.g. stackblitz to show the issue, we log the output from parent component https://stackblitz.com/edit/stackblitz-starters-4xt6l5f4?file=src%2Fmain.ts)

@JeanMeche
Copy link
Member

JeanMeche commented May 7, 2025

Uh that's a good find, It should be consistent imho (as in we probably should support protected outputs).

@e-oz
Copy link

e-oz commented May 7, 2025

outputs are part of public API by definition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: docs Related to the documentation
Projects
None yet
Development

No branches or pull requests

3 participants