TypeScript, as its name suggests, is a typed superset of JavaScript, which means that all JavaScript code is valid in TypeScript – making it very easy to get started. In addition, TypeScript has several language features including static typing, most ES6 syntax, and object-oriented programming constructs. It is a compiled language, which means you’ll have to transpile it to JavaScript before it can be run.
TypeScript's main mission is simple: make JavaScript, which is a dynamically typed language, behave more like a statically typed language.
But whats the difference between dynamic and static typing?
Basics
Every feature provided in TypeScript is non-invading, meaning that it has syntax that doesn't overlap with any part of the JS code. This makes porting your JS app back and forth relatively easily.
You can specify your variable's type using the colon (:) followed by the actual name of the type:
const stringVar: string = "str";
There are 6 basic, primitive types to remember:
number // represents any kind of numeric value
string // represents any kind of string value;
boolean // represents any boolean value, i.e. true or false;
symbol // represents symbol values;
null // represents null value only;
undefined //represents undefined value only;
What does TypeScript offer ?
- Static Type Checking (Optional) – Like other high level programming languages like Java, C etc. TypeScript does provide static type checking unlike JavaScript. Although static typing requires some extra steps while writing code but it has its own advantages. With TypeScript, we can check and assign variables, parameters and function types. It is completely Optional and helps us find and prevent bugs. Also helps make code more readable and descriptive.
- Class Based Objects – Another huge advantage is the use of Classes which provides the ability to use true object oriented programming in our applications and prevents use of prototype based objects. It also provides encapsulation, inheritance and modifiers.
- Modularity – It helps make the code more modular.
- ES6 Features – Support for ES6 features is also one of the main reasons for its popularity.
- Syntax – TypeScript provides syntax which is closer to java and other high level languages (Syntactical Sugaring).
Top comments (0)