Search code examples
javascriptexpressvisual-studio-codejsdoc

JSDoc Type hinting for express js request body


How do I get VSCode's intellisense to type hint a request's body (Express.js)?

For example here I'm trying to type hint body as a string, but I don't see any string methods/suggestions show up:

enter image description here

Here is what I want (a test example):

enter image description here


Solution

  • I believe the previous answer may in fact be a good solution for you.

    JSDoc works with intellisense when applied where a method/function/variable is defined rather than where it's used. Unfortunately, you won't be able to give intellisense to parameters of app.post (including a callback function) as you have it, because you are invoking app.post rather than defining it.

    Since the second argument to app.post is a callback function, and you want to have intellisense inside of it, your best bet is probably to define that function separately (instead of passing it as an anonymous function) so you can give it a JSDoc.

    Alternatively, you could look into using typescript which has typed intellisense built in for most commonly used packages.

    enter image description here