TypeScript


What is TypeScript?

  • TypeScript is a programming language, which is developed based on JavaScript.
  • TypeScript is a superset of JavaScript, which adds data types, classes, interfaces and other features.
  • TypeScript = JavaScript + Data Types + Classes + Interfaces + Misc. Concepts (Arrow Functions + Multiline Strings + String Interpolation + Destructuring + Modules etc.)
  • TypeScript is built on the top of JavaScript. That means all the code of JavaScrpt works as-it-is in TypeScript, but in TypeScript, we can additionally use data types, classes, interfaces etc., concepts.
  • TypeScript is developed by Microsoft Corporation in 2012.

Features of TypeScript

  • Browser doesn’t support TypeScript directly. TypeScript code can’t be executed directly by the browser. So TypeScript code should be converted into JavaScript code, and we have to import JavaScript language file into the web page. Browser executes JavaScript. We use “TypeScript Compiler” (tsc) to compile / transpile “filename.ts (TypeScript file)” to “filename.js (JavaScript file)”. We won’t load TypeScript file into the browser; We will load & execute JavaScript file into the browser.

Advantages of TypeScript

  1. Static Typing and Type Safety o Static Typing: Whenever we can fix a data type for the variable while declaration of variable, and we can’t change its data type throughout the program, then it is said to be “static typing”. Whenever we can’t fix a data type for the variable while declaration, and the data type will be automatically taken by the runtime engine automatically at the time of program execution, and we can assign any type of value into the variable, then it is said to be “dynamic typing”. C, C++, Java, C#.NET are examples of “static typing”. “JavaScript”, “Python” are examples of “dynamic typing”. TypeScript supports “Optional Static Typing”. That means you can use both “dynamic typing” and “static typing” in TypeScript. o Type Safety: If we specify data type while declaring the variable and when we assign wrong type of value into the variable, the compiler shows errors
  2. Identification of Errors : Typing mistakes and syntax errors are displayed as errors in the compilation time itself (rather than at runtime).
  3. Classes and Interfaces : TypeScript supports classes and interfaces; so it providers rich programming experience much like other languages such as Java, C#.NET etc.
  4. Intellisense Automatic suggestions will be displayed while writing the code. For example, when we type “c” in the IDE, the list of matching names that start with “c” will be automatically displayed. This is available only in supporting code editors / IDE’s such as Visual Studio Code, WebStrom, Sublime Text etc.
    1

tsc first.ts
node first.js

Output: Hello