Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create hints for personal functions in VS Code

I want to make a personal library with all my js functions written.
What I really need is that I need the intellisense of VS Code to show me more hints about this functions - type of the input variables, type of the return, maybe some information about the function. For example - a hint for substring looks like this: enter image description here
and a hint for my function looks like this: enter image description here

Digging a bit in VS Code I was able to find, that there are .ts files with declaration of objects, interfaces and functions (for example declaration of substring() in lid.es6.d.ts, etc.) enter image description here
My first thought is whether I can make a personal .ts file with declaration of all my functions and make the intellisense refer to it too.

like image 528
Bobbie Avatar asked Nov 29 '17 09:11

Bobbie


People also ask

What are inlay hints in VS Code?

Inlay hints add additional inline information to source code to help you understand what the code does. This can help you understand the meaning of each argument at a glance, which is especially helpful for functions that take Boolean flags or have parameters that are easy to mix up.

How do I show suggestions in Visual Studio?

The suggestion list of Basic completion appears when you press the default Visual Studio IntelliSense shortcut Ctrl+Space . If necessary, you can always return to the Visual Studio's native's IntelliSense.


1 Answers

You don't need a .d.ts for your javascript functions, you can just use a jsDoc comment. Ex:

/**
 * Does stuff
 * @param {Number} params 
 */
function myFun(params) {


}

Between {} is the type of the parameter. You can also add a description to your function.

VsCode will pick-up JsDoc comments and use them to show you hints. Also if you type /** and then press tab above a function it will insert a property formatted jsDoc comment base on your function to give you a starting point.

JsDoc reference here

Note: Give typescript a try, you will get the nice hints you are looking for because Typescript allows you to specify the argument types, also the output of typescript is plain TS, so you will be able to use your library anywhere you could have used it before

like image 171
Titian Cernicova-Dragomir Avatar answered Sep 25 '22 10:09

Titian Cernicova-Dragomir