0

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

2 Answers2

1

Could something like this be what you're looking for? I think you may want to specify that req is an object with a key of body that is a string.

enter image description here

This may also be a helpful response to check out: https://stackoverflow.com/a/31573441/21533064

  • Thank you for the post! But this didn't work and I don't think it's what I'm looking for. In my case, I have a function call (`app.post`) and I supplied an arrow function as an argument, as opposed to declaring a new function named `post`. – Diamond Block Apr 02 '23 at 19:45
1

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

Zachary Duvall
  • 304
  • 1
  • 9