Hello all!
This is my first post to Dev.to and I am very excited for this to be the beginning of my TypeScript series where I will be documenting what I am learning through Codecademy and Free Code Camp. I will not just be copying and pasting the information, but learning, and bringing it to you in a concise manner. Please enjoy and don't be timid to send me a message of what you think!
LETTUCE BEGIN🥬
Installing Typescript
Let's get this sucker started on our local machine. We can either install it globally, so it can be used on any project we would like, or install it to a specific project.
Installing it globally, using npm, would look like this:
npm install -g typescript
To install it to a specific project, we would need to open the terminal, type cd
and then your project's local address. Once in the project, we would install in this way:
npm install --save-dev typescript
How TypeScript Works
TypeScript is a superset of Javascript, invented by Microsoft, that adds strong type checking. Hence, the 'type' in TypeScript, I guess. The TS code is taken into a compiler and then translated into JS code, meaning all of the features of JS are available to us, plus the added features of TS.
Some added features are:
1) Better development-time tooling (the amount of time from work started to work delivered)
2) Static code analysis (analysis of the code without launching code)
3) Compile-time type checking (making sure all the types are used as they should be).
If you are coming into this with some Javascript knowledge, already, then that is preferred because TS sometimes looks roughly the same as JS code! TS files are stored in .ts files, compared to JS files being stored in .js files, so don't get that mixed up, ya hear?
When To Use TypeScript
Large Codebase
When to use a specific language, library, or framework can definitely be tricky. The same goes for TS, as there are many times where it is best to use, but also times where it may not be most practical. From what I gather, TS is getting such a big following in the Javascript community because it cuts down on common errors. However, if your codebase is not going to be large to begin with, there really isn't much reason to convert your JS files to TS, as sometimes it will add about 30% to the total size of the code
Accustomed to Type Languages
That's pretty vague and self-explanatory, right? If you or your team wants to use it, then use it. Sure? I can't judge because I had learned React a little while back and now I try to work it into every project I can.
There are also some other type-strict languages out there that keep the same mindset as TS, such as the C# language. While they are based on much different languages, the same person that created TS also created C#. I guess you could say they have a TYPE 😏.
Library or Framework Wants TS
I personally have never ran into this sort of request from a library or framework, but it is definitely not too far out of the ordinary. For TS, Angular 2 seems to be really screaming for the necessity of it. So if you are reading through the documentation of a library or framework and it is begging you to use TS, be my guest.
Type Demanding Situations
It makes sense that there would be some situations where having a strict type is crucial for your app or code snippet. One that pops into my mind is anything having to do with forms. If a rascal of a user tries to input a number into a string situation, that can throw a major error or crash your app if not handled properly. TS acts as some sort of gatekeeper in the way that this is one of the first things that it checks for via the compile-time type-checking we had discussed earlier. If you're expecting to be using a good amount of forms, or a type heavy application, then TS might be the best option.
Conclusion
I can not thank you enough for making it to the end of my first insertion to the Learning TypeScript series! While this article was more based on what TS is, and when to use it, the following articles should jump into some sweet, sweet code that we all know and love. Thank you and look out for the next submission, don't forget to follow me here and on Twitter! Peace!
Top comments (4)
Thanks for the series Jack. Following you to keep up to date. Specifically interested in how to refactor an existing project implementing typescript. Looking forward to your next post!
Thank you for the support, Jon. Also that is a great suggestion for a part of the series, I'll be sure to touch on that important skill in a bit!
Great first article.
Keep the good work !
Thanks Eric, stay tuned my friend!