What is the best way to categorize variables?

How can I organize variables together:

export let findbyc$: Observable<Object>;
export let findbyi$: Observable<Object>;
export let findbyo$: Observable<Object>;
export let findbyob$: Observable<Object>;

I would like to group them as follows:

const ChildObservers {
    export let findbyc$: Observable<Object>;
    export let findbyi$: Observable<Object>;
    export let findbyo$: Observable<Object>;
    export let findbyob$: Observable<Object>;
}

Then, at a later point in the application, I intend to use it like this: ChildObservers.findbyc$

Answer №1

If you're looking to define a set of properties that are grouped but not initialized, consider creating an interface and exporting a variable of that type.

interface ChildObservers {
    findbyc$?: Observable<Object>;
    findbyi$?: Observable<Object>;
    findbyo$?: Observable<Object>;
    findbyob$?: Observable<Object>;
}

export const observers: ChildObservers = { };

You can then initialize these properties as needed.

observers.findbyob$ = this.httpClient.get();

Additionally, I recommend making the interface generic to match the generic argument of the Observable<T>.

interface ChildObservers<TResult> {
    findbyc$?: Observable<TResult>;
    findbyi$?: Observable<TResult>;
    findbyo$?: Observable<TResult>;
    findbyob$?: Observable<TResult>;
}

export const obsersers: ChildObservers<Object> = { };

Make sure to initialize it correctly

obsersers.findbyc$ = this.http.get<Object>();

Answer №2

export class ChildObservers {
  public constructor(private readonly httpService: HttpService){}

  public fetchDataByCategory$ = (): Observable<Object> => {
    return this.httpService.get();
  };

  public fetchDataById$ = (): Observable<Object> => {
    return this.httpService.get();
  };

  public fetchDataByOwner$ = (): Observable<Object> => {
    return this.httpService.get();
  };

  public fetchDataByObject$ = (): Observable<Object> => {
    return this.httpService.get();
  };
}

// Example for using the APIs
class MyComponent {
  public constructor(private readonly childObservers: ChildObservers){}

  public callApiFunction(){
    this.childObservers.fetchDataByCategory$.subscribe(response => {
      console.log(response);
    });
  }
}

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

How do I insert a new column into the result set of a query in Prisma?

Imagine a scenario where there are two tables: User, which has fields for name and Id, and Post, which has fields for name and content. These tables are connected through a many-to-many relationship (meaning one post can have multiple users/authors and eac ...

Recording the details of an Angular project through the utilization of Compodoc

I am currently in the process of documenting my Angular + Typescript application using Compodoc. To install Compodoc, I utilized npm and executed the following command: 'npm install -g compodoc'. And included "compodoc": "./node_modules/ ...

The default filter in Angular Material Autocomplete is failing to function as expected

I've implemented the Autocomplete feature from Angular Material in my project, but I'm facing an issue with the search filter. It seems that the default filter provided in the example is not working as expected – instead of filtering based on m ...

Creating global variables in NodeJS allows you to access and modify data

Currently, this construct is being utilized to create a global LOG: declare global { let LOG: Logger; } // eslint-disable-next-line @typescript-eslint/no-namespace declare namespace globalThis { let LOG: Logger; } globalThis.LOG = new Logger(); It f ...

What is the proper way to incorporate ts-nameof in the gulp build process and encounter the error message: 'getCustomTransformers' is a compiler option that is not recognized

Utilizing ts-nameof in my TypeScript files, similar to this example in a .ts file: import 'ts-nameof'; export class MyTsNameOfTest { public testTsNameOf() { const nameString = nameof(console.log); } } My Gulp build task setup - followi ...

Incorporating an alternate object array to update an array of objects: A

There are two object arrays, the main array and the temp array. The goal is to compare the main array with the temp array and update the values in the main array based on matching IDs. In this example, IDs 2 and 3 match in both arrays. Therefore, the valu ...

Auth0 Angular - No routes found to match

I recently set up an angular application and integrated it with Auth0 by following two helpful tutorials: https://auth0.com/docs/quickstart/spa/angular2/01-login https://auth0.com/docs/quickstart/spa/angular2/02-calling-an-api Here is a brief overview o ...

Angular 6 and the intricacies of nested ternary conditions

I need help with a ternary condition in an HTML template file: <div *ngFor="let $m of $layer.child; let $childIndex=index" [Latitude]="$m.latitude" [Longitude]="$m.longitude" [IconInfo]="$childIndex== 0 ? _iconInfo1:$c ...

Angular: Preserve the URL even when encountering a 404 page

Creating a custom 404 page in Angular 4 is something I have recently done, and I am looking for a way to preserve the incorrect URL that was input by the user. To see an example of this behavior, you can visit sites like GitHub. They show a 404 page when a ...

What is the best way to integrate Sass into a Create-React-App project that is using TypeScript

After setting up a new project using create-react-app and typescript, I included a sass file in my project as per the documentation (which mentioned built-in support for sass files). However, when trying to compile, I encountered the following error relate ...

Encountering a Nextjs hydration issue after switching languages

I am facing an issue with my Next.js project using version v12.2.4 and implementing internationalization with i18next. The project supports two languages: Italian and English. Strangely, when I switch to English language, the app throws errors, while every ...

Encountering an "ionic 4 frame-ancestors *" error while attempting to watch a Twitter video

Currently, I am in the process of developing a news app using Ionic 4. I recently tackled the challenge of embedding tweets in Twitter cards successfully. However, a new issue has arisen. When a tweet includes a Youtube video, everything works perfectly ac ...

The functionality of CSS transitions may be affected when dynamically adding a class

Creating a custom CSS for my main div: .main { height: 0%; position: absolute; width: 100%; z-index: 100; background: whitesmoke; bottom: 0; border-radius: 15px; padding: 20px; color: gray; left: 0; right: 0; transition: height 1s e ...

Arranging Select Dropdown Options in a Specific Order using Angular 7 and Typescript

My select dropdown is populated dynamically with options fetched from a service using *ngFor. I am looking to customize the order of these options. Can this be achieved through Angular code? The array structure is as follows: console.log(this.paymentTyp ...

Navigating with hashtags in Angular2 and anchors does not change the page position

I recently came across a helpful post that showed me how to append a fragment to the URL. Angular2 Routing with Hashtag to page anchor Although the fragment is successfully added, I'm encountering an issue where the page does not scroll to the speci ...

Utilizing the power of Angular 4 in combination with mailgun

I need assistance with setting up an email form on my website using Angular 4 and Mailgun as the mail service. I have a method in my mail service file to send messages, but I keep encountering a Bad Request error stating that 'from' is not presen ...

Incorporate Canvg version 4 into a TypeScript project

I am currently facing an issue with an older TypeScript project that has the following tsconfig setup: { "compilerOptions": { "baseUrl": "./src", "outDir": "build/dist", "module": &q ...

Issue encountered: `property does not exist on type` despite its existence in reality

Encountering an issue in the build process on Vercel with product being passed into CartItem.tsx, despite being declared in types.tsx. The error message reads: Type error: Property 'imageUrl' does not exist on type 'Product'. 43 | ...

Leveraging react-share in combination with react-image-lightbox

I have recently inherited a React project and am struggling to integrate react-share with react-image-lightbox. My experience with both React and Typescript is limited, so any assistance would be greatly appreciated. Below are the relevant code snippets: ...

When receiving JSON and attempting to store the data in a variable, I encounter an issue where it returns the error message "undefined is not iterable (cannot read property Symbol

I'm currently integrating the Advice Slip API into my project. I am experiencing an issue when trying to store the JSON data in a variable like so: let advice; fetch("https://api.adviceslip.com/advice").then(response => response.json()). ...