What is TypeScript: Hello Guys! We all know the role of JavaScript in current coding market. Almost in every project we are using JavaScript and with increasing demand of JavaScript, many frameworks and libraries are released and doing so well i.e. React and Angular.
If you got a chance to work with Angular you must be aware that Angular uses TypeScript to write code and now React has also added the support for Typescript. If you want to boost your development skills then TypeScript is worth learning as it is going to be used at various places (client-server both sides)
Typescript is not replacing JavaScript and even to learn TypeScript you should know JavaScript. Its a superset of JavaScript with additional features. Let us know more about Typescript.
Static Typing Vs Dynamic Typing (TypeScript Vs JavaScript)
Before learning TypeScript, you need to aware about what exactly I mean by Dynamic typing and Static Typing. Every variable must associate with a type whether it is boolean, string, number…… right ?
So Dynamic Typing means this type will decide by language itself at runtime > JavaScript
Static Typing means you as a developer will tell while coding that particular variable will of type string, number or boolean etc >TypeScript
// JavaScript
let name = "Deepak"; // JavaScript will decide its type as string at runtime.
let age =20; // // JavaScript will decide its type as number at runtime.
// Typescript
let userName:string= "Deepak"; // it clearly mentioned it is of string type
let age:number =20; // it will be of type number
What is TypeScript?
TypeScript is an open-source programming language developed and maintained by Microsoft. As you can guess by the name, TypeScript adds a static typing feature in JavaScript. It is considered a superset of JavaScript because its adds new features to JavaScript. As a superset, all JavaScript programs are also valid TypeScript programs. You can use TypeScript to develop front-end and back-end applications. It fits well eith Voe.js, React.js, Node.js etc.
How browser understand TypeScript?
Browsers don’t understand TypeScript so Typescript is compiled to JavaScript only which browser can understand. TypeScript files uses the .ts extension rather than .js extension of JavaScript files. TypeScript provides a command-line utility tsc that compiles (transpiles) TypeScript files (.ts) into JavaScript (.js). There are other tools which convert TypeScript files to JavaScript files like Babel. You might use these tools for speed or consistency with your existing build tooling.
Advantages of TypeScript
Compile Time Error: Errors can be handled at compile time only: Unlike JavaScript TypeScript check for the type at compile time only and catch the error. It can save you a lot of time and help you to prevent potential crashes or bugs.
Readable Code: Thanks to types, you will understand your code more efficiently by improving its quality. All will be clear, and you will know what’s inside a variable without console.log everything.
Refactoring becomes more straightforward. At the same time, by improving the readability of your code, you will be able to modify it more easily. Where you’re using dynamic types, sometimes it can take a lot of time to understand in depth the code you’re changing.
Also Read: