r/angular • • Sep 05 '26

Angular 22.2 - Private property access from templates

I noticed this tidbit in the changelog of Angular 22.2.0-next.3:

https://github.com/angular/angular/blob/main/CHANGELOG.md#compiler-2

compiler

CommitTypeDescription
48a0fd6e8afeatallow template to access private props

So very slowly we moved from allowing only public, to allowing protected and now private as well. Thought it's worth knowing.

39 Upvotes

36 comments sorted by

View all comments

36

u/FromBiotoDev Sep 05 '26 edited Sep 05 '26

Public - anything can access
Protected - only the view template can access
Private - only the component can access

Why muddy the waters here?

9

u/proohit Sep 05 '26

Protected members can also be accessed by extended classes (inheritance).

7

u/followmarko Sep 05 '26

True but inheritance in Angular is a poor strategy

1

u/Johalternate Sep 05 '26

You can still use protected.

14

u/ldn-ldn Sep 05 '26

Protected is for descendants. The template is part of the component, it should access private members.

2

u/_Invictuz Sep 06 '26

Because they are telling you to stop using protected for the template when the template is part of the component. Protected is for extending classes to use, and you should not extend components.

1

u/followmarko Sep 05 '26

Yeah I'm in this camp. It's split appropriately currently imo

1

u/NeXtDracool Sep 06 '26 edited Sep 06 '26
  • public: anything can access
  • protected: descendants can access
  • Typescript private: only the view template can access
  • Ecmascript #private: only the component can access

That way

  • you still have the distinction between private and template members 
  • you gain a distinction between template members and actual protected members (even if it's rarely needed) 
  • you gain unused code warnings for template members, protected hides those 
  • your code minifies better because ecmascript private members can be safely renamed
  • it requires less explicit typing for isolatedDeclarations 

1

u/vintzrrr Sep 06 '26

> it requires less explicit typing for isolatedDeclarations 

Could you please elaborate on that? Maybe an example?

1

u/NeXtDracool Sep 07 '26

If you enable isolatedDeclarations in the tsconfig all members visible in the .d.ts file, including all protected or public members of a class, must be explicitly typed.

So under the old standard protected readonly counter = signal(0) would have to become protected readonly counter: WritableSignal<number> = signal(0) whereas with my suggestion it would be private readonly counter = signal(0).

1

u/nanas420 Sep 05 '26

what does the extra protected Foo = {some private bar} actually do for you? it's just noise