Why TypeScript?
- JavaScript can be used to develop both Client side as well as Server-side applications. Angular, React.js, Backbone.js can be used for Client-side application. Node.js can be used for Server-side application
- JavaScript is most popular, used programming language with one drawback or limitation that is no type system. Other languages like Java or C# are well structured and strongly typed programming languages.
- Why we need a Type system? The answer is Type system increase the code quality, readability and maintainability. Most important feature is errors can be occurred and rectified at compile time not at run time
- The main reason to use TypeScript is to Scale the JavaScript, as without scaling we can not build large and complex applications with the big team working on the same code. That’s why Type system / TypeScript is more important while working on JavaScript projects.
Basic Types
Boolean
The type which holds true or false value is called Boolean.
- let isChecked:Boolean = false;
Number
This type holds the number which can be floating point number, Decimal, Hexadecimal or Octal.
- let a1:number = 45;
- let a2:number = 0x31CF;
- let a3:number = 0o311;
- let a4:number = 110001;
-
- If we log these variables then we will get below values
-
- console.log(a1);
- console.log(a2);
- console.log(a3);
- console.log(a4);
String
String is primitive data type that is used to store text type of data, this data can be stored using single or double quotation mark.
- let man1:string = “Virat”
- let country1:string = “India”
- let man1details:string = man1 + “ plays for ” + country1
Template String
TypeScript version 1.4 included support for ES6 Template Strings. Template strings are used to embed expressions into the string.
- let man1details:string = ‘${man1} plays for ${country1}
Void
This type is similar to C# Void, it is used where there is no Datatype. It represents the absence of any type at all. The most common use is the function that does not return anything. It is almost opposite of Any data type.
This type is used to store any type of data. Many times, we don’t know the type of data coming from third-party API or user can enter anything, in such a situation we can use “any” data type.
- let variable1:any=4;
- variable1=”string data”;
- variable1 = true;
Array
The array is a special type which can be used to store multiple data values in sequential manner using a special syntax. There are two ways to declare array as below.
- let colors:string[] = [‘Black’, ‘White’,’Orange’];
- let colors:Array<string> = [‘Black’, ‘White’,’Orange’];
Enum
It is an addition to the standard set of data types from JavaScript is the enum. Almost same as C#, it is a way of giving more friendly names to sets of numeric values.
- Enum Color{ Red, Green, Blue }
Tuple
A tuple is a special type of array, we can say it heterogeneous array which can hold different type of data, however, it will be known type of data.
- let employees:[number, string] = [1, “Virat”];
Object
object is a type which represents the non-primitive type.
- let x1:object = {prop:0};
- let x2:object = null;
Never
The never type represents the type of variables that never occur. For instance, never is the return type for a function expression or an arrow function expression that always throws an exception or one that never returns.
- function abc():never{
- throw 20;
- while(true){
- }}
Variable Declarations
- JavaScript and TypeScript have the same rules of variable declarations. Variables can be declared using let, var and const.
- Variable which is declared within block inside function with var has function level scope rather than block level scope, because of typescript hosting in the JavaScript
- Let variable declaration allows to have block level scope to variables declared within the block inside function
Advantages using let vs var
- Block-scoped let variables cannot be read or written to before they are declared
- let variables cannot be re-declared
- Variables declared using ‘let’ minimize the possibilities or runtime errors, as the compiler give compile-time errors. This increases the code readability and maintainability
When the number of parameters that a function will receive is not known or can vary, we can use rest parameter. In JavaScript, this is achieved with the "arguments" variable. However, with TypeScript, we can use the rest parameter denoted by ellipsis'...'
- We can pass zero or more arguments to the rest parameter. The compiler will create an array of arguments with the rest parameter name provided by us.
- Rest parameters must come last in the function definition, otherwise the TypeScript compiler will show an error
- let Greet = (greeting: string, ...names: string[])=>{
- return greeting + " " + names.join(",") + "!";
- }
- Greet("Hello", "Virat","Kohli");
-
- Greet("Hello");
-
These are some basic concepts in TypeScript. the beginner can start with these concepts so that their concepts get clear.