@@ -5,35 +5,35 @@ permalink: /docs/handbook/literal-types.html
55oneline : Using literal types with TypeScript
66---
77
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" ` ๊ฐ ์๋๋ ๊ฒ์
๋๋ค .
1010
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์๋ ๋ฌธ์์ด๊ณผ ์ซ์, ๋ ๊ฐ์ง ๋ฆฌํฐ๋ด ํ์
์ด ์๋๋ฐ ์ด๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฌธ์์ด์ด๋ ์ซ์์ ์ ํํ ๊ฐ์ ์ง์ ํ ์ ์์ต๋๋ค .
1212
13- # Literal Narrowing
13+ # ๋ฆฌํฐ๋ด ํ์
์ขํ๊ธฐ ( Literal Narrowing)
1414
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์๊ฒ ์ด ๊ฐ์ฒด๋ ์ ๋ ๋ณ๊ฒฝ๋์ง ์์์ ์๋ฆฝ๋๋ค .
1717
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+ // ์ ๋ ๋ณ๊ฒฝ๋์ง ์์์ ๋ณด์ฅํฉ๋๋ค .
2121
22- // So, TypeScript sets the type to be "Hello World" not string
22+ // ๋ฐ๋ผ์, TypeScript๋ ๋ฌธ์์ด์ด ์๋ "Hello World"๋ก ํ์
์ ์ ํฉ๋๋ค.
2323const helloWorld = " Hello World" ;
2424
25- // On the other hand, a let can change, and so the compiler declares it a string
25+ // ๋ฐ๋ฉด, let์ ๋ณ๊ฒฝ๋ ์ ์์ผ๋ฏ๋ก ์ปดํ์ผ๋ฌ๋ ๋ฌธ์์ด์ด๋ผ๊ณ ์ ์ธํ ๊ฒ์
๋๋ค.
2626let hiWorld = " Hi World" ;
2727```
2828
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)๋ผ ํ๋ค .
3030
31- # String Literal Types
31+ # ๋ฌธ์์ด ๋ฆฌํฐ๋ด ํ์
( String Literal Types)
3232
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๊ณผ ๋น์ทํ ํํ๋ฅผ ๊ฐ์ถ ์ ์์ต๋๋ค .
3535
36- ``` ts twoslash
36+ ``` ts
3737// @errors: 2345
3838type Easing = " ease-in" | " ease-out" | " ease-in-out" ;
3939
@@ -44,8 +44,8 @@ class UIElement {
4444 } else if (easing === " ease-out" ) {
4545 } else if (easing === " ease-in-out" ) {
4646 } else {
47- // It's possible that someone could reach this
48- // by ignoring your types though .
47+ // ํ์ง๋ง ๋๊ตฐ๊ฐ๊ฐ ํ์
์ ๋ฌด์ํ๊ฒ ๋๋ค๋ฉด
48+ // ์ด๊ณณ์ ๋๋ฌํ๊ฒ ๋ ์ ์์ต๋๋ค .
4949 }
5050 }
5151}
@@ -55,41 +55,41 @@ button.animate(0, 0, "ease-in");
5555button .animate (0 , 0 , " uneasy" );
5656```
5757
58- You can pass any of the three allowed strings, but any other string will give the error
58+ ํ์ฉ๋ ์ธ ๊ฐ์ ๋ฌธ์์ด์ด ์๋ ๋ค๋ฅธ ๋ฌธ์์ด์ ์ฌ์ฉํ๊ฒ ๋๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
5959
6060```
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"' ํ์
์ ๋งค๊ฐ ๋ณ์์ ํ ๋นํ ์ ์์ต๋๋ค.
6262```
6363
64- String literal types can be used in the same way to distinguish overloads :
64+ ๋ฌธ์์ด ๋ฆฌํฐ๋ด ํ์
์ ์ค๋ฒ๋ก๋๋ฅผ ๊ตฌ๋ณํ๋ ๊ฒ๊ณผ ๋์ผํ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉ๋ ์ ์์ต๋๋ค :
6565
6666``` ts
6767function createElement(tagName : " img" ): HTMLImageElement ;
6868function createElement(tagName : " input" ): HTMLInputElement ;
69- // ... more overloads ...
69+ // ... ์ถ๊ฐ์ ์ธ ์ค๋ณต ์ ์๋ค ...
7070function createElement(tagName : string ): Element {
71- // ... code goes here ...
71+ // ... ์ฌ๊ธฐ์ ๋ก์ง ์ถ๊ฐ ...
7272}
7373```
7474
75- # Numeric Literal Types
75+ # ์ซ์ํ ๋ฆฌํฐ๋ด ํ์
( Numeric Literal Types)
7676
77- TypeScript also has numeric literal types, which act the same as the string literals above .
77+ TypeScript์๋ ์์ ๋ฌธ์์ด ๋ฆฌํฐ๋ด๊ณผ ๊ฐ์ ์ญํ ์ ํ๋ ์ซ์ํ ๋ฆฌํฐ๋ด ํ์
๋ ์์ต๋๋ค .
7878
79- ``` ts twoslash
79+ ``` ts
8080function rollDice(): 1 | 2 | 3 | 4 | 5 | 6 {
8181 return (Math .floor (Math .random () * 6 ) + 1 ) as 1 | 2 | 3 | 4 | 5 | 6 ;
8282}
8383
8484const result = rollDice ();
8585```
8686
87- A common case for their use is for describing config values :
87+ ์ด๋ ์ฃผ๋ก ์ค์ ๊ฐ์ ์ค๋ช
ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค :
8888
89- ``` ts twoslash
90- /** Creates a map centered at loc/lat */
89+ ``` ts
90+ /** loc/lat ์ขํ์ ์ง๋๋ฅผ ์์ฑํฉ๋๋ค. */
9191declare function setupMap(config : MapConfig ): void ;
92- // ---cut ---
92+ // ---์๋ต ---
9393interface MapConfig {
9494 lng: number ;
9595 lat: number ;
0 commit comments