Skip to content

Commit 9cce71a

Browse files
committed
๐Ÿšฉ: ts์˜ enum ์„ ์–ธ ๋ฐฉ์‹๊ณผ ๋ฐฉ๋ฒ•
1 parent a150497 commit 9cce71a

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

โ€Žsrc/index.tsโ€Ž

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,35 @@
1-
//ํ•จ์ˆ˜ void, never
1+
//enum : ํŠน์ • ๊ฐ’, ๊ณตํ†ต๋˜๋Š” ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๊ณ  ์‹ถ์„ ๋•Œ enum์„ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค.enum์€ key, value๋กœ ๋‘ ์Œ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.
22

3-
//void: void๋Š” ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์ด ์—†์„ ๋•Œ์ด๋‹ค. ์ฆ‰, ํ•จ์ˆ˜๊ฐ€ ์•„๋ฌด๋Ÿฐ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š์Œ์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.
4-
function sayHello(): void {
5-
console.log('hello');
3+
enum Os {
4+
Window = 3,
5+
Ios = 10,
6+
Android
67
}
78

8-
//never : never๋Š” ํ•ญ์ƒ error๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฑฐ๋‚˜ ์˜์›ํžˆ ๋๋‚˜์ง€ ์•Š๋Š” ๋ฌดํ•œloop์ผ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ์ฆ‰, ํ•จ์ˆ˜๊ฐ€ ์ ˆ๋Œ€์ ์œผ๋กœ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ์— ์‚ฌ์šฉ๋œ๋‹ค.
9-
function showError(): never {
10-
throw new Error();
9+
console.log(Os['Ios']); //10
10+
console.log(Os[10]); //Ios
11+
12+
//enum์„ ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž์—ด๋กœ ์ง€์ •ํ–ˆ์„ ๊ฒฝ์šฐ ๋‹จ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ์ž‘์šฉํ•œ๋‹ค. ์ฆ‰, ํ‚ค(Key)๋Š” ํ•ญ์ƒ ๋ฌธ์ž์—ด๋กœ ์ •์˜ ํ•ด์•ผํ•œ๋‹ค. ๊ฐ’(Value)์„ ๋ฌธ์ž์—ด๋กœ ์ •์˜ํ•˜๋ฉด ํ‚ค์™€ ํ˜ผ๋™์ด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ ‡๋‹ค๋Š” ๊ฑฐ์ง€?
13+
enum Os2 {
14+
Window = 'win',
15+
Ios = 'ios',
16+
Android = 'and'
1117
}
1218

13-
function infLoop(): never {
14-
while (true) {
15-
//do something...
16-
}
17-
}
19+
//๊ฐ„์ ‘ ์ ‘๊ทผ
20+
//key
21+
console.log(Os2['Window']);
22+
console.log(Os2['Ios']);
23+
//value
24+
// console.log(Os2['ios']); //-> error
25+
// console.log(Os2['win']); //-> error key๋Š” ํ•ญ์ƒ ๋ฌธ์ž์—ด๋กœ ์ง€์ •๋˜์–ด ์žˆ์–ด์„œ ๊ฐ’(value)๊ฐ€ ๋ฌธ์ž์—ด์ด๋ฉด ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
26+
27+
//๊ทธ๋ ‡๋‹ค๋ฉด ๋ฌธ์ž์—ด๋กœ ์ •์˜ ๋˜์–ด ์žˆ๋Š” ๊ฐ’(value)์— ์ ‘๊ทผ์„ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ? ์•„๋ž˜์™€ ๊ฐ™์ด ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
28+
29+
//์ง์ ‘ ์ ‘๊ทผ
30+
console.log(Os2.Window);
31+
//๊ฐ’ ํ• ๋‹น์„ ํ†ตํ•œ ์ง์ ‘ ์ ‘๊ทผ ๋ฐฉ์‹
32+
let myOs: Os2;
33+
34+
myOs = Os2.Window
35+
console.log(myOs);

0 commit comments

Comments
ย (0)