TypeScript is a superset of JavaScript language. It has a single open source compiler and developed by Microsoft. The goal of typescript is to make javascript development more efficient. TypeScript is an object-oriented programming language that supports classes and interfaces. TypeScript is naturally fit for frontend applications. To learn TypeScript, you should know about JavaScript.
Why should we use TypeScript -:
- TypeScript simplifies javascript code, making it easier to read and debug.
- TypeScripts is open source.
- TypeScript can help us to avoid painful bugs that developers commonly run into when writing JavaScript by type checking the code.
- TypeScript code can be compiled as per ES5 and ES6 standards to support the latest browser.
- It supports static typing.
- TypeScript will save developer's time.
Difference between TypeScript and JavaScript -:
- TypeScript is known as an object oriented programming language while JavaScript is a scripting language.
- TypeScript supports modules while JavaScript does not support modules.
- TypeScript has interface while Javascript does not have interface.
- TypeScript catches error at compile time.
TypeScript Features -: Top TypeScript features you must know are as followings:
1. Object-Oriented Programming : TypeScript is a very good set of Object Oriented Programming (OOP) features, that are good to maintain robust and clean code. It improves the quality of code.
Example:
class UserModel
{
userId: number;
userName: string;
}
class UserOperation{
addUser(userData: UserModel) : number {
//add user
let userId =101;// Id returned after save
return userId;
}
}
2. Interfaces, Generics, Inheritance and Method Access Modifiers : Interfaces are a good way of specifying a contract. Generics help to provide compile-time checking, inheritance enables new objects to take on the properties of existing objects, and access modifiers control the accessibility of the members of a class. TypeScript has two access modifiers - public and private. By default, the members are public but you can explicitly add a public or private modifier to them.
3. Compile-time Type Checking : If we do not follow the proper syntax and semantics of any programming language, then compile-time errors are thrown by the compiler. They will not let your program to execute a single line until you remove all the syntax errors or until you debug the compile time errors. It is the same case with TypeScript as well.
4. Less code compare to JavaScript : TypeScript is a wrapper around JavaScript so helper classes are available that reduces the code. Code in Typescript is easier to understand.
5. ES6 feature support : Typescript is a superset of ES6, so all feature of ES6 are there plus some additional features like it supports Arrow Function commonly called lambda function. ES6 has introduced a slightly different syntax to define anonymous functions called the fat arrow syntax.
6. Potable : TypeScript is portable across browsers, devices, and operating systems. It can run on any environment that JavaScript runs on. Unlike its counterparts, TypeScript does not need a dedicated VM or a specific runtime environment to execute.
0 Comments