An introduction to the new kid in the Microsoft block known as TYPESCRIPT
This article is written to introduce the new programming scripting language developed by Microsoft known as TYPESCRIPT. This article will just give you basic understanding of the TYPESCRIPT language and supported features. Any beginner can read to get some basic knowledge about typescript.
Typescript
is a new programming language developed by Microsoft. Typescript is similar to JavaScript in other words a superset of JavaScript and is an open source language (source code is available from Codeplex under the Apache 2 License). On contrast to JavaScript, Typescript provides additional option of static typing (A programming language for which type checking is performed during compile-time instead of run-time) and class based OOPS programming, which is not possible in JavaScript. And hence we can think of Typescript as a OOPS version of JavaScript. Existing frameworks such as JQuery and Node.js are also supported.
Typescript syntax are kept similar to JavaScript so that any existing JavaScript programs will be supported and work with Typescript with minimal or no changes at all.
Following are the additional features or extension that Typescript adds to the JavaScript
• Type annotations and compile-time type checking
• Classes
• Interfaces
• Modules
• Arrow functions (Lambda functions)Type annotations
Type annotation or Type signature defines the inputs and outputs for a function, subroutine or method. As i mentioned earlier Typescript provides additional option of static typing by using type annotations to enable compile-time type checking. This feature is optional and can be ignored if you want to use the normal dynamic typing of JavaScript.
Sample code:
function Area(height: number, width: number): number
{
return height * height;
}Classes
ECMAScript 6 classes are supported by the TypeScript which integrate the optional type annotations support.
Sample code:
class Person {
private name: string;
private age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
toString(): string {
return this.name + " (" + this.age + ")";
}
}Declaration files
Declaration files can be think of something similar to the concept of header file in C/C++. When Typescript script gets compiled we can generate a declaration file out of it by using a option with extension of .d .ts. This declaration file than functions as an Interface to the components in the compiled JavaScript. In this process compiler just takes the signature of the types that are exported to generate the interface. This declaration file can then be used to describe the Typescript types of JS library or Modules within it to the third party developers to consume it.
Sample code:
module MATHS {
add(first: number, second: number): number;
subtract(first: number, second: number): number;
multiply(first: number, second: number): number;
divide(first: number, second: number): number;
}Web browsers support
Since the Typescript is compiled into standard JavaScript it supports any web browser or platform.Typescript IDE
Microsoft provides a plug-in for Visual Studio 2012 as well as basic text editor support for Sublime Text, EMACS and Vim.Compiler
TSC or TypeScript compiler is written in TypeScript which can be compiled into standard JavaScript which can then be executed in any JavaScript engine in any host. For ex: Browser.
Hi,
Thanks for sharing this useful information.
Best regards,
Prachi Kulkarni.