Typescript is a front-end language that is quickly gaining popularity in the developer community. Several JavaScript’s drawbacks are resolved by Typescript while maintaining all of its benefits.
In this blog , we will discuss about the following subjects :
- What is typescript?
- Why do we use typescript?
- How to use typescript?
- Advantages of typescript ?
- Difference between typescript and Javascript
- Setup the Typescript development environment
- First Hello World with Typescript
What is typescript?
- Typescript is a superset of JavaScript.
- TypeScript is built over JavaScript. You start by creating the TypeScript code. After that, a TypeScript compiler converts the TypeScript code into regular JavaScript code.
- Any environment that supports JavaScript will accept the fundamental JS code for deployment. Files in TypeScript have the Ts extension, while those in JavaScript have the.js extension.
- A program written with TypeScript is one whose JavaScript code is free of any syntax errors. Hence, all TypeScript programs are JavaScript programs. When converting a JavaScript codebase to TypeScript, this is quite helpful.
Why do we use typescript?
We’ll see why typescript is necessary in this section.
There are two main applications for typescript:
- JavaScript gains a type system from TypeScript, which can help you avoid a number of issues with dynamic types.
- Future JavaScript functionality, such as ES Next, is already built into TypeScript so you can use it right away..
Example:
In this example, we declare a string-type variable called “message” and set its initial value to “Hello World.” We specifically indicate the type string since TypeScript is statically typed, in contrast to JavaScript.
let message: string = “Hello World”
console.log(message)
How to use typescript?
The typescript compiler is used to compile typescript code into JavaScript after it has been written in a file and saved with the.ts extension. Every Operating System, including Windows, Mac OS, and Linux, can be configured to use the Typescript compiler.
Any code editor, such as Visual Studio and Visual Studio Code or Notepad++, can be used to write it. the tsc filename> command. Your Typescript code is converted by Ts into a simple JavaScript file.
Advantages of typescript ?
- TypeScript is purely object-oriented programming.
- Both client-side and server-side development can be done with Typescript.
- The ability to compile down to a version of JavaScript that runs on all browsers.
- IntelliSense and code completion are two of TypeScript’s most important features.
- Intellisense provides active hints as the code is being typed.
- ECMAScript 6 Feature Support Static Type-checking.
- TypeScript compiler will do static type-checking to verify the type (to surface more typing errors at compiling time).
JavaScript doesn’t support this.
Difference between typescript and JavaScript
- Programming in TypeScript is entirely object-oriented.
- Both client-side and server-side development can be done with Typescript.
- JavaScript is a scripting language, whereas TypeScript is an object-oriented programming language.
- Static type-checking is a feature of TypeScript that JavaScript does not have.
- Module support is available in TypeScript but not in JavaScript.
- Interface is present in TypeScript but is absent in JavaScript.
- JavaScript does not support the optional parameter function, whereas TypeScript supports.
Setup the Typescript development environment
To set up the typescript development environment, we need the below prerequisites:
1.Install node js
To install the node js, click on the link, based on your operating system, and choose the Long term support option.
2. Install Typescript compiler
Launch the Terminal on Mac, Linux, or Windows, and then run the following command to install the TypeScript compiler:
npm install -g typescript
Run the following command from the Command Prompt on Windows or the Terminal on macOS or Linux to install the ts-node module globally:
npm install -g ts-node
3. Install Code Editor
So I will use the vs code editor.
First Hello World Program with Typescript
- Create a new folder, for example, TypescriptExample, and then write the first hello world programme in it.
- Open vs Code or another code editor next.
- Next make a new file with the name app.ts file inside the folder; typescript uses the.ts extension.
- After that, enter the following code in the app.ts file:
let Message: string = “Hello World”;
console.log(Message);
To compile the app.ts file, open a new terminal in vs code and type the following command:
tsc app.ts:
An app.js file will be created by the typescript compiler if the aforementioned command executes successfully.
Then, use the following command to launch the app.js file under node.js:
using Node.js
The result is displayed as Hello World!
Conclusion
In this Blog, we learned the fundamentals, including what typescript is, why we need it, and how it is superior to javascript, as demonstrated by a discussion of a problem. Also, we set up the typescript development environment and write our first typescript hello world program.
For any Help or Queries Contact us on info@crmonce.com or +918096556344