Here’s a real quick introduction to the TypeScript programming language.
In this article, I’ll explain what TypeScript is, in just a few paragraphs. Note that this is an excerpt from the introduction chapter of my book.
What is TypeScript?
The TypeScript programming language has been created by Microsoft and later open sourced under the Apache 2.0 License. The source code of the language is available on GitHub over at https://github.com/Microsoft/TypeScript
A safe investment
At the time of writing, TypeScript now has ~62K stars on Github. This is 25K more than the last time I checked, around the end of 2019. TypeScript keeps gaining traction and its popularity continues to rise as shown here: https://redmonk.com/sogrady/2018/08/10/language-rankings-6-18/
Whether you use React, Angular, Vue.js, NestJS or other things, TypeScript can be used and will help you write higher quality and more maintainable code.
As stated on the official Website of the language:
This simple definition actually says a lot about TypeScript. Let’s analyze it piece by piece.
- in the browser
- in the back-end (e.g., with node.js)
- on the desktop (e.g., with Electron)
- on mobile with frameworks like React Native, NativeScript, Ionic and many others
- in the cloud with platforms such as Azure Functions, Google Cloud Functions, Firebase, etc
A third takeaway is that TypeScript is (optionally) typed.
Although the fact that defining types is optional by default in TypeScript does not mean that you should avoid defining types. Instead, TypeScript shines when you make clever use of its type system.
TypeScript allows you to clearly specify the type of your variables and functions. In addition, it also has very powerful type inference, support for interfaces/custom types, classes, generics, enums, mixins, and many other cool things that I’ve covered in my TS book.
The best use of the language is made when the strict mode is enabled, which enforces stricter rules on what needs to be typed and when. But in any case, it’s often possible to let the compiler infer types for us.
Supports static and structural typing
One of the coolest things about TypeScript is its support for structural typing.
In TypeScript, if something quaks like a duck, then TypeScript considers that it’s a duck. Duck typing as it is called is baked into the language..
TypeScript considers the compatibility of types.
For instance, if some function expects to receive an object of a
Foo type and it is instead called with an object of the
Bar type, then TypeScript will not complain as long as
Bar has the same structure (i.e., is structually the same) as
Foo. That is to say, a
Bar object can be considered to be a
Foo if it exposes (at least) the expected properties/methods, with the expected types. This is a simplification, but it’s not far away from the truth.
Structural typing is super useful as it helps to avoid writing quite some boilerplate code. In programming languages that don’t support duck typing, you often have to waste time converting objects to different data structures. In TypeScript, if the form is compatible, then you’re good to go.
Note that TypeScript also supports some nominal typing (i.e., non-structural), which is interesting if you want to distinguish between objects with different types, even if their structure is the same. Basarat has an excellent section about this in his book.
There’s not one single way to use TypeScript. You can mix and match approaches.
Of course, the best is to use TypeScript all the way and, right from the start.
But you can also add it into an existing JS codebase without having to convert everything at once. You can also make use of it even without converting your code to TypeScript at all; just benefiting from the compiler’s help.
In fact, you may already be using TypeScript without knowing; for instance if you’re using Visual Studio Code (which is built in TypeScript); since it uses TypeScript to help you out (auto completion, etc).
Types for JS libraries
This means that you can benefit from strong(er) typing while using many libraries.
It’s not always perfect, but it does a great job! Having types for existing libraries is great, as it helps to more easily understand the APIs, make less mistakes and, most importantly, avoid many bugs.
In this short introductory article, I’ve explained what TypeScript is. Hopefully, it should give you an idea about why this language is interesting to learn.
I’ve dived into TypeScript around 2015–2016 and really ain’t looking back. I’ve used it to develop a front-end framework for the National Bank of Belgium and to write back-end as well as front-end applications, with a lot of success. I’m now also using it as the backbone of my upcoming SaaS startup and couldn’t be happier.
I’ve mentioned it already in the article, but I also wrote an 800+ pages book about this language (this tells how much I like it :p), covering from the very basic to the most advanced concepts as well as its usage in combination with multiple modern frameworks/libraries like React, Angular, Vue, NestJS, etc.
If you’re interested, then I’ll continue posting introductory material about TypeScript. There’s a ton more to know!
That's it for today! ✨
Hello everyone! I'm Sébastien Dubois. I'm an author, founder, and CTO. I write books and articles about software development & IT, personal knowledge management, personal organization, and productivity. I also craft lovely digital products 🚀
If you've enjoyed this article and want to read more like this, then become a subscriber, check out my Obsidian Starter Kit, the PKM Library and my collection of books about software development 🔥.
You can follow me on Twitter 🐦
If you want to discuss, then don't hesitate to join the Personal Knowledge Management community or the Software Crafters community.