Learning TypeScript–Part 1Nov 11th, 2012 | By sankarsan | Category: Programming & Languages
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.
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
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.