I had been fascinated by Typescript and have been going back and forth on making it my primary programming language. In a few posts and with as much less information as possible, let’s see why anyone should bother with Typescript.
What is Typescript?
Typescript is a typed Javascript.
No, I am kidding - it is more than that. Typescript is not only strongly typed language that acts as a super-set of Javascript, but also is compiled and object oriented language. What distinguishes Typescript is not the language alone but the tooling that surrounds the language.
Typescript was invented and popularised by the good folks at Microsoft. It is in active development and continues to add amazing features with each release.
The Typescript ecosystem makes you welcome as a Javascript developer, does not impose itself but gives you really powerful tools if you want dive right in. Your development is going to be more standardized, faster, and your code is going to be friendly with everyone in the team.
I see a great value for Typescript in a team setting or for components and libraries that are used by everyone and their dogs.
Why another language?
You should use Typescript because it is and is not another language at the same time.
Strong typing may seem daunting at first but the amount of errors it catches by itself and the ease of development(?) more than makes up for the initial hiccups and the extra typing.
There are of course a host of features that make Typescript invaluable and my objective here is not to cover them all. Key features include -
- Compiled language - catch more errors at compile and not at runtime
- Good support for object oriented features (at least during development / compilation)
- Runs seamlessly across environments - web, operating systems and devices
- Typing everything can be the most beautiful form of self-documenting everything
- Easier to code!
But, I am a Javascript fanatic
Typescript is not a new language housed in an island and its own ecosystem. It coexists with Javascript.
- Typescript is Javascript++. Typescript gets compiled to Javascript for execution when required
- You can use both Javascript and other Typescript libraries within Typescript. You can use Typescript libraries within Javascript too
- Migrations can be incremental. You can use inferred typing and get started on Typescript with existing code
Get started
Getting started with Typescript is easy. Let us setup the development environment first.
|
|
Then, create a new file “hello.ts”.
|
|
If you now do -
|
|
.. you will get a hello.js
file. This is a JS compiled version from your Typescript file.
Since we did not quite use any Typescript specific stuff, our JS file is same as TS file.
|
|
You can run the JS file to the same end result as a normal JS file.
Alternatively, you can create the TS file and simply execute two commands in one go -
|
|
Or, use a better utility built to the purpose.
Install ts-node
|
|
Now, you can use nodemon
to watch for changes in your Typescript file, generate Javascript when required and execute the Javascript - in one seamless way.
|
|
We do not see the intermittent file “hello.js” using this method but it is recommended for development purposes only.
Write a better typescript example
Create a new file sum.ts
.
|
|
You may observe that when you type getSum(
, the autocomplete will suggest the type or arguments and the return. This auto-suggestion does not seem all that important, but is super useful when you are working in teams or have to work with third party libraries.
Next, try to do -
|
|
You see a couple of significant things -
- Editors like VSCode catch the error - no compilation or execution required. The ‘a’ argument is underlined with red squiggly in the hope that you correct the error
- Even if you ignore the editor error, Typescript does not compile the file but throws the error outlined above