Why I love TypeScript 💕
Believe it or not but typescript has saved me a lot of time by catching errors and providing fixes before actually running the code.
Let's see how
Did you see that little red underline? That’s what TypeScript giving us a hint that something has gone wrong while writing code. You may have already figured out what's wrong here. Number has been given to user method instead of a string.
That’s the reason why I love TypeScript ❤.
Moving on, there's lot more to typescript than just type checking. Most important features I use every day is Decorators and interfaces
Decorators are special kind of declaration or method that can be attached to a class, method, property, or any parameter. Decorators use the @decorator_name, where decorator_name must evaluate to a method that will be called at runtime with information about the decorated declaration.
Typescript works great with VS Code. Anyways official Typescript docs is a great way to learn language in detail.
How To Get Started With Typescript
It is available as a package on the npm registry available as "typescript".
Node.js environment is must to run the package. Then dependency manager like npm, yarn can be used to download TypeScript into your project.
npm install -g typescript
You can then run the TypeScript compiler using one of the following commands:
Now we initialize a new typescript project using
You should end up with the TypeScript config.
Now all you need is to create a
.ts file and start writing code in typescript. Once you are done you can compile
.ts file into
.js using following command
Great 🎉 Now you can start building something awesome with Typescript.
If you have already been using it and wants to start using typescript in your NodeJS application I have a Nodejs / Typescript starter project built using Nx workspace and express server includes MongoDB and jest testing framework. If you like the project leave a star🌟
That's why I love TypeScript. Basically, Typescript allows you to write better, cleaner, productive and more maintainable code. Obviously, you don’t have to go all out for TypeScript directly and start using decorators, interfaces or classes. Initially I have used TypeScript only with type checking on small projects. and gradually you can start adding more and more features.
I hope this post helps you decide if you should give it a try, and I strongly encourage you to start using it.
Thanks for reading, stay awesome! ❤