enum Color { Red, Yellow, Green };
let c: Color = Color.Green;
if (c === Color.Green) {
console.log('Lets go!');
}
function bigHorribleAlert(): void {
alert('I'm a little annoying box!');
}
// Function Declaration
function f1(i: number): number {
return i * i;
}
// Function Expression let f1 = function (i: number): number { return i * i; }
let f2 = function (i: number) {
return i * i;
}
let f3 = (i: number): number => {
return i * i;
}
let f4 = (i: number) => { return i * i; }
let f5 = (i: number) => i * i;
var width:number = 100;
let height:number = 200;
const key:string = 'abc123';
var width = 100;
let height = 200;
const key = 'abc123';
var width: number;
let height: number;
// const does not support without initial value
var width;
let height;
// const does not support without initial value
function someFn() {
if (true) {
// defined locally
// its scope ends where curly braces ends
var local = 1000;
console.log(local); //ok
}
console.log(local); //ok
function nested() {
console.log(local); //ok
}
}
console.log(local); //error
function someFn() {
if (true) {
// defined locally
// its scope ends where curly braces ends
let local = 1000;
console.log(local); // ok
}
console.log(local); //error
function nested() {
console.log(local); //error
}
}
console.log(local); //error
interface Person {
name: string;
// Optional properties, marked with a '?'
age?: number;
// And of course functions
move(): void;
}
let p: Person = {
name: 'Bobby',
move: () => { }
};
let validPerson: Person = {
name: 'Bobby',
age: 42, // optional property
move: () => { }
};
let invalidPerson: Person = {
name: 'Bobby',
age: true
};
let mySearch: SearchFunc;
mySearch = function (src: string, sub: string) {
return src.search(sub) != -1;
}
class Point {
// Properties
x: number;
/* 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;
}
// Functions
dist() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
// Static members
static origin = new Point(0, 0);
}
let p1 = new Point(10, 20);
let p2 = new Point(25); //y will be 0
class PointPerson implements Person {
name: string;
move() {}
}
class Point3D extends Point {
constructor(x: number, y: number, public z: number = 0) {
// Explicit call to the super class constructor
// is mandatory
super(x, y);
}
// Override
dist() {
let d = super.dist();
return Math.sqrt(d() * d() + this.z * this.z);
}
}
class Tuple<T1, T2> {
constructor(public item1: T1, public item2: T2) {}
}
interface Pair<T> {
item1: T;
item2: T;
}
let pairToTuple = function <T>(p: Pair<T>) {
return new Tuple(p.item1, p.item2);
};
let tuple = pairToTuple({
item1: 'hello',
item2: 'world'
});
let name = 'Tyrone';
let greeting = `Hi ${name}, how are you?`
let multiline = `This is an example
of a multiline string`;
let arrayOfAnyType = [1, 'string', false];
for (const val of arrayOfAnyType) { console.log(val); // 1, 'string', false }
let list = [4, 5, 6];
for (const i of list) {
console.log(i); // 4, 5, 6
}
let arrayOfAnyType = [1, 'string', false];
for (const val of arrayOfAnyType) {
console.log(val); // 1, 'string', false
}
let list = [4, 5, 6];
for (const i of list) { console.log(i); // 4, 5, 6 }
for (const i in list) {
console.log(i); // 0, 1, 2
}