Learning TypeScript–Part 1

Nov 11th, 2012 | By | Category: Programming & Languages

Over the past few years, we have seen how the importance of client side scripting has grown in modern web applications.For these browser based applications Javascript has been the only language available. Javascript is a fine, lightweight dynamic language with full support for several functional programming constructs.But with growing volume of code there is need of a language which is more structured, object oriented and can be supported by tools for intellisense,refactoring etc. like any modern programming language of the day.Last year Google unveiled a new language called DART in an attempt create a structured programming language for web which will replace Javascript in future. In a similar attempt Microsoft this year, has come up with it’s open source initiative, TypeScript.

TypeScript is more of a syntactic sugar on top of Javascript and TypeScript compiler translates the code into Javascript and this Javascript code is embedded in the web applications.

The TypeScript compiler can be installed as a  Node.js plugin and also there is a VS2012 plugin which support TypeScript development.

To start with we will focus on the declarations and various data types supported by the language.

The first in the list of types will be the Any type, this basically tells the compiler to avoid any static type checking for the variable, just as normal javascript.


This allows us to assign the variable to both string and numeric values.If we are not specifying any type explicitly then the variable by default will behave as Any as shown below:


The language supports the following primitive types

  • Number
  • Boolean
  • String
  • Null
  • Undefined

If we are declaring a variable with type say number or string then the TypeScript compiler’s static type checking will not allow us to assign the variable with values of other types.


Typescript also infers the type based on the value it is initialized with ( quite like the var keyword in C#). In snippet below, the type of “i” is inferred as number and hence can’t be assigned with a string value.


The Null data type is a valid value for any other data type as shown below:


The undefined data type on the other hand is for uninitialized variables. In the snippet below variable “i” is declared but not initialized.


The next set of types are the object types but before proceeding into that we need to discuss about classes, interfaces & functions. We will start with functions from the next post.

Kick It on DotNetKicks.com
  • http://codingndesign.com/blog/?p=376 Null Vs. Undefined in Javascript – Coding N Design

    [...] my last post on TypeScript I had mentioned about the null & the undefined data types very briefly.Both [...]

  • http://www.facebook.com/anwesha.chakraborty.50 Anwesha Chakraborty

    Two small questions,
    1. var i=null;
    will the above lines be compiled together? If not then what is the type of the variable ‘i’ here?

  • http://www.facebook.com/anwesha.chakraborty.50 Anwesha Chakraborty

    2nd question is very trivial one. Is the following statement valid?
    var i:number=10;
    // you have not mentioned it anywhere, but if I refer to the null initialization, I think this will not throw an error.