To start, you can add a file called “jsconfig.json” to the root of your project. You’ll need to add some compiler options, and set “allowSyntheticDefaultImports” to true.
This allows the VSCode precompiler to get type information about your variables and functions, presenting you with handy information when you hover over a symbol.
You can get type information about primitives, or even more complex objects.
And your es6 class declarations too!
The synthetic type inference is smart and can infer return types of expressions and function calls.
You can import libraries and modules and still have the benefits of type inference and completion, as long as the modules have JSDoc documentation.
Even after multiple function applications, we have static type inference to guide us at every step.
Since it uses JSDoc, we can specify JSDoc params where necessary to add type annotations to arguments.
Return types can still be inferred even when using JSDoc.
Type inference is especially useful when invoking more complex behaviors. Here we create a pipeline of logic to:
- Start with a range of numbers from 0 to 100
- Convert each of the numbers to its string form and English word form
- Group the result by the length of the English word form of the number
At each step, we have the compiler giving us type information about the objects we are working with. This helps to reduce our cognitive loads as programmers, making it easier to identify mistakes in our logic.
When we evaluate the expression, our run-time object matches the compile-time inferred type perfectly.
I have a strong background Functional Programming, so I’m fond of type safety. The idea is that your compiler can reduce your cognitive load by holding your hand and making sure you don’t write things incorrectly.
Everything is very well documented and I would strongly encourage you to read up on the documentation.