5

Even tought Javascript is not a strong typed language I would like to tell VSCode about the type of some variables.

Like the snippet code in image below I would like to tell VSCode that task_id is a string variable.

I tried to specify it using a comment before the variable declaration (I've see this trick from this answer, but that apparently doesn't work with VSCode. Is there any VSCode plugin or something else to do that?

enter image description here

alexpfx
  • 6,412
  • 12
  • 52
  • 88
  • 1
    Either define the callback somewhere else and document it (you can use the plugin "document this" for javascript in vscode), or use typescript instead. There isn't much else to do. Like this: http://prntscr.com/mk18xx . Plugin: https://marketplace.visualstudio.com/items?itemName=joelday.docthis ; use `ctrl + alt + D + D` to document. – briosheje Feb 12 '19 at 12:24
  • 2
    VSCode is perfectly able to recognize JSDoc (http://usejsdoc.org/) – Seblor Feb 12 '19 at 12:31
  • thanks, by moving the and documenting the callback it works fine. – alexpfx Feb 12 '19 at 12:42
  • Please include code as text and not as image. – t.niese Feb 12 '19 at 13:30
  • @t.niese I include an image because I wanted to show that vscode doesn't autocomplete. But I cannot capture the automplete suggestions. – alexpfx Feb 12 '19 at 13:48
  • 1
    @alexpfx you still should add the code as text, and provide the image as additional information. If someone want to use your code to create a complete answer, then the person can't use the copy functionality, but has to type everything. And not everyone is that lucky to have good eye sight, needs e.g. screen reader or has other color setting and I would guess auto completion would be a helpful tool for them too. – t.niese Feb 12 '19 at 13:54

1 Answers1

4

Use the jsdoc documentation, I suggest you to install the document this plugin: https://marketplace.visualstudio.com/items?itemName=joelday.docthis, this will allow you to easily document your (javascript) code.

Just isolate the callback, define the params, then document them following the jsdoc standards.

enter image description here

Otherwise, use typescript instead, which will allow you to define types and so on. https://www.typescriptlang.org/

You will need a transpiler, though.

briosheje
  • 7,356
  • 2
  • 32
  • 54