TypeScript
TypeScript is a programming language.It is superset of JavaScript.This means that any valid JavaScript program is a valid TypeScript program.It is open source programming language developed by Microsoft.It is the prefered language for developing Angular 2 applications.
TypeScript adds some useful features which are missing in JavaScript.Some of these features are:
- It adds type checking at compile time
- It adds object oriented features to JavaScript
- JavaScript is not suitable for developing large applications because of the complexity of large applications.TypeScript allows the developers to develop large applications by using object oriented principles.
Please refer TypeScript Overview for more
Installing TypeScript
It is available as a package and can be installed as:
npm install -g typescript
Before installing TypeScript, using the above command ,ensure if Node.js is installed.
Extension of TypeScript files
By convention TypeScript files have an extension of .ts.
Compiling TypeScript Code
Converting TypeScript code to JavaScript is called transpiling.TypeScript transpiler is called tsc.To transpile a file using the TypeScript compiler execute the following command:
tsc hello.ts
TypeScript transpiler is integrated with Visual Studio.
tsconfig.json
This is a TypeScript configuration file. tsconfig.json defines the compiler settings and the TyepScript files to be included in a TypeScript application.
Which Editor can be used for developing TypeScript applications
TypeScript applications can be developed in Visual Studio as well as Visual Studio Code.Visual Studio Code is a free code editor from Microsoft.
Object Oriented features of TypeScript
TypeScript is a Object Oriented Language.It supports the Object Oriented Concepts such as:
- Classes
- Modules
- Interfaces
- Types
TypeScript includes primitive types such as Number,Boolean,String and Enum and allows creation of custom types such as classes and interfaces.
Variable Declaration in TypeScript
variables are declared in TypeScript as:
var identifier: type= value;
for example to declare a variable of type number called x we write:
var x:number=1;
Throwing exceptions in TypeScript
To throw an error in our TypeScript program we use the throw keyword as:
throw new Error('error occured');
In the above throw statement we can pass any string value.
Catching exceptions in TypeScript
try { throw new Error("error"); } catch (errorObj) { //handle error }
Arrays in TypeScript
Please see Arrays in TypeScript
Annotations
Annotations describe variable and function parameters.
For example to declare a variable we use the annotation as:
message:string;
and to declare a method parameter we use the type annotation as:
function Hello(message: string) { return "Hello, " + person; }
Leave a Reply