Use case:
I have a base class from which many other classes inherit.
The base class is called HSManagedObject
.
I have another class called HSContext
that keeps a dictionary of HSManagedObject
s where the keys are the names of the various subclasses and the values are lists of the instances of those subclasses. I insert them like so:
insertObject(object: HSManagedObject) {
this.projectObjects[object.key()].push(object)
}
Because class names go away when I minify my javascript (they all become t
), I added a static property to each of those classes called key
that uniquely identifies the class in question.
When I add an object to the dictionary, I would like to infer the class name of that object from the instance. Is there a way to get the static key
variable just from the instance when I don't know which subclass it belongs to?
Currently I am adding an instance method to each of the subclasses called key()
that returns the class's static key
value and calling the instance method to get the class value. It seems like I shouldn't need to do this though. So in all of my subclasses I have some code like this:
static key = "HSRule";
key() {
return HSRule.key;
}
You can't define a static property on an interface in TypeScript. Say you wanted to change the Date object, rather than trying to add to the definitions of Date , you could wrap it, or simply create your rich date class to do the stuff that Date doesn't do.
TypeScript doesn't allow a static property or method to be affected by an object instance. We can instantiate the object just fine, but if we try to access the property or method, TypeScript will raise an error.
The class or constructor cannot be static in TypeScript.
Starting from ES6, typescript supports static properties. static members can be accessed without creating an object of a class. We just need the class name and member name to access it using a dot notation. We can have any method or any field of a class as static. static keyword is used to mark one value as static.
As you can see, the static field pi can be accessed in the static method using this.pi and in the non-static (instance) method using Circle.pi . The class or constructor cannot be static in TypeScript. Want to check how much you know TypeScript?
Classes in TypeScript, as in JavaScript, are a special syntax for its prototypical inheritance model that’s a comparable inheritance in class-based object oriented languages. Classes are just special functions added to ES6 that are meant to mimic the class keyword from these other languages.
The static members of a class are accessed using the class name and dot notation, without creating an object e.g. <ClassName>.<StaticMember>. The static members can be defined by using the keyword static. Consider the following example of a class with static property. The above Circle class includes a static property pi.
This may be tricky. If TypeScript is compiled down to JavaScript, the "classes" become simple variables and the static vars are just assigned to them. You could try something like this:
Object.getPrototypeOf(object).constructor.key
getPrototypeOf() Reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With