Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I access a static property in typescript from an instance of a subclass?

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 HSManagedObjects 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;
}
like image 639
Samantha John Avatar asked Aug 05 '15 14:08

Samantha John


People also ask

How do you define a static property in TypeScript?

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.

Does TypeScript support static classes if not why?

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.

Does TypeScript support static classes?

The class or constructor cannot be static in TypeScript.

How to use static properties 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.

How to access the static field Pi in typescript?

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?

What is the use of classes in 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.

How to access the static members of a class in Java?

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.


1 Answers

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

like image 84
SVSchmidt Avatar answered Sep 19 '22 17:09

SVSchmidt