-Activate transpilation in ES5
Scope :
public
private
protected
static
class Demo {
private _element
constructor () {
}
// Getter & setter
set element (value: string) {
this._element = value
}
get element (): string {
return this._element
}
}
let d = new Demo()
// Getters & setters are automatically called
d.element = 'Salut'
console.log(d.element);
class Animal {
move() {
console.log("Moving along!");
}
}
class Dog extends Animal {
woof(times: number) {
for (let i = 0; i < times; i++) {
console.log("woof!");
}
}
}
const d = new Dog();
// Base class method
d.move();
// Derived class method
d.woof(3);
Interface for param :
interface LabeledValue {
label: string;
}
function printLabel(labeledObj: LabeledValue) {
console.log(labeledObj.label);
}
let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj);
Interface for class :
interface ClockInterface {
currentTime: Date;
setTime(d: Date): void;
}
class Clock implements ClockInterface {
currentTime: Date = new Date();
setTime(d: Date) {
this.currentTime = d;
}
constructor(h: number, m: number) {}
}