-
Notifications
You must be signed in to change notification settings - Fork 0
/
interfaces.ts
55 lines (47 loc) · 1.17 KB
/
interfaces.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/**
* Interface: Works only to describe an object
* Type Alias: Works to describe an object and primitive types
*/
interface Game {
title: string;
description: string;
readonly genre: string;
platform?: string[];
getSimilars?: (title: string) => void;
}
const gameTLOU: Game = {
title: "The Last of Us",
description: "The best game in the world",
genre: "action",
getSimilars: (title: string) => {
console.log(`Similar games to ${title}: ...`);
},
};
console.log(gameTLOU.genre);
if (gameTLOU.getSimilars) {
gameTLOU.getSimilars("Metro");
}
// Extending an interface to another
interface DLC extends Game {
originalGame: Game;
newContent: string[];
}
const leftBehind: DLC = {
title: "The Last of Us - Left Behind",
description: "You play as Ellie before the original game",
genre: "action",
platform: ["PS4"],
originalGame: gameTLOU,
newContent: ["new characters", "3 hours story"],
};
// Implementing a class with an 'interface'
class CreateGame implements Game {
title: string;
description: string;
genre: string;
constructor(t: string, d: string, g: string) {
this.title = t;
this.description = d;
this.genre = g;
}
}