CodeDevStack

TS-check and jsdocs with VSCode

March 02, 2020

The magic of types

I’ve been using Java at work for almost six months and, to be honest, I got used to types and intellisense being useful.

At the same time, I really like Javascript as is, so when I found out I could get VS code to help me with my types and still not use typescript, I had to try it.

So here’s what you need to do:

  • Use //@ts-check
  • Use JSDocs when infering types is not enough

How does this work? Just add //@ts-check at the very top of the files where you want to implement type checking. Easy right?

And then, if you have function and want to define types, use JSdocs

/**
 * My function
 * @param {String} myFirstParam
 * @param {Number} mySecondParam
 */
function myFunction(myFirstParam, mySecondParam) {
  return;
}

The final code should look something like this

//@ts-check

/**
 * My function
 * @param {String} myFirstParam
 * @param {Number} mySecondParam
 */
function myFunction(myFirstParam, mySecondParam) {
  return;
}

Now if you try to use the function while writing some kickass code you are gonna get:

Some intellisense magic 0. Magic! Awesome typed magic.