//Typed functions
function concat(a: string, b: string): string {
return a + b;
}
console.log(concat("a", "b"));
console.log(concat(concat("a", "b"), "c"));
console.log(concat(1, 2)); // <-- This is wrong!!!
console.log("---");
//Interfaces
interface Point {
x: number;
y: number;
}
function translatePoint(point: Point, translation: Point): Point {
return {
x: point.x + translation.x,
y: point.y + translation.y
};
}
console.log(translatePoint({ x: 2, y: 4 }, { x: 1, y: 2 }));
console.log(translatePoint({ x: 2, y: 4 }, { x: 1, y: 2, z: 3 })); // <-- This is wrong!!!
console.log(translatePoint({ x: 2, y: 4 }, { x: 1, z: 3 })); // <-- This is wrong!!!
console.log("---");
//Classes
class Point3D {
x: number;
y: number;
z: number;
constructor(x: number, y: number, z: number) {
this.x = x;
this.y = y;
this.z = z;
}
}
var point3D = new Point3D(1, 2, 3);
console.log(point3D);
console.log(translatePoint(point3D, { x: 1, y: 2 })); // <-- This is ok, Point3D is implicitly "implementing" Point
console.log("---");
class Point3D_2 {
constructor(public x: number, public y: number, public z: number) { // <-- Using "public" we create the properties
}
}
var point3D_2 = new Point3D_2(4, 5, 6);
console.log(point3D_2); // <-- this is equivalent to point3D
console.log("---");
//Methods
class Point3D_3 {
constructor(public x: number, public y: number, public z: number) { // <-- Using "public" we create the properties
}
dinstance() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2) + Math.pow(this.z, 2));
}
}
var point3D_3 = new Point3D_3(4, 5, 6);
console.log(point3D_3.dinstance());
console.log("---");
//Private Methods (Protected is available too)
class Point3D_4 {
constructor(public x: number, public y: number, public z: number) { // <-- Using "public" we create the properties
}
private dinstance() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2) + Math.pow(this.z, 2));
}
}
var point3D_4 = new Point3D_4(4, 5, 6);
console.log(point3D_4.dinstance()); // <-- This is wrong!!!
console.log("---");
//Getters & Setters
class Point3D_5 {
constructor(public x: number, public y: number, public z: number) { // <-- Using "public" we create the properties
}
get dinstance() {
return Math.sqrt(Math.pow(this.x, 2) + Math.pow(this.y, 2) + Math.pow(this.z, 2));
}
}
var point3D_5 = new Point3D_5(4, 5, 6);
console.log(point3D_5.dinstance);
point3D_5.dinstance = 0; // <-- This is wrong!!!
console.log(point3D_5.dinstance);
console.log("---");
//Inheritance
class Animal {
name: string;
constructor(name: string) {
this.name = name;
}
move(distanceInMeters: number = 0) { // <-- Notice the default value for the parameter
console.log(this.name + " moved " + distanceInMeters);
}
}
class Snake extends Animal {
constructor(name: string) {
super(name); // <-- Calling super class
}
move(distanceInMeters = 5) { // <-- Overriding method
console.log("Slithering...");
super.move(distanceInMeters);
}
}
class Horse extends Animal {
constructor(name: string) {
super(name); // <-- Calling super class
}
move(distanceInMeters = 45) { // <-- Overriding method
console.log("Galloping...");
super.move(distanceInMeters);
}
}
var sam = new Snake("Sammy the Python");
var tom: Animal = new Horse("Tommy the Palomino");