First of all, I wanna talk about my own motivations behind learning Typescript. Having been coming from statically-typed language background, when I first introduced to dynamic language (Javascript ES6) in a non-toy project, it felt positive in many ways. It was particularly powerful for prototyping a program where the structures weren't well determined yet. In my case, it was due to business requirement that was still ambiguous, so things were inevitably changing. It was also convenient because dynamically-typed language is often also a scripting language. Being able to save the code and immediately run it without having to wait for compilation time is just so convenient.
But as soon as my code-base grew large, Javascript became hard to maintain. Scaling up my medium-size code base became very hard because:
1. Special discipline needed to be ensured in order to make the code readable and maintainable (i.e. by manually documenting input and output of each functions)
2. Refactoring became very hard as there's no easy way to 100% ensure where a function or a class is being referenced from, which implied hardship when renaming a function, class, or file, and also while changing a function's parameter or return type.
3. The absence of type-checker also caused many mistakes that would have been easily caught during the compile phase of a statically-typed language to be hidden until run-time, hence requiring super rigorous testing. (don't forget how Javascript is sometimes crazy, i.e. == vs. ===)
4. Overseeing and reviewing beginner developers who didn't yet have the discipline was also very hard, particularly because the language did not "force" them to think about data types and structures before implementing something, which often times resulted in them writing spaghetti code.
Typescript is wonderful because it combines the best of both static and dynamic typing. It is a superset of ES6, which means all ES6 code will work as-is without requiring any changes at all. This allows one to quickly prototype a code using regular dynamically-typed Javascript, and then slowly adding type definitions into it as the requirement steadily becoming more mature. Typescript code also interacts fine with Javascript code. So yeah, you're not gonna run out of libraries to use as you can use everything you find on NPM. It still does require a lot of discipline in order to make the type system useful though, because one can simply turn a ".js" file to ".ts" without actually leveraging the power of the type system, duh! But I guess an effectiveness of a tool always depends on the hands of its users anyway. But yeah, it's a real solution to keep a large JS code-base more maintainable while still being versatile when needed.
Visual Studio Code is another reason why one would wanna use Typescript. It supports all the fancy stuffs that static language IDE supports such as auto-refactor, auto-complete, jumps to definitions, symbols, etc. If you ever used and came to appreciate typical Microsoft IDEs, this feels similar. And not only that, this time Microsoft chooses to publish its source code on GitHub! So yeah, we no longer have the worries that typically come with using closed-source programs: fell in love then got heart-broken because things didn't go as hoped.
Another thing to love about Typescript is how powerful its typing system is. Even if you only read its documentation, you'll probably guess that Microsoft had a bunch of world-class programming language experts and PhD holders working on this super-smart typing system. One thing I enjoy is its structural typing system where a type is compatible with another as long as it has the property expected out of it. So this is kinda like duck-typing, but typed, meaning that the transpiler is going to yell if you try to pass in incompatible types. It also has supports for type union, intersection, etc, just like what one would expect out of a typical modern programming languages.
In the next blog post, I'm gonna write about how I refactored my NodeJs ES6 code-base into Typescript.
No comments:
Post a Comment