1//declare interface to be extended
2interface Vehicle {
3 brand: string;
4 plateNumber: number;
5}
6
7interface Car extends Vehicle {}
1interface Task{
2 name: String; //property
3 run(arg: any):void; //method
4}
5
6class MyTask implements Task{
7 name: String;
8 constructor(name: String) {
9 this.name = name;
10 }
11 run(arg: any): void {
12 console.log(`running: ${this.name}, arg: ${arg}`);
13 }
14}
15
16let myTask: Task = new MyTask('someTask');
17myTask.run("test");
1interface Animal {
2 name: string
3}
4interface Bear extends Animal {
5 honey: boolean
6}
7const bear = getBear()
8bear.name
9bear.honey
10
1class Animal {
2 name: string;
3 constructor(theName: string) { this.name = theName; }
4 move(distanceInMeters: number = 0) {
5 console.log(`${this.name} moved ${distanceInMeters}m.`);
6 }
7}
8
9class Snake extends Animal {
10 constructor(name: string) { super(name); }
11 move(distanceInMeters = 5) {
12 console.log("Slithering...");
13 super.move(distanceInMeters);
14 }
15}
16
17class Horse extends Animal {
18 constructor(name: string) { super(name); }
19 move(distanceInMeters = 45) {
20 console.log("Galloping...");
21 super.move(distanceInMeters);
22 }
23}
24
25let sam = new Snake("Sammy the Python");
26let tom: Animal = new Horse("Tommy the Palomino");
27
28sam.move();
29tom.move(34);