forked from rails-camp/typescript-introduction
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path025_interface_classes.ts
More file actions
56 lines (38 loc) · 804 Bytes
/
Copy path025_interface_classes.ts
File metadata and controls
56 lines (38 loc) · 804 Bytes
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
// Loosely connected Interface with Class
interface User {
email: string;
firstName? : string;
lastName? : string;
}
class Admin {
role : string;
constructor(public email : string) {
this.role = 'Admin';
}
}
function profile(user: User) : string {
return `Welcome, ${user.email}`;
}
var joe = new Admin('joe@example.com');
console.log(joe.role);
// Direct implementation
interface IPost {
title: string;
body: string;
}
class Post implements IPost {
title: string;
body: string;
constructor(post: IPost) {
this.title = post.title;
this.body = post.body;
}
printPost() {
console.log(this.title);
console.log(this.body);
}
}
var post = new Post({ title: "My Great Title", body: "Some content"});
console.log(post.title);
console.log(post.body);
post.printPost();