In this article, I will show you my first experience with TypeScript. I will follow instructions from the TypeScript website tutorials to create a “Hello World” example. What TypeScript is and reasons to use it can be found here.
The first step is to install TypeScript using the command:
npm install -g typescript
After the command is completed you will see a message informing you that one module has been added. The command to compile files is:
Here is my “Hello World” example in the JS console. I start with the following .ts file:
const msg:string = "Hello World!"; console.log(msg);
and after compiling it I receive:
var msg = "Hello World!"; console.log(msg);
In this basic example, there are almost no differences between the original file and the one after compilation. The only one is that I declare the constant “msg” as a string and when the process is completed I have code without it. In this example, there is no big impact, but TypeScript will be useful when you work on a big project and have to declare many classes and have to be careful about types. TypeScript can help you make your code cleaner and less error-prone.
Leave A Comment