var name: string = "Olli";
function doIt(p1: number, p2: boolean, p3: string): string {
if (p2) {
return p3;
} else {
return "" + p1;
}
}
class Person {
name: string; // public is the default visibility
private age: number; // either public or private
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
toString(): string {
return "Name: " + this.name;
}
}
var olli: Person = new Person("Olli", 42);
console.log(olli.name); // => "Olli"
olli.age; // => Error: Person.age' is inaccessible
class Customer extends Person {
private id: number;
constructor(name: string, age: number, id: number) {
super(name, age);
this.id = id;
}
toString(): string {
return super.toString() + ", id: " + this.id;
}
}
var c1: Customer = new Customer("Oma", 88, 47);
console.log(c1.toString()); // => "Name: Oma, id: 47"
class Customer extends Person {
private static sequence: number = 1;
public static nextNumber(): number {
return Customer.sequence++;
}
private id: number;
constructor(name: string, age: number) {
super(name, age);
this.id = Customer.nextNumber();
}
toString(): string {
return super.toString() + ", id: " + this.id;
}
}
var c1: Customer = new Customer("Oma", 88);
console.log(c1.toString());
module CustomerModule {
class Person {
// ...
}
export class Customer extends Person {
// ...
}
}
// new CustomerModule.Person(...); // => undefined
new CustomerModule.Customer(...); // => cool
interface HasName {
name: string;
}
var olli: HasName = {name: 'Olli'};
olli.name; // good
olli.age; // not good, as not declared
olli = {name: 'Olli', age: 100};
olli.age; // still not good, as not declared
lib.d.ts
)
lib.d.ts
#1declare function parseFloat(string: string): number;
interface Function {
apply(thisArg: any, ...argArray: any[]): any;
call(thisArg: any, ...argArray: any[]): any;
bind(thisArg: any, ...argArray: any[]): Function;
prototype: any;
length: number;
}
lib.d.ts
#2interface Node extends EventTarget {
nodeType: number;
previousSibling: Node;
localName: string;
parentNode: Node;
nextSibling: Node;
// ...
}
tsc
: Command Line Tool for Compiler
These Slides on GitHub: https://github.com/DJCordhose/typescript-hh-js
Code Samples on GitHub: https://github.com/DJCordhose/typescript-sandbox