Intellisense for JavaScript in in VSCode doesn't recognize the type of this inside a class

1.8k views Asked by At

Is there any way to make VSCode interpret the this keyword inside a member function of a JavaScript class as an instance of said class, thus providing intellisense for it's other members and properties?

I know that it is not necessarily the case when the function gets called, because it isn't automatically bound to the instance, but most of the time (at least in my experience) that is the desirable behavior, so it wouldn't hurt to assume the type of the this object to be a self-reference.

Update: After some time and guessing I think that I have figured out the problem, in fact it wasn't in Visual Studio Code, but in the class that I have written. You can find my solution attached as an answer.

1

There are 1 answers

2
Isti115 On BEST ANSWER

Somehow after waiting for a while (I typed the question and then checked back on VSCode) the methods started to show up, and after looking into one of my other projects I realized that the classes I have written there have their properties showing up as well. After some trial and error I got to the conclusion that properties only show up if they are declared in the constructor, so the cause of my original problem was that I had a separate initialization method that assigned some additional attributes to the object.

Solution: If you need to assign some properties later, that the values of cannot be known at the time of the constructor running (for example because of asynchronous promises in my case), just assign undefined to them and VSCode will pick it up!