简体   繁体   中英

Getting intellisense on function parameter object types in javascript in vs code

I'm trying to specify the types of req and res, to get intellisense to work on the parameters as I'm trying to illustrate below, but I'm not sure how to accomplish it.

var http = require('http');
http.createServer(foo).listen(8081);

/**
 * @? ? ? 
*/
function foo(req, res) {
    req.<intellisense context menu>
}

I've looked through some of the documentation at https://github.com/Microsoft/TypeScript/wiki/JavaScript-Language-Service-in-Visual-Studio and https://github.com/Microsoft/TypeScript/wiki/JsDoc-support-in-JavaScript , but don't know if this is even possible. I'm a beginner at javascript, can somebody point me in correct direction?

Bonus question: How can I make this work also if foo is in another module/file, and that module doesn't in itself require('http') ?

This is a very old question but since I stumbled upon it while looking for the same thing, here is an answer:

You need to specify the type of the parameter after @param in curly brackets like this:

/**
 * 
 * @param {XMLHttpRequest} req Some XMLHttpRequest
 * @param {string} res (Let's suppose res is a string)
 */
function foo(req, res) {
req.<intellisense context menu appears>
}

More info (with a nice video demonstration) is available in Visual Studio Code documentation:

https://code.visualstudio.com/docs/languages/javascript#_js-doc-support

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