Accessing parametric type from JS

#1

Hi,

If I have the following type:

type MyType<V> {
      getObj : function(id: string) : V js server
}

How do I access V inside the body of the getObj function? For instance if I want to do this:

function getObj(id) {
     return V.get(id);
}

(This is a simplified example but I need a reference to V to perform fetches & computations on instances of V).

Thanks

0 Likes

#2

You want to use this.

if your function is called as:
MyType.getObj()
then this will be the type MyType.

e.g.

function getObj(id) {
     return this.get(id);
}

If you also say type OtherType mixes ValueType<Foo>
then when OtherType.getObj() is called, this will be the type OtherType

(for member funcitons, this is the instance of the type on which the function is called)

0 Likes

#3

I’m not sure to understand your answer, Riley. I want to access V, not MyType.
In my example the return type of getObj is V, not MyType (or MyType). Therefore, this will be of type MyType but I need to call a static function on V (to fetch objects for instance).

0 Likes

#4

If you have a type, and you wish to get the mixin bindings, you could call something like:

PhysicalMeasurementSeries is defined as:

entity type PhysicalMeasurementSeries extends BasePhysicalMeasurementSeries mixes TimeseriesHeader<Measurement> type key 'PMS' {

PhysicalMeasurementSeries.mixins()[2].bindings()['TDP'].name()
will give
Measurement

So to mix the two , you could do

this.mixins()...
0 Likes

Determine the type used in parametric type
#5

Your latest suggestion seems to work. To access the actual type (so that I can run functions on it), do you suggest using .dereference() ?

ie.:

PhysicalMeasurementSeries.mixins()[2].bindings()['TDP'].dereference().get(id)
2 Likes

#6

Yes that is how you get the actual type instance.

0 Likes