2014-08-14 14:12:50 +04:00
---
language: TypeScript
contributors:
2014-09-01 12:51:43 +04:00
- ["Philippe Vlérick", "https://github.com/pvlerick"]
2014-08-27 18:14:57 +04:00
filename: learntypescript.ts
2014-08-14 14:12:50 +04:00
---
TypeScript is a language that aims at easing development of large scale applications written in JavaScript.
TypeScript adds common concepts such as classes, modules, interfaces, generics and (optional) static typing to JavaScript.
2014-09-01 12:51:43 +04:00
It is a superset of JavaScript: all JavaScript code is valid TypeScript code so it can be added seamlessly to any project. The TypeScript compiler emits JavaScript.
2014-08-14 14:12:50 +04:00
2014-08-14 17:40:21 +04:00
This article will focus only on TypeScript extra syntax, as oposed to [JavaScript] (../javascript/).
2014-08-14 14:12:50 +04:00
2014-08-14 17:40:21 +04:00
To test TypeScript's compiler, head to the [Playground] (http://www.typescriptlang.org/Playground) where you will be able to type code, have auto completion and directly see the emitted JavaScript.
2014-08-14 15:34:41 +04:00
2014-09-08 15:08:28 +04:00
```js
2015-03-10 23:09:35 +03:00
// There are 3 basic types in TypeScript
2014-08-14 14:12:50 +04:00
var isDone: boolean = false;
var lines: number = 42;
var name: string = "Anders";
2015-03-10 23:09:35 +03:00
// When it's impossible to know, there is the "Any" type
2014-08-14 14:12:50 +04:00
var notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean
2015-03-10 23:09:35 +03:00
// For collections, there are typed arrays and generic arrays
2014-08-14 14:12:50 +04:00
var list: number[] = [1, 2, 3];
2015-03-10 23:09:35 +03:00
// Alternatively, using the generic array type
2014-08-14 14:12:50 +04:00
var list: Array< number > = [1, 2, 3];
2015-03-10 23:09:35 +03:00
// For enumerations:
2014-08-14 14:12:50 +04:00
enum Color {Red, Green, Blue};
var c: Color = Color.Green;
2015-03-10 23:09:35 +03:00
// Lastly, "void" is used in the special case of a function returning nothing
2014-08-14 14:12:50 +04:00
function bigHorribleAlert(): void {
alert("I'm a little annoying box!");
}
2015-03-10 23:09:35 +03:00
// Functions are first class citizens, support the lambda "fat arrow" syntax and
// use type inference
// The following are equivalent, the same signature will be infered by the
// compiler, and same JavaScript will be emitted
2015-03-11 00:58:18 +03:00
var f1 = function(i: number): number { return i * i; }
2015-03-10 23:09:35 +03:00
// Return type inferred
var f2 = function(i: number) { return i * i; }
2015-03-11 00:58:18 +03:00
var f3 = (i: number): number => { return i * i; }
2015-03-10 23:09:35 +03:00
// Return type inferred
var f4 = (i: number) => { return i * i; }
// Return type inferred, one-liner means no return keyword needed
2015-03-11 00:58:18 +03:00
var f5 = (i: number) => i * i;
2014-08-14 17:40:21 +04:00
2015-03-10 23:09:35 +03:00
// Interfaces are structural, anything that has the properties is compliant with
// the interface
2014-08-14 14:12:50 +04:00
interface Person {
name: string;
2015-03-10 23:09:35 +03:00
// Optional properties, marked with a "?"
2014-08-14 17:40:21 +04:00
age?: number;
2015-03-10 23:09:35 +03:00
// And of course functions
2014-08-27 17:41:12 +04:00
move(): void;
2014-08-14 17:40:21 +04:00
}
2015-03-10 23:09:35 +03:00
// Object that implements the "Person" interface
// Can be treated as a Person since it has the name and move properties
2015-03-11 00:58:18 +03:00
var p: Person = { name: "Bobby", move: () => {} };
2015-03-10 23:09:35 +03:00
// Objects that have the optional property:
2015-03-11 00:58:18 +03:00
var validPerson: Person = { name: "Bobby", age: 42, move: () => {} };
2015-03-10 23:09:35 +03:00
// Is not a person because age is not a number
2015-03-11 00:58:18 +03:00
var invalidPerson: Person = { name: "Bobby", age: true };
2014-08-14 14:12:50 +04:00
2015-03-10 23:09:35 +03:00
// Interfaces can also describe a function type
2014-08-14 14:12:50 +04:00
interface SearchFunc {
(source: string, subString: string): boolean;
}
2015-03-10 23:09:35 +03:00
// Only the parameters' types are important, names are not important.
2014-08-14 14:12:50 +04:00
var mySearch: SearchFunc;
mySearch = function(src: string, sub: string) {
2014-08-14 17:40:21 +04:00
return src.search(sub) != -1;
2014-08-14 14:12:50 +04:00
}
2015-03-10 23:09:35 +03:00
// Classes - members are public by default
2014-08-14 17:40:21 +04:00
class Point {
2015-03-10 23:09:35 +03:00
// Properties
x: number;
2015-03-11 00:58:18 +03:00
2015-03-10 23:09:35 +03:00
// Constructor - the public/private keywords in this context will generate
// the boiler plate code for the property and the initialization in the
// constructor.
// In this example, "y" will be defined just like "x" is, but with less code
// Default values are also supported
constructor(x: number, public y: number = 0) {
this.x = x;
}
2015-03-11 00:58:18 +03:00
2015-03-10 23:09:35 +03:00
// Functions
dist() { return Math.sqrt(this.x * this.x + this.y * this.y); }
2015-03-11 00:58:18 +03:00
2015-03-10 23:09:35 +03:00
// Static members
static origin = new Point(0, 0);
2014-08-14 17:40:21 +04:00
}
2014-08-14 14:12:50 +04:00
2014-08-14 17:56:30 +04:00
var p1 = new Point(10 ,20);
var p2 = new Point(25); //y will be 0
2015-03-10 23:09:35 +03:00
// Inheritance
2014-08-14 17:56:30 +04:00
class Point3D extends Point {
2015-03-10 23:09:35 +03:00
constructor(x: number, y: number, public z: number = 0) {
super(x, y); // Explicit call to the super class constructor is mandatory
}
2015-03-11 00:58:18 +03:00
2015-03-10 23:09:35 +03:00
// Overwrite
dist() {
var d = super.dist();
return Math.sqrt(d * d + this.z * this.z);
}
2014-08-14 17:56:30 +04:00
}
2015-03-10 23:09:35 +03:00
// Modules, "." can be used as separator for sub modules
2014-08-27 17:41:12 +04:00
module Geometry {
export class Square {
constructor(public sideLength: number = 0) {
}
area() {
return Math.pow(this.sideLength, 2);
}
}
}
var s1 = new Geometry.Square(5);
2015-03-10 23:09:35 +03:00
// Local alias for referencing a module
2014-08-27 17:41:12 +04:00
import G = Geometry;
var s2 = new G.Square(10);
2014-08-14 14:12:50 +04:00
2015-03-10 23:09:35 +03:00
// Generics
// Classes
2014-08-31 19:57:44 +04:00
class Tuple< T1 , T2 > {
constructor(public item1: T1, public item2: T2) {
}
}
2015-03-10 23:09:35 +03:00
// Interfaces
2014-08-31 19:57:44 +04:00
interface Pair< T > {
2015-03-10 23:09:35 +03:00
item1: T;
item2: T;
2014-08-31 19:57:44 +04:00
}
2015-03-10 23:09:35 +03:00
// And functions
2014-08-31 19:57:44 +04:00
var pairToTuple = function< T > (p: Pair< T > ) {
2015-03-10 23:09:35 +03:00
return new Tuple(p.item1, p.item2);
2014-08-31 19:57:44 +04:00
};
var tuple = pairToTuple({ item1:"hello", item2:"world"});
2014-08-14 15:34:41 +04:00
2015-03-10 23:09:35 +03:00
// Including references to a definition file:
2014-08-14 17:40:21 +04:00
/// < reference path = "jquery.d.ts" / >
```
2014-08-14 14:12:50 +04:00
## Further Reading
* [TypeScript Official website] (http://www.typescriptlang.org/)
* [TypeScript language specifications (pdf)] (http://go.microsoft.com/fwlink/?LinkId=267238)
* [Anders Hejlsberg - Introducing TypeScript on Channel 9] (http://channel9.msdn.com/posts/Anders-Hejlsberg-Introducing-TypeScript)
2014-08-14 17:40:21 +04:00
* [Source Code on GitHub] (https://github.com/Microsoft/TypeScript)
* [Definitely Typed - repository for type definitions] (http://definitelytyped.org/)