2

I have an issue when trying to document javascript methods,

I tried this: enter image description here

I really don't think I should be setting a property for getMother() but rather a method.

Yet I don't know if it's even supported (I'm using VS Code).

So the given example might work, but it doesn't add parenthesis when autocompleting and just isn't clean because we are seeing it as a property.

I spent a few hours searching for a solution and failed. I hope someone knows the answer, feel free to ask for more details.

1 Answer 1

1

You can try something like this:

// @ts-check

/*
 * @typedef {{
 * age: number,
 * getMother(arg1: string, arg2: string): HTMLCat
 * } & HTMLElement} HTMLCat
 */

/*
 * @type {HTMLCat}
 */
var cat;

cat.getMother('a', 'b').age

I'm not sure what AdditionalCat is doing exactly since it's just an object, which would likely correlate with the key/value index I added, so I left it out. But this syntax might be a bit easier to reason about.

You can find more examples here: https://medium.com/better-programming/type-checking-in-vscode-without-typescript-eba92161cd08

Sign up to request clarification or add additional context in comments.

2 Comments

Great answer! I'm wondering why it was so hard to find a good example / tutorial. I hope that maybe someone else will find it usefull.
I found out that adding optional parameters is as simple as "name?". I was confused because I didn't know that there are two types of notation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.