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 ?.
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.