Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
How to add a public or private property in the class using public method in ts

I am trying to add a public or private variable in the class definition on calling a public method of a class for example in the class definition given below.

export default class Container {
  public addFlag(flagName: string): void {
     // create a new private or public property in class and assign a value to it.
  }
}

If I make an instance of container class and call addFlag() on that instance, I want to add a public or private variable in the class definition and assign a value to it, how can i achieve this in Typescript ?.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Make your class become dynamic object with [index signature][1]

class Container {
    [key: string]: any;
    public addFlag(flagName: string): void {
        this.flagName = flagName;
   }
}

const container = new Container();
container.addFlag("abc");
console.log(container.flagName); // "abc" 

But index signature can't use Access modifiers: public, private or protected.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!