Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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')?

like image 968
erihanse Avatar asked Oct 02 '17 19:10

erihanse


People also ask

How do I enable IntelliSense in Visual Studio?

To access this options page, choose Tools > Options, and then choose Text Editor > C# > IntelliSense.


1 Answers

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

like image 106
Petr Srníček Avatar answered Oct 17 '22 15:10

Petr Srníček