I have an abstract class that helps in avoiding repetitive code (to unsubscribe observables), it looks like:
export abstract class SubscriptionManagmentDirective implements OnDestroy {
componetDestroyed = new Subject<void>()
constructor() {}
ngOnDestroy(): void {
this.componetDestroyed.next()
this.componetDestroyed.unsubscribe()
}
}
In some cases it happened that I override ngOnDestroy
and forget to call super.ngOnDestroy()
and then subscriptions go wild.
I added a unit test to the extending components to test that un-subscriptions has been called, but to do so I have to actually remember to add them.
My question is,
super.ngOnDestroy()
is called whenever the component is extended?Any help would be appreciated, thanks.
I think the best approach to this would be adding "noImplicitOverride": true
to your tsconfig. It won't necessarily help you remember to call a super method but it will force you to be explicit about overriding methods, throwing a compilation error in cases where you override a method without using the override
keyword. It is then up to the developer to decide if the super method needs to be called or not.