skip to Main Content

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

Answers


  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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search