-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.ts
More file actions
284 lines (226 loc) · 7.41 KB
/
interface.ts
File metadata and controls
284 lines (226 loc) · 7.41 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
let IF = "-------- 인터페이스 ---------";
let SP = "------ 선택적 프로퍼티 ------";
let RP = "------- 읽기 프로퍼티 -------";
let EPC = "----- 초과 프로퍼티 검사 -----";
let FT = "-------- 함수 타입 --------";
let IT = "--------- 인덱서블 타입 --------";
let CT = "--------- 클래스 타입 --------";
let EI = "-------- 인터페이스 확장 -------";
let SI = "------- 스테틱과 인스턴스 ------";
let HT = "-------- 하이브리드 타입 -------";
let CEI = "---- 클래스 확장 인터페이스 ----";
console.log(IF);
// 인터페이스: 객체가 어떤 속성과 메서드를 가져야 하는지 지정
interface labelValue {
size: number;
label: string;
}
function printLabel(labelObj: { label: labelValue }) {
console.log(labelObj.label);
}
let myObj: labelValue = { size: 10, label: "size 10 object" }; // labelValue 타입으로 정의
printLabel({ label: myObj });
console.log(SP);
// 선택적 프로퍼티 : 객체 안의 몇 개의 프로퍼티만 채워 함수에 전달
// 선택적 프로퍼티는 선언에서 프로퍼티 이름 끝에 ?를 붙여 표시
interface SquareConfig {
color?: string; // 색상명
width?: number; // 둘레 수치
}
function createSquare(config: SquareConfig): {color: string; area: number} {
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);
console.log(RP);
// 읽기 프로퍼티 : 할당 후 수정 불가인 속성, 앞에 readonly을 넣는다.
interface read{
readonly x: string;
readonly y: number;
}
let x1: read = { x: "number", y: 123 };
// x1.x = "apb"; // 오류
// x1.y = 5; // 오류
console.log(x1);
let a: number[] = [1, 2, 3, 4];
let arr: ReadonlyArray<number> = a;
console.log(arr);
console.log(EPC);
interface SquareConfig {
color?: string;
width?: number;
[propName: string]: any; // 추가 프로퍼티 허용
}
function createSquare2(config: SquareConfig): { color: string, area: number } {
const color = config.color || "white";
const width = config.width || 10;
const area = width * width;
return { color, area };
}
// let mySquare = createSquare({ colour: "red", width: 100 }); // 현재상태 오류 발생
// let mySquare2 = createSquare2({ width: 100, opacity: 0.5 } as SquareConfig);
let squareOptions = { color: "red", width: 200 } as SquareConfig;
let Square = createSquare(squareOptions);
console.log(Square);
console.log(FT);
// 기본 구문 : 함수의 매개변수와 반환 타입을 지정하는 호출 시그니처를 사용
interface MathOperation {
(a: number, b: number): number;
}
const add: MathOperation = (x, y) => x + y;
const subtract: MathOperation = (x, y) => x - y;
function operate(a: number, b: number, operation: MathOperation): number {
return operation(a, b);
}
// 출력 결과
console.log(add(5, 3)); // 출력: 8
console.log(subtract(5, 3)); // 출력: 2
// 응용
interface Calculator {
name: string; // 이름 타입
add: MathOperation; // 더하기 타입
subtract: MathOperation; // 빼기 타입
}
const calculator: Calculator = {
name: "Simple Calculator",
add: (x, y) => x + y,
subtract: (x, y) => x - y,
};
// 출력 결과
console.log(calculator.name); // 출력: "Simple Calculator"
console.log(calculator.add(10, 5)); // 출력: 15
console.log(calculator.subtract(10, 5)); // 출력: 5
console.log(IT);
//구문 : [key: KeyType]: ValueType 형태
interface StringArray {
[index: number]: string; // 인덱스는 숫자, 키값은 문자
}
let myArray: StringArray = ["Hello", "World"]; // 0번 인덱스 "Hello", 1번 인덱스 "World"
console.log(myArray[0]); // 출력: "Hello"
interface NumberDictionary {
[key: string]: number; // 인덱스는 문자, 키값은 숫자
length: number; // 추가적인 프로퍼티도 정의 가능
}
let myDictionary: NumberDictionary = { length: 5, width: 10 };
console.log(myDictionary["length"]); // 출력: 5
// 주의 : 키 타입은 string 또는 number만 허용
console.log(CT);
interface Person {
name: string;
age: number;
greet(): void;
}
class Employee implements Person {
name: string;
age: number;
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
greet(): void {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}
const employee = new Employee("Alice", 30);
employee.greet(); // Hello, my name is Alice and I am 30 years old.
console.log(EI);
// 기존 인터페이스를 기반으로 새로운 속성이나 메서드를 추가하는 기능
interface Shape {
color: string;
}
interface Square extends Shape {
sideLength: number;
}
let square = {} as Square;
square.color = "red";
square.sideLength = 15;
console.log(square);
console.log(SI);
// 스테틱과 인스턴스의 차이
// 스태틱(static):
// 클래스 자체에 속합니다.
// 클래스 이름을 통해 직접 접근합니다.
// 인스턴스와 관련이 없습니다.
// 인스턴스(instance):
// 클래스의 인스턴스를 통해 접근합니다.
// 인스턴스마다 고유의 값을 가질 수 있습니다.
// 스테틱
class MyClass {
static staticProperty = "I belong to the class!";
static staticMethod() {
console.log("This is a static method.");
}
}
console.log(MyClass.staticProperty); // I belong to the class!
MyClass.staticMethod(); // This is a static method.
// 인스턴스
class MyClass2 {
instanceProperty = "I belong to an instance!";
instanceMethod() {
console.log("This is an instance method.");
}
}
const instance = new MyClass2();
console.log(instance.instanceProperty); // I belong to an instance!
instance.instanceMethod(); // This is an instance method.
console.log(HT);
// 하이브리드 타입 : 나의 객체가 여러 형태를 가질 수 있도록 정의
interface Counter {
(start: number): string; // 함수 시그니처
interval: number; // 속성
reset(): void; // 메서드
}
function getCounter(): Counter {
const counter = ((start: number) => `Count starts from ${start}`) as Counter;
counter.interval = 1000;
counter.reset = () => {
console.log("Counter reset!");
};
return counter;
}
const counter = getCounter();
console.log(counter(5)); // Count starts from 5
console.log(counter.interval); // 1000
counter.reset(); // Counter reset!
console.log(CEI);
// 클래스 확장 인터페이스
// 클래스에서 상속한 필드와 메서드 타입을 인터페이스가 받아들여,
// 그 클래스의 구조를 기반으로 한 인터페이스를 정의할 수 있는 기능
class Animal1 {
name: string;
constructor(name: string) {
this.name = name;
}
makeSound(): void {
console.log(`${this.name} makes a sound.`);
}
}
interface Pet extends Animal1 {
owner: string;
play(): void;
}
class Dog implements Pet {
name: string;
owner: string;
constructor(name: string, owner: string) {
this.name = name;
this.owner = owner;
}
makeSound(): void {
console.log(`${this.name} barks!`);
}
play(): void {
console.log(`${this.name} is playing with ${this.owner}.`);
}
}
// 사용 예시
const myDog = new Dog("Buddy", "Alice");
myDog.makeSound(); // Buddy barks!
myDog.play(); // Buddy is playing with Alice.