Passing "this" to classes initialized within another class in Typescript

Asked

Viewed 55 times

1

INTRO:

I’m relatively new to Typescript (a few weeks of practice), I’m slowly learning superset and using it in conjunction with Electron. I was making a extend class Browserwindow to create extra features and porting part of my old code in pure JS to Typescript, when a doubt arose, possibly for some bad practice or for the TS not accept my logic ...

DOUBT:

I am initiating other classes within the class in which I give the extend of Browserwindow:

(the snippet is only to exemplify the code, since it will return error);

class Worker extends BrowserWindow {

    public mouse: Mouse = new Mouse(this);   

    constructor(options: object) {
      super(options);
				
    }
};

she takes as argument the this which would be the "parent" class to have direct access to all functionalities of the Worker and of Browserwindow such as .getBounds() which returns the window area on the user monitor.

However, as the this for the class Mouse i have no idea of the correct way to declare it within the class, what type I should use, or the correct way to do it, the idea is to not extend the class further.

(Again, snippet only for code exemplification)

export class Mouse extends Steering  {    
    
    // Qual tipo devo declarar a variavel???
    public window: ???;     
    
    // Qual tipo devo passar o parametro ???
    constructor (window: ???) {           
        
        super (new Vector(0, 0));           

        this.window = window;        

    }
}

Passing the type any for the argument window compiler compiles without errors, but I no longer have one of the greatest qualities of Typescript, because any wrong code I write it will pass without validation.

What would be the right way to pass the this for my class Mouse? What type do I use? My logic is incorrect?

Grateful!

  • Guy tries to use any or Object. I have not tested to see if it works

  • I have tried... and still can not access the methods of the class "father", besides returning me a compilation error ...

  • Which build error returns?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.