As we all know JavaScript is used as the scripting language for the web, but in a couple of years, TypeScript is gaining popularity. Also whatever we write in TypeScript will comply with plain JavaScript. So people like me still think, if it's converting to javascript why should we need TypeScript?
TypeScript came into the picture in the case of large-scale modern web applications developed using React, Angular, etc.
Typescript definition is "TypeScript is the superset of JavaScript", which means TypeScript got all the features of JavaScript plus some additional features. This means the developer can use these additional features as well.
Typing
The first feature is TypeScript provides static typing which avoids some of the JavaScript runtime errors and in TypeScript, this will get captured during compilation.
- JavaScript variable declaration => var a=10; or var str="test";
- TypeScript varable declaration => a:number=10; or str:string ="test";
The above dynamic typing in JavaScript can cause large amount of runtime errors and this is one area where TypeScript came into picture.
Is TypeScript a replacement for JavaScript
Yes, even if you don't know typescript, you can write javascript in your typescript file (.ts). A valid JavaScript file can be converted to TypeScript by just converting its extension to .ts. Like JavaScript, TypeScript can also be used in the front-end or back-end. If you know C# or any object-oriented language, typescript will be easier. But still, you can use
JavaScript for small-scale applications as JavaScript is not deprecated. JavaScript getting a lot of features, but TypeScript got all these features plus some other features.
TypeScript to JavaScript - Transpiling
TypeScript cannot be used directly in the browser. Hence in your code, you write everything in TypeScript and the transpiler compiles to plain JavaScript and this will get executed in the browser.
Install npm package "typescript" using the command "npm install -g typescript"
Create ts with your TypeScript code and run the command "tsc filename.ts". This will create the javascript file in the same folder where the ts file is saved.
Modular programming
TypeScript supports modular programming and JavaScript does not support it. Also, TypeScript supports interfaces, and it's not supported in JavaScript.