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?
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$.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With