简体   繁体   中英

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: 在此输入图像描述
and a hint for my function looks like this: 在此输入图像描述

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.) 在此输入图像描述
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.

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

Yes you could write your own typings. If you don't want to use TS, you can use the JSDoc standard:

/**
 * Get a survey's details and possible answers by id
 * @param {Number} surveyId
 */ 
const getById = async (surveyId) => {
    ...
}

VSC is smart enough to read all that and display it when implementing. Especially when you build services which are exported and the imported this works great.

JSDoc info: http://usejsdoc.org/about-getting-started.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM