@@ -5,35 +5,35 @@ permalink: /docs/handbook/literal-types.html
5
5
oneline : Using literal types with TypeScript
6
6
---
7
7
8
- A literal is a more concrete sub-type of a collective type .
9
- What this means is that ` "Hello World" ` is a ` string ` , but a ` string ` is not ` "Hello World" ` inside the type system .
8
+ ๋ฆฌํฐ๋ด ํ์
์ ์งํฉ ํ์
์ ๋ณด๋ค ๊ตฌ์ฒด์ ์ธ ํ์ ํ์
์
๋๋ค .
9
+ ์ด๊ฒ์ด ์๋ฏธํ๋ ๋ฐ๋ ํ์
์์คํ
์์์ ` "Hello World" ` ๋ ` string ` ์ด์ง๋ง, ` string ` ์ ` "Hello World" ` ๊ฐ ์๋๋ ๊ฒ์
๋๋ค .
10
10
11
- There are two sets of literal types available in TypeScript today, strings and numbers, by using literal types you can allow an exact value which a string or number must have .
11
+ ์ค๋๋ TypeScript์๋ ๋ฌธ์์ด๊ณผ ์ซ์, ๋ ๊ฐ์ง ๋ฆฌํฐ๋ด ํ์
์ด ์๋๋ฐ ์ด๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฌธ์์ด์ด๋ ์ซ์์ ์ ํํ ๊ฐ์ ์ง์ ํ ์ ์์ต๋๋ค .
12
12
13
- # Literal Narrowing
13
+ # ๋ฆฌํฐ๋ด ํ์
์ขํ๊ธฐ ( Literal Narrowing)
14
14
15
- When you declare a variable via ` var ` or ` let ` , you are telling the compiler that there is the chance that this variable will change its contents .
16
- In contrast, using ` const ` to declare a variable will inform TypeScript that this object will never change .
15
+ ` var ` ๋๋ ` let ` ์ผ๋ก ๋ณ์๋ฅผ ์ ์ธํ ๊ฒฝ์ฐ ์ด ๋ณ์์ ๊ฐ์ด ๋ณ๊ฒฝ๋ ๊ฐ๋ฅ์ฑ์ด ์์์ ์ปดํ์ผ๋ฌ์๊ฒ ์๋ฆฝ๋๋ค .
16
+ ๋ฐ๋ฉด, ` const ` ๋ก ๋ณ์๋ฅผ ์ ์ธํ๊ฒ ๋๋ฉด TypeScript์๊ฒ ์ด ๊ฐ์ฒด๋ ์ ๋ ๋ณ๊ฒฝ๋์ง ์์์ ์๋ฆฝ๋๋ค .
17
17
18
- ``` ts twoslash
19
- // We're making a guarantee that this variable
20
- // helloWorld will never change, by using const .
18
+ ``` ts
19
+ // const๋ฅผ ์ฌ์ฉํ์ฌ ๋ณ์ helloWorld๊ฐ
20
+ // ์ ๋ ๋ณ๊ฒฝ๋์ง ์์์ ๋ณด์ฅํฉ๋๋ค .
21
21
22
- // So, TypeScript sets the type to be "Hello World" not string
22
+ // ๋ฐ๋ผ์, TypeScript๋ ๋ฌธ์์ด์ด ์๋ "Hello World"๋ก ํ์
์ ์ ํฉ๋๋ค.
23
23
const helloWorld = " Hello World" ;
24
24
25
- // On the other hand, a let can change, and so the compiler declares it a string
25
+ // ๋ฐ๋ฉด, let์ ๋ณ๊ฒฝ๋ ์ ์์ผ๋ฏ๋ก ์ปดํ์ผ๋ฌ๋ ๋ฌธ์์ด์ด๋ผ๊ณ ์ ์ธํ ๊ฒ์
๋๋ค.
26
26
let hiWorld = " Hi World" ;
27
27
```
28
28
29
- The process of going from an infinite number of potential cases (there are an infinite number of possible string values) to a smaller, finite number of potential case (in ` helloWorld ` 's case: 1) is called narrowing.
29
+ ๋ฌดํํ ์์ ์ ์ฌ์ ์ผ์ด์ค๋ค (๋ฌธ์์ด ๊ฐ์ ๊ฒฝ์ฐ์ ์๊ฐ ๋ฌดํ๋)์ ์ ํํ ์์ ์ ์ฌ์ ์ผ์ด์ค ( ` helloWorld ` ์ ๊ฒฝ์ฐ: 1๊ฐ)๋ก ์ค์ฌ๋๊ฐ๋ ๊ฒ์ ํ์
์ขํ๊ธฐ ( narrowing)๋ผ ํ๋ค .
30
30
31
- # String Literal Types
31
+ # ๋ฌธ์์ด ๋ฆฌํฐ๋ด ํ์
( String Literal Types)
32
32
33
- In practice string literal types combine nicely with union types, type guards, and type aliases .
34
- You can use these features together to get enum-like behavior with strings .
33
+ ์ค์ ๋ก ๋ฌธ์์ด ๋ฆฌํฐ๋ด ํ์
์ ์ ๋์ธ ํ์
, ํ์
๊ฐ๋ ๊ทธ๋ฆฌ๊ณ ํ์
๋ณ์นญ๊ณผ ์ ๊ฒฐํฉ๋ฉ๋๋ค .
34
+ ์ด๋ฐ ๊ธฐ๋ฅ์ ํจ๊ป ์ฌ์ฉํ์ฌ ๋ฌธ์์ด๋ก enum๊ณผ ๋น์ทํ ํํ๋ฅผ ๊ฐ์ถ ์ ์์ต๋๋ค .
35
35
36
- ``` ts twoslash
36
+ ``` ts
37
37
// @errors: 2345
38
38
type Easing = " ease-in" | " ease-out" | " ease-in-out" ;
39
39
@@ -44,8 +44,8 @@ class UIElement {
44
44
} else if (easing === " ease-out" ) {
45
45
} else if (easing === " ease-in-out" ) {
46
46
} else {
47
- // It's possible that someone could reach this
48
- // by ignoring your types though .
47
+ // ํ์ง๋ง ๋๊ตฐ๊ฐ๊ฐ ํ์
์ ๋ฌด์ํ๊ฒ ๋๋ค๋ฉด
48
+ // ์ด๊ณณ์ ๋๋ฌํ๊ฒ ๋ ์ ์์ต๋๋ค .
49
49
}
50
50
}
51
51
}
@@ -55,41 +55,41 @@ button.animate(0, 0, "ease-in");
55
55
button .animate (0 , 0 , " uneasy" );
56
56
```
57
57
58
- You can pass any of the three allowed strings, but any other string will give the error
58
+ ํ์ฉ๋ ์ธ ๊ฐ์ ๋ฌธ์์ด์ด ์๋ ๋ค๋ฅธ ๋ฌธ์์ด์ ์ฌ์ฉํ๊ฒ ๋๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
59
59
60
60
```
61
- Argument of type '"uneasy"' is not assignable to parameter of type '"ease-in" | "ease-out" | "ease-in-out"'
61
+ '"uneasy"' ํ์
์ '"ease-in" | "ease-out" | "ease-in-out"' ํ์
์ ๋งค๊ฐ ๋ณ์์ ํ ๋นํ ์ ์์ต๋๋ค.
62
62
```
63
63
64
- String literal types can be used in the same way to distinguish overloads :
64
+ ๋ฌธ์์ด ๋ฆฌํฐ๋ด ํ์
์ ์ค๋ฒ๋ก๋๋ฅผ ๊ตฌ๋ณํ๋ ๊ฒ๊ณผ ๋์ผํ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉ๋ ์ ์์ต๋๋ค :
65
65
66
66
``` ts
67
67
function createElement(tagName : " img" ): HTMLImageElement ;
68
68
function createElement(tagName : " input" ): HTMLInputElement ;
69
- // ... more overloads ...
69
+ // ... ์ถ๊ฐ์ ์ธ ์ค๋ณต ์ ์๋ค ...
70
70
function createElement(tagName : string ): Element {
71
- // ... code goes here ...
71
+ // ... ์ฌ๊ธฐ์ ๋ก์ง ์ถ๊ฐ ...
72
72
}
73
73
```
74
74
75
- # Numeric Literal Types
75
+ # ์ซ์ํ ๋ฆฌํฐ๋ด ํ์
( Numeric Literal Types)
76
76
77
- TypeScript also has numeric literal types, which act the same as the string literals above .
77
+ TypeScript์๋ ์์ ๋ฌธ์์ด ๋ฆฌํฐ๋ด๊ณผ ๊ฐ์ ์ญํ ์ ํ๋ ์ซ์ํ ๋ฆฌํฐ๋ด ํ์
๋ ์์ต๋๋ค .
78
78
79
- ``` ts twoslash
79
+ ``` ts
80
80
function rollDice(): 1 | 2 | 3 | 4 | 5 | 6 {
81
81
return (Math .floor (Math .random () * 6 ) + 1 ) as 1 | 2 | 3 | 4 | 5 | 6 ;
82
82
}
83
83
84
84
const result = rollDice ();
85
85
```
86
86
87
- A common case for their use is for describing config values :
87
+ ์ด๋ ์ฃผ๋ก ์ค์ ๊ฐ์ ์ค๋ช
ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค :
88
88
89
- ``` ts twoslash
90
- /** Creates a map centered at loc/lat */
89
+ ``` ts
90
+ /** loc/lat ์ขํ์ ์ง๋๋ฅผ ์์ฑํฉ๋๋ค. */
91
91
declare function setupMap(config : MapConfig ): void ;
92
- // ---cut ---
92
+ // ---์๋ต ---
93
93
interface MapConfig {
94
94
lng: number ;
95
95
lat: number ;
0 commit comments