angular rxjs subscriptions

Viewed 359

i am declaring a subscription in Angular like this:

counterSubscription: Subscription

and it is giving me this error:

Property 'counterSubscription' has no initializer and is not definitely assigned in the constructor.

So what could be the initial value of a Subscription please?

2 Answers

If you are initializing your subscription in an angular lifecycle hook, like ngOnInit, you can use the Definite Assignment Assertion Operator ! to inform TypeScript that the variable will be initialized.

counterSubscription!: Subscription;

You can also add strictPropertyInitialization: false rule to your tsconfig.json to disable this behaviour.

Related