forked from sjimenez77/typescript-play
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterfaces.ts
88 lines (69 loc) · 1.75 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
interface LabelledValue {
label: string;
}
function printLabel(labelledObj: LabelledValue) {
console.log(labelledObj.label);
}
let myObj = { size: 10, label: 'Size 10 Object' };
printLabel(myObj);
///////////////////////////////////////////////////////////////
interface SquareConfig {
color?: string;
width?: number;
}
interface Square {
color: string;
area: number;
}
function createSquare(config: SquareConfig): Square {
let newSquare = { color: 'white', area: 100 };
if (config.color) {
newSquare.color = config.color;
}
if (config.width) {
newSquare.area = config.width * config.width;
}
return newSquare;
}
let mySquare = createSquare({ color: 'black' });
console.log(mySquare);
///////////////////////////////////////////////////////////////
interface Point {
readonly x: number;
readonly y?: number;
}
let p1: Point = { x: 10, y: 20 };
p1.x = 5; // error!
///////////////////////////////////////////////////////////////
interface SearchFunc {
(source: string, subString: string): boolean;
}
let mySearch: SearchFunc;
mySearch = function(source: string, subString: string) {
let result = source.search(subString);
return result > -1;
};
let mySearch2: SearchFunc;
mySearch2 = function(src: string, sub: string): boolean {
let result = src.search(sub);
return result > -1;
};
///////////////////////////////////////////////////////////////
interface ClockInterface {
currentTime: Date;
}
class Clock implements ClockInterface {
currentTime: Date;
constructor(h: number, m: number) {}
}
interface ClockInterface2 {
currentTime: Date;
setTime(d: Date);
}
class Clock2 implements ClockInterface2 {
currentTime: Date;
setTime(d: Date) {
this.currentTime = d;
}
constructor(h: number, m: number) {}
}