Before version 6.x, in RxJS, the method _subscribe
located in Subject.ts
was marked as @deprecated
but still accessible. In a project I'm currently involved with, someone decided to override this method to perform specific actions whenever someone subscribes to that Subject
. However, in newer versions (7.x), the method is now protected
and marked as @internal
. Even though my class extends the Subject
class, attempting to override it results in an error:
Error: src/app/common/util/updatable-subject.ts:42:11 - error TS4117: This member cannot have an 'override' modifier because it is not declared in the base class 'Subject<T>'. Did you mean 'subscribe'?
42 override _subscribe(subscriber: Subscriber<T>) {
~~~~~~~~~~
Error: src/app/common/util/updatable-subject.ts:43:30 - error TS2551: Property '_subscribe' does not exist on type 'Subject<T>'. Did you mean 'subscribe'?
43 const subscription = super._subscribe(subscriber);
~~~~~~~~~~
node_modules/rxjs/dist/types/internal/Observable.d.ts:50:5
50 subscribe(observerOrNext?: Partial<Observer<T>> | ((value: T) => void)): Subscription;
~~~~~~~~~
'subscribe' is declared here.
This leads to the question of how RxJS manages to hide the function by marking it @internal
? In my attempt to replicate this behavior in a simple example, I was unsuccessful:
// app.ts
import express from 'express';
import {B} from './B';
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
new B().subscribe();
});
app.listen(port, () => {
return console.log(`Express is listening at http://localhost:${port}`);
});
// A.ts
export class A {
/**
* @internal
*/
protected _subscribe() {
console.log("A");
}
}
// B.ts
import {A} from './A';
export class B extends A {
override _subscribe() {
super._subscribe();
console.log("B");
}
subscribe() {
this._subscribe();
}
};
Logs
Express is listening at http://localhost:3000
A
B
Despite running
npx tsc && node dist/app.js
and loading it in the browser (http://localhost:3000/
), the @internal
_subscribe
method remains visible. Is there a way to still override it? If not, are there alternative methods to track when and with what arguments that method is called in RxJS?