Typescript – walk-through – Part 1

In this series, we will examine best parts of typescript language.
For full reference please visit: http://www.typescriptlang.org/

If you would like to practice typescript under node.js environment you can go to https://c9.io and create a sample node.js project. It is very easy to create projects and practice them on this platform. Highly recommended!

 

interface and constructor

referencing other libraries (such as jquery, knockout) in typescript

primitive types

function parameters, optional parameter

\

function overriding on TypeScript

typescript compiler command over node.js

As in JavaScript, all numbers in TypeScript are floating point values.

list and enum types

another example of interface usage

Optional Properties, option bags

Once defined, we can use this function type interface like we would other interfaces. Here, we show how you can create a variable of a function type and assign it a function value of the same type.

Array types have an ‘index’ type that describes the types allowed to index the object, along with the corresponding return type for accessing the index.

Implementing an interface

Difference between static/instance side of class

This is because when a class implements an interface, only the instance side of the class is checked. Since the constructor sits in the static side, it is not included in this check.

Extending Interfaces

Like classes, interfaces can extend each other. This handles the task of copying the members of one interface into another, allowing you more freedom in how you separate your interfaces into reusable components.

or multiple extension

Hybrid Types

As we mentioned earlier, interfaces can describe the rich types present in real world JavaScript. Because of JavaScript’s dynamic and flexible nature, you may occasionally encounter an object that works as a combination of some of the types described above.

Classes

Inheritance

Languages like C# require that each member be explicitly labelled ‘public‘ to be visible. In TypeScript, each member is public by default.

For two types to be considered compatible, if one of them has a private member, then the other must have a private member that originated in the same declaration.

When we try to assign from an ‘Employee’ to ‘Animal’ we get an error that these types are not compatible. Even though ‘Employee’ also has a private member called ‘name’, it is not the same one as the one created in ‘Animal’.

Accessors

TypeScript supports getters/setters as a way of intercepting accesses to a member of an object. This gives you a way of having finer-grained control o

ver how a member is accessed on each object.

Static Properties

.ts version

.js version

usage of class instances:

Here, when we say ‘var greeter: Greeter’, we’re using Greeter as the type of instances of the class Greeter. This is almost second nature to programmers from other object-oriented languages.

Using a class as an interface

Modules

A sample page imports modules into page.

/// <reference path=”Validation.ts” />
/// <reference path=”LettersOnlyValidator.ts” />
/// <reference path=”ZipCodeValidator.ts” />

module called “Validation” exports an interface “StringValidator”

LettersOnlyValidator.ts file

ZipCodeValidator.ts

If multiple JS files get produced, we’ll need to use <script> tags on our webpage to load each emitted file in the appropriate order, for example:

Below, we have converted the previous example to use external modules. Notice that we no longer use the module keyword – the files themselves constitute a module and are identified by their file names.

The reference tags have been replaced with import statements that specify the dependencies between modules. The import statement has two parts: the name that the module will be known by in this file, and the require keyword that specifies the path to the required module:

We specify which objects are visible outside the module by using the export keyword on a top-level declaration, similarly to how export defined the public surface area of an internal module.

To compile, we must specify a module target on the command line. For node.js, use –module commonjs; for require.js, use –module amd.

Depending on the module target specified during compilation, the compiler will generate appropriate code for either node.js (commonjs) or require.js (AMD) module-loading systems.

nodejs-what-are-commonjs-modules

This simple example shows how the names used during importing and exporting get translated into the module loading code.

export =