Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating tsconfig.json target target to "es2022" causes "Property 'xx' is used before its initialization.ts(2729)"

I have a lot of Angular code where I assign a property at its definition like the following...

 public errors$ = this.store$.select(fromApp.getErrors);

 constructor(private store$: Store<MyState>

As we can see store$ is injected via the constructor. But now after changing tsconfig.json target target to "es2022" I get the error

Property 'store$' is used before its initialization.ts(2729)

Are we still able to assign such properties at the declaration?

like image 742
peterc Avatar asked Oct 31 '25 13:10

peterc


1 Answers

You actually don't know for sure if store$ will be filled before or after errors$.
A good rule of thumb is to initialize simple properties within the declaration, but whenever it depends on something else, do it in the constructor.

public errors$: Observable<Foo>;

constructor(private store$: Store<MyState>) {
   this.errors$ = this.store$.select(fromApp.getErrors);
}

here, you're certain store$ is filled before errors$.

like image 160
Random Avatar answered Nov 02 '25 04:11

Random



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!