Skip to content

Commit f5240ca

Browse files
committed
๐Ÿšฉ: interface๋กœ ์„ ์–ธ๋œ ํƒ€์ž…์˜ ํƒ€์ž…์„ keyof๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•
1 parent b0c1106 commit f5240ca

File tree

2 files changed

+11
-40
lines changed

2 files changed

+11
-40
lines changed

โ€Žsrc/index.tsโ€Ž

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,5 @@
1-
//์ ‘๊ทผ ์ œํ•œ์ž(Access modifier) - public, private, protected / es6์—์„œ๋Š” ์ ‘๊ทผ ์ œํ•œ์ž๋ฅผ ์ง€์›ํ•˜์ง€ ์•Š์•˜๋‹ค ํ•˜์ง€๋งŒ ts์—์„œ๋Š” ์ œ๊ณตํ•ด์ค€๋‹ค.
1+
import { User } from "./utils/data.interface";
2+
// ์œ ๋‹ˆ์˜จ ํƒ€์ž…์˜ user์˜ key ๊ฐ’์„ ๊ฐ€์ง€๊ณ ์™€ keyof๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
3+
type UserKey = keyof User;
24

3-
abstract class Car {
4-
color: string;
5-
constructor(color: string) {
6-
this.color = color;
7-
}
8-
start(): void {
9-
console.log("start");
10-
}
11-
abstract doSomething(): string; //์•„๋ฌด ์ž‘์—…์„ ํ•˜์ง€ ์•Š์€ ์ถ”์ƒ ํ•จ์ˆ˜ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค์— ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์—†๋‹ค๋ฉด error ๋ฐœ์ƒ
12-
}
13-
14-
// const Car = new Car("red"); //-> error : ์ถ”์ƒํด๋ž˜์Šค๋Š” new ์ธ์Šคํ„ด์Šค๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์—†๋‹ค. ์ถ”์ƒํด๋ž˜์Šค๋Š” ์ฒญ์‚ฌ์ง„๊ณผ ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐํ•˜๋ฉฐ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ค๋กœ์ง€ ์ƒ์†์„ ํ†ตํ•ด์„œ๋งŒ ๊ฐ€๋Šฅํ•˜๋‹ค.
15-
16-
class Bmw extends Car {
17-
constructor(color: string) {
18-
super(color);
19-
// ์ฐธ๊ณ ๋กœ 'super()'๋Š” ๋ถ€๋ชจ(์ผ๋ฐ˜์ ์ธ super๊ฐ€ ์•„๋‹˜)์˜ constructor์— ์ ‘๊ทผ
20-
}
21-
// abstract doSomething()๊ฐ€ ์—†๋‹ค๋ฉด class๋Š” error, ์ฆ‰, ์ถ”์ƒ ํด๋ž˜์Šค๋Š” ์ƒ์†์„ ๋ฐ›์€ ์ชฝ์—์„œ ํ•ด๋‹น ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
22-
doSomething(): string {
23-
// alert("do!"); //-> error : alert๋Š” browser์—์„œ๋งŒ ์‚ฌ์šฉ์ด ๊ฐ€๋Šฅ
24-
console.log("do!");
25-
return 'Do!';
26-
}
27-
// - private: ๋ถ€๋ชจ name์ด private์ธ ๊ฒฝ์šฐ error๊ฐ€ ๋‚˜์˜จ๋‹ค. ์ถ”๊ฐ€์ ์œผ๋กœ '#name'์€ private๋กœ ์ธ์‹ํ•œ๋‹ค.
28-
// - protected: ๋ถ€๋ชจ name์ด protected์ธ ๊ฒฝ์šฐ ์ •์ƒ ๋™์ž‘ํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด public๊ณผ ์ฐจ์ด๋Š” ๋ฌด์—‡์ธ๊ฐ€.
29-
/*
30-
* new๋ฅผ ํ†ตํ•ด์„œ ์ธ์Šคํ„ด์Šค ํ–ˆ์„ ๊ฒฝ์šฐ ์ ‘๊ทผ์„ ์ œํ•œํ•˜๊ฒŒ ๋œ๋‹ค. ์ฆ‰, ์ž์‹ ํด๋ž˜์Šค์—์„œ๋Š” ๋ถ€๋ชจ ์†์„ฑ์— ์ ‘๊ทผ์„ ํ•  ์ˆ˜ ์žˆ์œผ๋‚˜ new ์ธ์Šคํ„ด์Šค์ธ ๊ฒฝ์šฐ ์ ‘๊ทผ์ด ๋ถˆ๊ฐ€ํ•˜๋‹ค.
31-
*/
32-
// - readonly: new ์ธ์Šคํ„ด์Šค๋ฅผ ํ†ตํ•ด์„œ ๊ฐ’์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์—†๋‹ค. ๊ทธ๋ ‡๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผ ๋ณ€๊ฒฝ์„ ํ•  ์ˆ˜ ์žˆ์„๊นŒ?
33-
/*
34-
* ๋ถ€๋ชจ constructor ๋‚ด๋ถ€์—์„œ ์ž‘์—…์„ ํ•ด์•ผํ•œ๋‹ค.
35-
*/
36-
// - static: static์€ ์ •์  ๋งด๋ฒ„ ๋ณ€์ˆ˜(property)๋ฅผ ๋งŒ๋“ค์–ด ์ค„ ์ˆ˜ ์žˆ๋‹ค. static์— ์ ‘๊ทผ์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” this๊ฐ€ ์•„๋‹Œ class ์ด๋ฆ„์œผ๋กœ ์ ‘๊ทผ์„ ํ•ด์•ผ ํ•œ๋‹ค.
37-
// - abstract: abstract์€ ์ถ”์ƒ ํด๋ž˜์Šค(class) ๋˜๋Š” ํ•จ์ˆ˜, ๋ณ€์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
38-
}
39-
40-
const z4 = new Bmw("black");
41-
console.log(z4.doSomething()); //undefined๊ฐ€ ์ถœ๋ ฅ๋˜๋Š” ์ด์œ  : doSomething์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด void์ด๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
5+
const uk: UserKey = 'id';

โ€Žsrc/utils/data.interface.tsโ€Ž

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,11 @@ export interface InterToy {
7474
name: string;
7575
color: string;
7676
price: number;
77+
}
78+
79+
export interface User {
80+
id: number;
81+
name: string;
82+
age: number;
83+
gender: 'm' | 'f';
7784
}

0 commit comments

Comments
ย (0)