Customizing a private method within RxJS

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?

Answer №1

@internal functions are excluded from the output when the stripInternal option is activated in the tsconfig.

As a result, these functions will not be included in the generated d.ts file and will not be visible in the type definitions.

I strongly recommend against relying on private methods.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Angular : Is toFixed() functioning properly with one value but not with the other one?

My form has 2 inputs, each calling the calculeSalaire() function. calculeSalaire() { this.fraisGestion = this.getFraisGestion(); this.tauxFraisGestion = this.getTauxFraisGestion(); this.salaireBrut = this.getSalaireBrut(); this.salaireNet = this.g ...

Passing a service into a promise in Angular 2 using TypeScript

Is there a way to pass a service into a promise? I am currently working on a promise that will only resolve once all the http requests are complete. However, I am facing an issue where this.jiraService is undefined. Is there a method to pass it to the co ...

Exploring ways to list interface keys in order to create a new interface where the value is determined by the corresponding key

How can we iterate through interface keys to create a new interface where the value is dependent on the key? type IParse<T> = { [K in keyof T as K extends string ? K : never]: string // How can we specify that if K === 'a', the type sho ...

Error message encountered: Typescript, Webpack, angular - Unable to execute due to TypeError: Object(…) does not operate as

Just starting out with TypeScript and Angular, I've been tasked with creating a typedefinition for a custom library. Despite confirming that the JavaScript from my external library is loading correctly in the resources, I encountered an error when cal ...

Using React with Typescript: Passing Props and Defining Data Types

As a relatively new TypeScript enthusiast, I find myself facing some challenges. Specifically, I am struggling with errors in my code and uncertain about how to properly pass props and select the correct type. Any guidance on this matter would be greatly a ...

Styling with CSS in Angular 2+ can be quite challenging

Hey there, I'm new to Angular 4 and running into some troubles with styling my application. I tried adding a background image to the body, which worked fine, and then added a component to display content, also looking good. Now, when I added a second ...

Images in the Ionic app are failing to display certain asset files

Currently, I am working on an Ionic 4 app for both Android and iOS platforms. The issue I am facing is that only SVG format images are displaying in the slide menu, even though I have images in both SVG and PNG formats. public appPages = [ { ...

What is the process for comparing two objects in TypeScript?

There is a unique class named tax. export class tax { private _id: string; private _name: string; private _percentage: number; constructor(id: string = "", taxName: string = "", percentage: number = 0) { thi ...

RouteConfig not found in Vue3 Router when using Typescript

After initializing a new Vue project using @vue/cli $> vue create my-project, I opted for Typescript and router options, then updated to vue3 beta using $>vue add vue-next. Unfortunately, when running $>npm run serve, I encountered the following ...

Discovering a specific property of an object within an array using Typescript

My task involves retrieving an employer's ID based on their name from a list of employers. The function below is used to fetch the list of employers from another API. getEmployers(): void { this.employersService.getEmployers().subscribe((employer ...

How to customize Material UI Autocomplete options background color

Is there a way to change the background color of the dropdown options in my Material UI Autocomplete component? I've checked out some resources, but they only explain how to use the renderOption prop to modify the option text itself, resulting in a a ...

Techniques for returning errors to the calling function using async functions

I am currently encountering an error where if "dateofBirth" is not found, an empty object is sent back to the client. How can I change this so that an error object is sent back instead of an empty object? Essentially, I want to send back a catch process. ...

The intermingling of two Generics can lead to an unexpectedly deep and potentially limitless Type instantiation

My experience with Typescript is still new, and I could use some guidance in resolving the error message Type instantiation is excessively deep and possibly infinite.(2589) You can access the playground here. To identify the error, uncomment the Type Exp ...

Checking for unnecessary properties in Typescript with vue-i18n

Let's consider two different languages represented in JSON format: jp.json { "hello": "こんにちは" } ge.json { "hello": "hallo", "world": "welt" } Now, we are going to com ...

Issue Encountered When Attempting to Reuse Component Loaded Dynamically

Recently, I developed a component that has the ability to load a child component dynamically with a template retrieved from the server. If you are interested in similar topics, check out this discussion here: Compile dynamic HTML in Angular 4/5 - somethin ...

Exciting Angular feature: Dynamic Titles

I am working with an <i> tag <i class="actionicon icon-star" [ngClass]="{'yellow' : data.isLiked}" (click)="Like(data)" aria-hidden="true" title="Liked"></i> In my current set ...

What is the best way to divide an HTML table into two sections?

I'm looking to organize my data into a table with two separate sections, one on the left and one on the right. For example, if I have 20 rows of data, I want to display the first 10 rows on the left side with headers and the remaining 10 rows on the r ...

Improving event observation efficiency with Observable.fromEvent on the browser window

Within my file manager UI, each individual file item is currently set to monitor the window's wheel event. As soon as a file item comes into view on the page, its corresponding image loading process will be initiated by the file item component. While ...

The ngOnInit function is not triggered upon instantiation of an Injectable class

What could be causing the ngOnInit() method not to be called upon resolution of an Injectable class? Code import {Injectable, OnInit} from 'angular2/core'; import { RestApiService, RestRequest } from './rest-api.service'; @Injectable ...

Issues with executing code within the react package for Yahoo Finance 2

I am currently in the process of developing a React application using Vite. The purpose of my app is to retrieve stock-related information from Yahoo using the yahoo-finance2 package. Interestingly, when I run the code as a standalone JavaScript file, eve ...