-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.ts
193 lines (154 loc) · 3.4 KB
/
Test.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
interface Ab {
a: number;
b: number;
}
function test(obj : Ab) {
console.log(obj.a);
}
test({a: 1, b: 2});
interface Hi {
hello(): void;
bye(): void;
}
type Bye = {
hello(): void;
bye(): void;
}
class SayI implements Hi {
hello(): void {
console.log("hi");
}
bye(): void {
console.log("bye");
}
}
class SayT implements Bye {
hello(): void {
console.log("hi");
}
bye(): void {
console.log("bye");
}
}
const ab: SayI = new SayI();
ab.hello();
ab.bye();
const b: SayT = new SayT();
b.hello();
b.bye();
interface HiBye {
hi: string;
bye?: string;
}
const hi = {hi: 'hello'};
function foo(obj: HiBye): void {
console.log(obj.hi);
}
foo(hi);
interface HiByeA {
readonly hi: string;
bye?: string;
}
const hiA = {hi: 'hello'};
// 이곳에서 정의한 hiA의 객체에서 hi속성의 값을 변경할 수 없다.
function fooA(obj: HiByeA): void {
console.log(obj.hi);
}
fooA(hiA);
let arr: ReadonlyArray<number> = [1, 2, 3];
interface A {
a?: string;
}
function a(obj: A) {
}
a({b: 'b'}); // error
interface A {
a?: string;
[b: string]: any;
}
// interface Add {
// (operand1: number, operand2: number): number;
// }
//
// const cal: Add = (a: number, b: number): number => {
// console.log(a + b);
// return a + b;
// }
//
// cal(1, 2);
interface Cal {
add(operand1: number, operand2: number): number;
sub(operand1: number, operand2: number): number;
}
class Calculator implements Cal {
add(operand1: number, operand2: number): number {
return operand1 + operand2;
}
sub(operand1: number, operand2: number): number {
return operand1 - operand2;
}
}
const c: Calculator = new Calculator();
console.log(c.add(1, 2), c.sub(2, 3));
interface Add {
add(operand1: number, operand2: number): number;
}
interface Sub {
sub(operand1: number, operand2: number): number;
}
interface Cals extends Add, Sub {
print(res: number): void;
}
class Calcul implements Cals {
add(operand1: number, operand2: number): number {
return operand1 + operand2;
}
sub(operand1: number, operand2: number): number {
return operand1 - operand2;
}
print(res: number): void {
console.log(res);
}
}
const cc: Calcul = new Calcul();
cc.print(cc.add(1,2));
cc.print(cc.sub(1,2));
interface Add {
add(operand1: number, operand2: number): number;
}
interface Sub {
sub(operand1: number, operand2: number): number;
}
interface Cals {
print(res: number): void;
}
class CalculA implements Cals, Add, Sub {
add(operand1: number, operand2: number): number {
return operand1 + operand2;
}
sub(operand1: number, operand2: number): number {
return operand1 - operand2;
}
print(res: number): void {
console.log(res);
}
}
const ccc: CalculA = new CalculA();
ccc.print(ccc.add(1,2));
ccc.print(ccc.sub(1,2));
// 함수 타입이면서 객체 타입을 정의할 수 있는 인터페이스
interface Adder {
(operand1: number, operand2: number): number;
op: string;
print(ans: number): void;
}
function add(): Adder {
const tmpAdd = (function(a: number, b: number){return a + b;}) as Adder;
tmpAdd.op = '+';
tmpAdd.print = function(c: number) {console.log(c)};
return tmpAdd;
}
const realAdd = add();
realAdd(1, 2);
console.log(realAdd.op);
realAdd.print(realAdd(1, 2));