Create a function to set initial values for variables in a TypeScript class

I have the following code snippet in my constructor to initialize the variables.

public Elements: Observable<dcElementDataStoreItem>;
private elementSubjectData: BehaviorSubject<dcElementDataStoreItem>;

constructor() {
  this.elementSubjectData = new BehaviorSubject([]) as BehaviorSubject<ElementDataStoreItem>;
  this.Elements = this.elementSubjectData.asObservable();
}

Currently, there are only 2 lines of code for initialization but I may have multiple similar items to set up. Is it possible to optimize it like the proposed code below (although it does not work)?

constructor() {
  this.initSubjectData<dcElementDataStoreItem>(this.elementSubjectData, this.Elements);
}

private initSubjectData<T>(privateList: BehaviorSubject<T>, publicList: Observable<T>) {
  privateList = new BehaviorSubject([]) as BehaviorSubject<T>;
  publicList = privateList.asObservable();
}

This way, I aim to make use of the initSubjectData method for initializing all other potential elements.

Answer №1

Unlike some other programming languages, TypeScript does not allow passing a field "by reference." However, you can work around this limitation by passing in the field names instead and accessing them via indexing. This approach ensures type safety since TypeScript supports the concept of a type that is a key of another type.

public elementSubjectData: BehaviorSubject<ElementDataStoreItem>;
public Elements: Observable<ElementDataStoreItem>;

constructor() {
    this.initSubjectData("elementSubjectData", "Elements");
}

private initSubjectData(privateListField: keyof this, publicListField:  keyof this) {
    let privateList = new BehaviorSubject([]);
    this[privateListField] = new BehaviorSubject([]) as any;
    this[publicListField] = privateList.asObservable() as any;
}

However, there are two drawbacks to this method:

  1. There is no check on field type.
  2. The properties must be public for the field to be a keyof this.

We can address issue number 1 by introducing a second function that returns a typed representation of the field:

interface Field<TName, TType> {
    name: string;
    set(value: TType): void;
}

// Inside the class
constructor() {
    this.initSubjectData(this.field("elementSubjectData"), this.field("Elements"));
}

field<TKey extends keyof this>(name: TKey): Field<TKey, this[TKey]> {
    return {
        name,
        set: (value) => {
            this[name] = value
        }
    };
}

private initSubjectData<T>(privateListField: Field<keyof this, BehaviorSubject<T>>, publicListField:  Field<keyof this, Observable<T>>) {
    let privateList = new BehaviorSubject([]) as BehaviorSubject<T>;
    privateListField.set(privateList);
    publicListField.set(privateList.asObservable());
}

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

Having trouble connecting Nextjs with ChromaDB?

I am encountering issues while trying to establish a connection with the Chromadb vector database in Nextjs. The objective is to store user-generated content in Chromadb. Below is the code snippet I am utilizing along with its dependencies: Dependencies V ...

Issue with displaying ngRx/store Observable values in Angular template

Using examples from the ngRx documentation, I am implementing a redux model in our Angular application. The code functions correctly, with actions firing and updating the store as expected. However, I am facing an issue where the template remains empty, an ...

What is the best way to organize information within a template using a *ngFor loop without needing to include functions directly in the template?

When working with Angular 11, I encountered a scenario where a Component receives an array of data from an external source that needs to be displayed in a template using a *ngFor loop with custom formatting. While my initial approach involved creating func ...

How can I delete the black background from the ion-tab-bar in Ionic 7?

In my current project using Ionic 7, I have a navigation guide set up with 4 tabs. I am trying to customize the styling of these ion tabs by adding some custom CSS. The issue I'm facing is that despite my attempts to make the background transparent, ...

I am having trouble accessing my JSON data via HTTP get request in Angular 2 when using TypeScript

I am working on developing a JSON file configuration that can be accessed via HTTP GET request in order to retrieve the desired value from the config file and pass it to another component. However, whenever I try to return the value, it shows up as undefin ...

Errors in TypeScript are being brought up by using if-else statements inside a loop

I am currently working on a function to retrieve referral codes from users. The user inputs a code, which is then checked against the database to see if it exists or not If the code provided matches the current user's code, it should not be accept ...

Angular Material Themes - retrieve the current main color shade

My current setup includes two themes: Theme 1: Light $candy-app-primary: mat-palette($mat-blue, A700); $candy-app-accent: mat-palette($mat-orange, A200, A100, A400); $candy-app-warn: mat-palette($mat-red); $candy-app-theme: mat-light-theme($candy-app ...

What is the best way to iterate through a collection of two or more arrays in order to determine the total length of all

https://i.stack.imgur.com/PpFlB.pngI currently have multiple Arrays containing various inputs this.listNumber = [ { "GenericQuestions": [ { "input": "long", }, { "input": & ...

Ways to modify environment variables in Angular 6 without the need to rebuild

Typically, I store my API URLs in the environment.ts file. However, when deploying builds to multiple clients with different API URLs, I find myself creating separate builds for each client after adjusting the environment variables. Is there a solution th ...

The error in React TypeScript with react-markdown: "No matching overload found for this call

While I'm open to trying out different libraries, I've heard good things about react-markdown. Unfortunately, I'm encountering an issue when attempting to use it with TypeScript in a functional component. import React from 'react' ...

Switch branches to projects without node_modules installed

Is there a better way to checkout a project that had node_modules in .gitignore? I have 2 computers and currently follow the following steps: Commit the project from computer 1 to GitHub Create a new angular project with the same name and folder structur ...

Generate a collection of elements using a different collection as a reference

I am struggling with an array of objects: let data = [{ createdDate: "2222", email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="087c6d7b7c3d487c6d7b7c266b6765">[email protected]</a>", histories: [ ...

Unable to download files from Angular frontend connected to Spring backend, experiencing corruption or malfunction

I am facing an issue with downloading files (.odt) stored in a mysql database. Here is the code snippet for handling this operation in my project: Backend (using Spring, not Spring Boot): @RequestMapping(value = "/downloadRequest", method = Requ ...

Transferring properties to components within ng-content (attributes that are not accessible in the component where the code is located)

I am in the process of creating a carousel. The goal is for the developer to simply write all the markup in one place (let's say in app.component.html) with just an options property, and then have the carousel take control. The issue lies in the fac ...

Angular appears to be having trouble with localStorage functionality

Having an issue with my service that interacts with a local NOTES object array using localStorage. Whenever the page refreshes, the previously entered data is lost and only the initial data in the const NOTES array remains. Can't seem to figure out wh ...

Encountering Main.js Path Error in Asp.net Core and Angular 2 Integration

I am currently in the process of developing an asp.net core application with Angular 2 and utilizing Angular Quickstart for assistance. With the flexibility provided by Asp.Net core application to use wwwroot as a folder, I have consolidated all dependenc ...

Change number-type object fields to string representation

I am in the process of creating a function that takes an object as input and converts all number fields within that object to strings. My goal is for TypeScript to accurately infer the resulting object's fields and types, while also handling nested st ...

What is the best way to exhibit information from a lone table column in a modal using Angular 7?

Is there a way to have an "edit" button beside each row in the appointments table that triggers a modal popup allowing users to change appointment dates? Unfortunately, I'm facing an issue where the modal does not pop up and my screen turns white. ** ...

The mat-table is failing to fetch data from the API, despite the API functioning properly

After searching for answers on this site, I was able to find some information but still couldn't resolve my issue. Despite reading through many resources here, I'm still stuck. I am struggling to populate a mat-table with my code. Here is the sn ...

Settings for various filters

I'm facing an issue while trying to set parameters in the URL for table filtering. I have created a reusable method to handle this, but run into trouble when dealing with multiple objects for parameter setting. Here is a glimpse of my default paramet ...