There's like a fight or something like that about JS vs TS and I always like to go against TS in TS posts and against JS in JS posts. This way I get solid sentences about pros for one or another.
This makes me look like a troll but most of you guys only put a good amount of attention when you read something that bothers you or that is against your believes π be honest and don't blame me much for this (please).
I'll try to recap things straightforward and give you a good option as middle point in a vague try to stop that war from now on.
You can laugh as much as I'm laughing at the time of writting that down π
The question would be:
Which are the main reasons to use TypeScript?
1. Type Inference
Type inference is the automatic deduction of the data types of specific expressions in a programming language.
It works when calling a function/method so the IDE knows how many params does it need, which type they should be and will complain if there's something wrong.
It also lets you hover your mouse over the function name and you'll get inferred information about this function/method like the params and it's types, what it returns and so.
2. Real-time type checking
When you have an object in JavaScript, you can access every property of that object either the object having this property or not.
This can lead to errors that are difficult to debug. In Typescript, your IDE tells you in real-time that you are trying to access something that doesnβt exist or that lacks this property or if you try to use some non-compatible method/function on it.
TypeScript always points out the compilation errors at the time of development (pre-compilation). Because of this getting runtime errors is less likely, whereas JavaScript is an interpreted language.
3. Interfaces
You are able to set up interfaces in TS by default whereas in JS you need some external lib like implement-js or validate a schema using validation libraries like Joi.
Ok so we all understand and agree that the points 1 and 2 are a MUST.
Can we all -please- agree as well on the sentence "to reach this in JavaScript (nicely) we need TypeScript"?
Ok, let's move on.
How can we reach that?
We have 2 ways to use TS in our projects.
1- One is to add TypeScript as project dependency, perform the setup and start coding. You all know about that or you can find tones of information about this online so I'm not extending on that.
2- The second one is to use TS Pragma in your JS files along with JSDoc and enforce the overall with a linter like ESLint.
VSCode will handle that out of the box in real time. Zero config needed.
// @ts-check
/**
* Calculates the total weight of the inventory
* @param {string} inventory
* @returns {number}
*/
function inventoryScore(inventory) {
let totalInventory = 0;
inventory.split(',').forEach((item) => {
totalInventory += getItemWeight(item.trim());
});
return totalInventory;
}
/**
* Retrieves the weight of a given item
* @param {string} item
* @returns {number} weight of the item
*/
function getItemWeight(item) {
if (item === 'gold cup') return 5;
else if (item === 'puppy') return 4;
else if (item === 'magic cup') return 10;
else if (item === 'tooth of a magestic whale') return 20;
else if (item === 'tentacle of a giant squid') return 100;
return 0;
}
Copied this code from my own comment on this post as example.
Which benefits do we get using TS Pragma and JavaScript?
Of course the same happens with variables:
We can also define variable and constant types with inline JSDoc like that:
- We also avoid the extra-build time that adding TS as dependency causes.
For those thinking on "yeah but people will refactor things and forget about documentation", well, TS Pragma will use TS behind the scenes to ensure it's OK plus we have a linter, so we can use it. Check the ESLint plugin JSDoc.
As extra benefit, this approach can serve as introduction to TS, getting yourself used to declare types and forcing you to document your code (which is always good).
If you want at any point and for any reason to migrate your project from JS to TS it will be much easier.
Cons:
- This workaround does not let you use all features that TS brings. i.e. you can't use generics afaik.
Do you like my content? βBuy me a coffee
Still here? Pin this JSDoc Reference
Will you take a try to this approach? Let me know in the comment section
Best regards