Wednesday, 28 August 2019

Accessing base class properties in derived class using prototype/Access base class properties in the decorator

I am using typescript and i am writing a custom decorator for one of my angular class. I want to access the base class method in the child class decorator. Or access base class methods using the child class prototype. Is there any way to do this? Problem explained in detail below.

Scenario

I have a base class which is like

export class Base {
    public init() {
        console.log('My base class function');
    }
}

And i have a derived class which extends this base class

export class Child extends Base {

}

What i am trying to do

I am trying to write a decorator for the derived class something like

@TestDecorator(['init'])
export class Child extends Base {

}

which will call the init method from the base class.

What is the issue

To do the above scenario, i have written code something like below

export function Tool<T extends Base>(methods: any[]) {
    return function (target: Function) {
        methods.forEach((item) => {
            if (item === 'init') {
                target.super.init() // Stuck here
            }
        })
    }
}

I am not understanding how to make the following line work

target.super.init() // Stuck here

Please help me with the solution. I am stuck. Thanks



from Accessing base class properties in derived class using prototype/Access base class properties in the decorator

No comments:

Post a Comment