What sets apart a private static function from a public static function in TypeScript?

Exploring the nuances of angular2 services: what distinguishes a private static function from a public static function in typescript?

  public static getUserStockList(): Stock[] {
    /* TODO: implement http call */
    return WATCHLIST;
  }

vs.

  private static getUserStockList(): Stock[] {
    /* TODO: implement http call */
    return WATCHLIST;
  }

QUERY:

Under what circumstances would opting for a private static function be more suitable than a private function?

Answer №1

One interesting aspect of private static methods is that they can be called from instances of the same class.
Here's an example to illustrate this:

interface Info {
    // ...
}

interface SecureInfo {
    // ...

abstract class MyObject {
    protected constructor(info: SecureInfo) {
        // ...
    }
}

class AnotherObject extends MyObject {
    private static processInfo(info?: Info | SecureInfo): SecureInfo {
        // ...
    }

    constructor(info?: Info | SecureInfo) {
        super(AnotherObject.processInfo(info));
    }
}

The method AnotherObject.processInfo can be accessed by both the instance and the compiler without any issues.
However, attempting to do this:

console.log(AnotherObject.processInfo({}));

Will yield the following error message:

Property 'processInfo' is private and can only be accessed within the 'AnotherObject' class

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 my Node.js server to my Angular application

I encountered an issue while trying to upload an image using Angular and Node.js on the server-side. Unfortunately, when attempting to view the webpage, I am unable to see anything. Here is the browser output: https://i.stack.imgur.com/t9MrF.png Below is ...

Pausing repetitive HTTP requests in an Angular 6 project within a do while loop

While waiting for the completion of one API call, I am recursively consuming an external API. The http calls are being made using import {HttpClient} from '@angular/common/http' As a newcomer to the framework, there may be something wrong in the ...

In the latest version of Expo SDK 37, the update to [email protected] has caused a malfunction in the onShouldStartLoadWithRequest feature when handling unknown deeplinks

After updating to Expo SDK 37, my original code that was previously functioning started encountering issues with <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0e7c6b6f6d7a23606f7a67786b23796b6c78667f79627a">[email prot ...

Challenges associated with adding information to FormData in an Angular Net 5 Application

I'm currently working on developing an application using Angular 11 and .NET 5 and am facing some challenges while trying to implement the file upload feature. I have successfully managed to send pictures to the server, but I'm encountering issue ...

Experiencing a problem in React JS when trying to render a component?

I encountered an error message while trying to render a component from an object. Type 'FC<Boxprops> | ExoticComponent<{ children?: ReactNode; }>' is not compatible with type 'FC<Boxprops>'. Type 'ExoticComponen ...

Combining multiple 'Eithers' and 'Promises' in fp-ts: A guide to piping and chaining operations

Recently, I began working with fp-ts and wanted to create a method with functional-like behavior that would: Parse a bearer token Verify the validity of the user using the parsed token import { Request } from 'express'; import { either } from & ...

Tips for implementing a filtering function within an array of objects

I am struggling to implement a filter search feature in my ionic mobile application for an array of objects. The array is structured like this: initializeItems() { this.items = [ {Place:"Dumaguete city", date:"February 2 2018"}, {Place:"Sibulan", date: ...

Utilizing a directive in contexts beyond a component

I have developed a popover module that consists of two components and three directives. However, I am encountering an issue where I am unable to utilize the directives outside of the main component. Whenever I try to do so, I face an editor error stating: ...

When attempting to open a form in edit mode, data binding fails to work across multiple form controls

When clicking on the edit button, data is loaded into the form using [(ng-model)], however, it seems to be working correctly only for some fields and not all fields. The data is displayed in only a few fields despite receiving data for all fields. Below is ...

Creating an array of objects data is a breeze with Vue.js

I have an array of data containing selected items, and I need to extract the IDs from this array into a new array so that I can send only the IDs to the back-end. Sample Code method toggleSelection(rows) { console.log('this.multipleSelection : &a ...

What is the best way to ensure that the operations are not completed until they finish their work using RX

Is there a way to make RXJS wait until it finishes its work? Here is the function I am using: getLastOrderBeta() { return this.db.list(`Ring/${localStorage.getItem('localstorage')}`, { query: { equalTo: fa ...

Transforming every piece of TypeScript code into JavaScript on the server-side

I'm relatively new to developing Node.js applications for production. I've created an app using TypeScript for the backend of my Node.js project. Currently, in the package.json file, I have the following script section: "scripts": { ...

Navigating through CloudFront to connect with AWS WebSocket

After following the instructions in this URL to configure Amazon CloudFront CDN for WebSocket connection, , I am struggling to connect WebSocket through my Angular app. I have attempted two ways but haven't been successful. let data = this.httpClien ...

I'm having trouble opening a new Angular project even though both my Node.js and npm are up to date. Can anyone help me

Just starting my Angular journey. I have successfully installed the latest version of node.js with npm and then added Angular CLI to it. All good until I typed this command in the node.js prompt: ng new my-app But now I'm stuck here! Any ideas on wh ...

Is it possible to utilize types as constants in a switch statement?

In my file called checkoutTypes.ts, I have defined some checkout types like this: export type CheckoutInvoiceAddressSection = "InvoiceAddress"; export type CheckoutDeliveryAddressSection = "DeliveryAddress"; export type CheckoutDelivery ...

How to pass a distinct identifier to an Angular 2 component?

Encountering an issue with Angular2 where I need to assign a unique identifier to my reusable component. Seeking assistance from the community. account.html <div class="container"> <!-- ACCOUNT INFO --> <div class="row"> ...

Supply a variety of contexts to ngIf

Consider this template example. I am creating a user context using the as syntax and want to pass it into the userTmpl. It seems like there is no direct way to pass context beyond what is specified in the ngIf condition, which in this case is the showUser ...

Accessing files from various directories within my project

I'm working on a project with 2 sources and I need to import a file from MyProject into nest-project-payment. Can you please guide me on how to do this? Here is the current file structure of my project: https://i.stack.imgur.com/KGKnp.png I attempt ...

RxJS: Ensure Observables emit values sequentially, waiting for the completion of the previous Observable

In my current project, I have been working on implementing a unique Angular structural directive. This directive is designed to read and store the text content of an HTML element along with all its children, remove the contents upon AfterViewInit, and then ...

My goal is to design a dynamic form consisting of various components, ensuring that all required fields are validated upon the user's submission

I am trying to set up an Angular reactive form with multiple sub-components. My goal is to validate both the parent and child form components at once when the user clicks the Submit button. Currently, I am running into an issue where error messages only ...