@@ -5,21 +5,21 @@ permalink: /docs/handbook/unions-and-intersections.html
55oneline : How to use unions and intersection types in TypeScript
66---
77
8- So far, the handbook has covered types which are atomic objects .
9- However, as you model more types you find yourself looking for tools which let you compose or combine existing types instead of creating them from scratch .
8+ ์ง๊ธ๊น์ง, ํธ๋๋ถ์ ์์ ๊ฐ์ฒด์ ํ์
๋ค์ ๋ค๋ค์์ต๋๋ค .
9+ ํ์ง๋ง, ๋ ๋ง์ ํ์
์ ๋ชจ๋ธ๋งํ ์๋ก ์ฒ์๋ถํฐ ํ์
์ ๋ง๋ค์ด๋ด๊ธฐ๋ณด๋ค๋ ์ด๋ฏธ ์กด์ฌํ๋ ํ์
์ ๊ตฌ์ฑํ๊ฑฐ๋ ๊ฒฐํฉํ๋ ๋ฐฉ๋ฒ๋ค์ ์ฐพ๊ฒ ๋ ๊ฒ์
๋๋ค .
1010
11- Intersection and Union types are one of the ways in which you can compose types .
11+ ๊ต์ฐจ ํ์
๊ณผ ์ ๋์ธ ํ์
์ ํ์
์ ๊ตฌ์ฑํ ์ ์๋ ๋ฐฉ๋ฒ ์ค ํ๋์
๋๋ค .
1212
13- # Union Types
13+ # ์ ๋์ธ ํ์
( Union Types)
1414
15- Occasionally, you'll run into a library that expects a parameter to be either a ` number ` or a ` string ` .
16- For instance, take the following function :
15+ ๊ฐ๋, ` number ` ๋ ` string ` ์ ๋งค๊ฐ๋ณ์๋ก ๊ธฐ๋ํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ ๋๊ฐ ์์ต๋๋ค .
16+ ์๋ฅผ ๋ค์ด, ๋ค์ ํจ์๋ฅผ ์ฌ์ฉํ ๋์
๋๋ค :
1717
18- ``` ts twoslash
18+ ``` ts
1919/**
20- * Takes a string and adds "padding" to the left .
21- * If 'padding' is a string, then 'padding' is appended to the left side .
22- * If 'padding' is a number, then that number of spaces is added to the left side .
20+ * ๋ฌธ์์ด์ ๋ฐ๊ณ ์ผ์ชฝ์ "padding"์ ์ถ๊ฐํฉ๋๋ค .
21+ * ๋ง์ฝ 'padding'์ด ๋ฌธ์์ด์ด๋ผ๋ฉด, 'padding'์ ์ผ์ชฝ์ ๋ํด์ง ๊ฒ์
๋๋ค .
22+ * ๋ง์ฝ 'padding'์ด ์ซ์๋ผ๋ฉด, ๊ทธ ์ซ์๋งํผ์ ๊ณต๋ฐฑ์ด ์ผ์ชฝ์ ๋ํด์ง ๊ฒ์
๋๋ค .
2323 */
2424function padLeft(value : string , padding : any ) {
2525 if (typeof padding === " number" ) {
@@ -31,33 +31,33 @@ function padLeft(value: string, padding: any) {
3131 throw new Error (` Expected string or number, got '${padding }'. ` );
3232}
3333
34- padLeft (" Hello world" , 4 ); // returns " Hello world"
34+ padLeft (" Hello world" , 4 ); // " Hello world"๋ฅผ ๋ฐํํฉ๋๋ค.
3535```
3636
37- The problem with ` padLeft ` in the above example is that its ` padding ` parameter is typed as ` any ` .
38- That means that we can call it with an argument that's neither a ` number ` nor a ` string ` , but TypeScript will be okay with it .
37+ ์ ์์ ์์ ` padLeft ` ์ ๋ฌธ์ ๋ ๋งค๊ฐ๋ณ์ ` padding ` ์ด ` any ` ํ์
์ผ๋ก ๋์ด์๋ค๋ ๊ฒ์
๋๋ค .
38+ ์ฆ, ` number ` ๋ ` string ` ๋ ๋ค ์๋ ์ธ์๋ก ํจ์๋ฅผ ํธ์ถํ ์ ์๋ค๋ ๊ฒ์ด๊ณ , TypeScript๋ ์ด๋ฅผ ๊ด์ฐฎ๋ค๊ณ ๋ฐ์๋ค์ผ ๊ฒ์
๋๋ค .
3939
40- ``` ts twoslash
40+ ``` ts
4141declare function padLeft(value : string , padding : any ): string ;
42- // ---cut ---
43- // passes at compile time, fails at runtime .
42+ // ---์๋ต ---
43+ // ์ปดํ์ผ ํ์์๋ ํต๊ณผํ์ง๋ง, ๋ฐํ์์๋ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค .
4444let indentedString = padLeft (" Hello world" , true );
4545```
4646
47- In traditional object-oriented code, we might abstract over the two types by creating a hierarchy of types .
48- While this is much more explicit, it's also a little bit overkill .
49- One of the nice things about the original version of ` padLeft ` was that we were able to just pass in primitives .
50- That meant that usage was simple and concise .
51- This new approach also wouldn't help if we were just trying to use a function that already exists elsewhere .
47+ ์ ํต์ ์ธ ๊ฐ์ฒด์งํฅ ์ฝ๋์์, ํ์
์ ๊ณ์ธต์ ์์ฑํ์ฌ ๋ ํ์
์ ์ถ์ํํ ์ ์์ต๋๋ค .
48+ ์ด๋ ๋ ๋ช
์์ ์ผ ์๋ ์์ง๋ง, ์กฐ๊ธ ๊ณผํ๋ค๊ณ ํ ์๋ ์์ต๋๋ค .
49+ ๊ธฐ์กด ๋ฐฉ๋ฒ์ ` padLeft ` ์์ ์ข์ ์ ์ค ํ๋๋ ์์ ๊ฐ์ ๋จ์ง ์ ๋ฌํ ์ ์๋ค๋ ๊ฒ์
๋๋ค .
50+ ์ฆ ์ฌ์ฉ๋ฒ์ด ๊ฐ๋จํ๊ณ ๊ฐ๊ฒฐํฉ๋๋ค .
51+ ๋ํ ์ด ์๋ก์ด ๋ฐฉ๋ฒ์ ๋จ์ง ๋ค๋ฅธ ๊ณณ์ ์ด๋ฏธ ์กด์ฌํ๋ ํจ์๋ฅผ ์ฌ์ฉํ๊ณ ์ ํ ๋ ๋์์ด ๋์ง ์์ต๋๋ค .
5252
53- Instead of ` any ` , we can use a _ union type _ for the ` padding ` parameter :
53+ ` any ` ๋์ ์, _ ์ ๋์ธ ํ์
_์ ๋งค๊ฐ๋ณ์ ` padding ` ์ ์ฌ์ฉํ ์ ์์ต๋๋ค :
5454
55- ``` ts twoslash
55+ ``` ts
5656// @errors: 2345
5757/**
58- * Takes a string and adds "padding" to the left .
59- * If 'padding' is a string, then 'padding' is appended to the left side .
60- * If 'padding' is a number, then that number of spaces is added to the left side .
58+ * ๋ฌธ์์ด์ ๋ฐ๊ณ ์ผ์ชฝ์ "padding"์ ์ถ๊ฐํฉ๋๋ค .
59+ * ๋ง์ฝ 'padding'์ด ๋ฌธ์์ด์ด๋ผ๋ฉด, 'padding'์ ์ผ์ชฝ์ ๋ํด์ง ๊ฒ์
๋๋ค .
60+ * ๋ง์ฝ 'padding'์ด ์ซ์๋ผ๋ฉด, ๊ทธ ์ซ์๋งํผ์ ๊ณต๋ฐฑ์ด ์ผ์ชฝ์ ๋ํด์ง ๊ฒ์
๋๋ค .
6161 */
6262function padLeft(value : string , padding : string | number ) {
6363 // ...
@@ -66,14 +66,14 @@ function padLeft(value: string, padding: string | number) {
6666let indentedString = padLeft (" Hello world" , true );
6767```
6868
69- A union type describes a value that can be one of several types .
70- We use the vertical bar (` | ` ) to separate each type, so ` number | string | boolean ` is the type of a value that can be a ` number ` , a ` string ` , or a ` boolean ` .
69+ ์ ๋์ธ ํ์
์ ์ฌ๋ฌ ํ์
์ค ํ๋๊ฐ ๋ ์ ์๋ ๊ฐ์ ์๋ฏธํฉ๋๋ค .
70+ ์ธ๋ก ๋ง๋ (` | ` )๋ก ๊ฐ ํ์
์ ๊ตฌ๋ถํ์ฌ, ` number | string | boolean ` ์ ๊ฐ์ ํ์
์ด ` number ` , ` string ` ํน์ ` boolean ` ์ด ๋ ์ ์์์ ์๋ฏธํฉ๋๋ค .
7171
72- ## Unions with Common Fields
72+ ## ๊ณตํต ํ๋๋ฅผ ๊ฐ๋ ์ ๋์ธ ( Unions with Common Fields)
7373
74- If we have a value that is a union type, we can only access members that are common to all types in the union .
74+ ์ ๋์ธ ํ์
์ธ ๊ฐ์ด ์์ผ๋ฉด, ์ ๋์ธ์ ์๋ ๋ชจ๋ ํ์
์ ๊ณตํต์ธ ๋ฉค๋ฒ๋ค์๋ง ์ ๊ทผํ ์ ์์ต๋๋ค .
7575
76- ``` ts twoslash
76+ ``` ts
7777// @errors: 2339
7878
7979interface Bird {
@@ -91,19 +91,19 @@ declare function getSmallPet(): Fish | Bird;
9191let pet = getSmallPet ();
9292pet .layEggs ();
9393
94- // Only available in one of the two possible types
94+ // ๋ ๊ฐ์ ์ ์ฌ์ ์ธ ํ์
์ค ํ๋์์๋ง ์ฌ์ฉํ ์ ์์ต๋๋ค.
9595pet .swim ();
9696```
9797
98- Union types can be a bit tricky here, but it just takes a bit of intuition to get used to .
99- If a value has the type ` A | B ` , we only know for _ certain _ that it has members that both ` A ` _ and _ ` B ` have .
100- In this example , ` Bird ` has a member named ` fly ` .
101- We can't be sure whether a variable typed as ` Bird | Fish ` has a ` fly ` method .
102- If the variable is really a ` Fish ` at runtime, then calling ` pet.fly() ` will fail .
98+ ์ฌ๊ธฐ์ ์ ๋์ธ ํ์
์ ์ฝ๊ฐ ๊น๋ค๋ก์ธ ์ ์์ผ๋, ์ต์ํด์ง๋ ๋ฐ ์ฝ๊ฐ์ ์ง๊ด๋ง ์์ผ๋ฉด ๋ฉ๋๋ค .
99+ ๋ง์ฝ ๊ฐ์ด ` A | B ` ํ์
์ ๊ฐ๊ณ ์์ผ๋ฉด, _ ํ์ ํ _ ์ ์๋ ๊ฒ์ ๊ทธ ๊ฐ์ด ` A ` _ ์ _ ` B ` ๋ ๋ค ๊ฐ์ง๊ณ ์๋ ๋ฉค๋ฒ๋ค์ ๊ฐ๊ณ ์๋ค๋ ๊ฒ๋ฟ์
๋๋ค .
100+ ์ด ์์ ์์ , ` Bird ` ๋ ` fly ` ๋ผ๋ ๋ฉค๋ฒ๋ฅผ ๊ฐ๊ณ ์์ต๋๋ค .
101+ ` Bird | Fish ` ๋ก ํ์
์ด ์ง์ ๋ ๋ณ์๊ฐ ` fly ` ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์๋์ง ํ์ ํ ์ ์์ต๋๋ค .
102+ ๋ง์ฝ ๋ณ์๊ฐ ์ค์ ๋ก ๋ฐํ์์ ` Fish ` ์ด๋ฉด, ` pet.fly() ` ๋ฅผ ํธ์ถํ๋ ๊ฒ์ ์ค๋ฅ์
๋๋ค .
103103
104- ## Discriminating Unions
104+ ## ์ ๋์ธ ๊ตฌ๋ณํ๊ธฐ ( Discriminating Unions)
105105
106- A common technique for working with unions is to have a single field which uses literal types which you can use to let TypeScript narrow down the possible current type. For example, we're going to create a union of three types which have a single shared field .
106+ ์ ๋์ธ์ ์ฌ์ฉํ๋ ๋ฐ ์์ด์ ์ผ๋ฐ์ ์ธ ๊ธฐ์ ์ TypeScript๊ฐ ํ์ฌ ๊ฐ๋ฅํ ํ์
์ถ๋ก ์ ๋ฒ์๋ฅผ ์ขํ๋๊ฐ๊ฒ ํด์ค ์ ์๋ ๋ฆฌํฐ๋ด ํ์
์ ๊ฐ๋ ๋จ์ผ ํ๋๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค. ์๋ฅผ ๋ค์ด, ํ๋์ ๊ณตํต ํ๋๋ฅผ ๊ฐ์ง๊ณ ์๋ ์ธ ๊ฐ์ง ํ์
์ ์ ๋์ธ์ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค .
107107
108108``` ts
109109type NetworkLoadingState = {
@@ -124,45 +124,15 @@ type NetworkSuccessState = {
124124 };
125125};
126126
127- // Create a type which represents only one of the above types
128- // but you aren't sure which it is yet .
127+ // ์ ํ์
๋ค ์ค ๋จ ํ๋๋ฅผ ๋ํํ๋ ํ์
์ ๋ง๋ค์์ง๋ง,
128+ // ๊ทธ๊ฒ์ด ๋ฌด์์ ํด๋นํ๋์ง ์์ง ํ์คํ์ง ์์ต๋๋ค .
129129type NetworkState =
130130 | NetworkLoadingState
131131 | NetworkFailedState
132132 | NetworkSuccessState ;
133133```
134134
135- <style type =" text/css " >
136- .markdown table .tg {
137- border-collapse :collapse ;
138- width : 100% ;
139- text-align : center ;
140- display : table ;
141- }
142-
143- .tg th {
144- border-bottom : 1px solid black ;
145- padding : 8px ;
146- padding-bottom : 0 ;
147- }
148-
149- .tg tbody , .tg tr {
150- width : 100% ;
151- }
152-
153- .tg .highlight {
154- background-color : #F3F3F3 ;
155- }
156-
157- @media (prefers-color-scheme: dark) {
158- .tg .highlight {
159- background-color : #424242 ;
160- }
161- }
162-
163- </style >
164-
165- All of the above types have a field named ` state ` , and then they also have their own fields:
135+ ์ ํ์
๋ค์ ๋ชจ๋ ` state ` ๋ผ๋ ํ๋๋ฅผ ๊ฐ๊ณ ์์ผ๋ฉฐ, ๊ทธ๋ค ๊ฐ์๋ง์ ํ๋๋ ๊ฐ๊ณ ์์ต๋๋ค:
166136
167137<table class =' tg ' width =" 100% " >
168138 <tbody >
@@ -184,9 +154,9 @@ All of the above types have a field named `state`, and then they also have their
184154 </tbody>
185155</table >
186156
187- Given the ` state ` field is common in every type inside ` NetworkState ` - it is safe for your code to access without an existence check .
157+ ` state ` ํ๋๊ฐ ` NetworkState ` ์์ ๋ชจ๋ ํ์
์ ๊ณตํต์ผ๋ก ์กด์ฌํ๋ค๋ ์ ์ ์๋ค๋ฉด - ์กด์ฌ ์ฌ๋ถ๋ฅผ ์ฒดํฌํ์ง ์๊ณ ๋ ์ ๊ทผํ ์ ์์ต๋๋ค .
188158
189- With ` state ` as a literal type, you can compare the value of ` state ` to the equivalent string and TypeScript will know which type is currently being used .
159+ ๋ฆฌํฐ๋ด ํ์
์ผ๋ก์ ` state ` ๋ฅผ ๊ฐ๊ณ ์๋ค๋ฉด, ` state ` ์ ๊ฐ์ ๋์ํ๋ ๋์ผํ ๋ฌธ์์ด๊ณผ ๋์กฐ๋๊ณ TypeScript๋ ํ์ฌ ์ด๋ค ํ์
์ด ์ฌ์ฉ๋๊ณ ์๋์ง ์ ๊ฒ์
๋๋ค .
190160
191161<table class =' tg ' width =" 100% " >
192162 <tbody >
@@ -203,9 +173,9 @@ With `state` as a literal type, you can compare the value of `state` to the equi
203173 </tbody>
204174</table >
205175
206- In this case, you can use a ` switch ` statement to narrow down which type is represented at runtime:
176+ ์ด ๊ฒฝ์ฐ, ๋ฐํ์์ ๋ํ๋๋ ํ์
์ ๋ฒ์๋ฅผ ์ขํ๊ธฐ ์ํ์ฌ ` switch ` ๋ฌธ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
207177
208- ``` ts twoslash
178+ ``` ts
209179// @errors: 2339
210180type NetworkLoadingState = {
211181 state: " loading" ;
@@ -224,28 +194,28 @@ type NetworkSuccessState = {
224194 summary: string ;
225195 };
226196};
227- // ---cut ---
197+ // ---์๋ต ---
228198type NetworkState =
229199 | NetworkLoadingState
230200 | NetworkFailedState
231201 | NetworkSuccessState ;
232202
233203function networkStatus(state : NetworkState ): string {
234- // Right now TypeScript does not know which of the three
235- // potential types state could be .
204+ // ํ์ฌ TypeScript๋ ์
์ค ์ด๋ค ๊ฒ์ด
205+ // state๊ฐ ๋ ์ ์๋ ์ ์ฌ์ ์ธ ํ์
์ธ์ง ์ ์ ์์ต๋๋ค .
236206
237- // Trying to access a property which isn't shared
238- // across all types will raise an error
207+ // ๋ชจ๋ ํ์
์ ๊ณต์ ๋์ง ์๋ ํ๋กํผํฐ์ ์ ๊ทผํ๋ ค๋ ์๋๋
208+ // ์ค๋ฅ๋ฅผ ๋ฐ์์ํต๋๋ค.
239209 state .code ;
240210
241- // By switching on state, TypeScript can narrow the union
242- // down in code flow analysis
211+ // state์ swtich๋ฌธ์ ์ฌ์ฉํ์ฌ, TypeScript๋ ์ฝ๋ ํ๋ฆ์ ๋ถ์ํ๋ฉด์
212+ // ์ ๋์ธ ํ์
์ ์ขํ๋๊ฐ ์ ์์ต๋๋ค.
243213 switch (state .state ) {
244214 case " loading" :
245215 return " Downloading..." ;
246216 case " failed" :
247- // The type must be NetworkFailedState here ,
248- // so accessing the `code` field is safe
217+ // ์ฌ๊ธฐ์ ํ์
์ NetworkFailedState์ผ ๊ฒ์ด๋ฉฐ ,
218+ // ๋ฐ๋ผ์ `code` ํ๋์ ์ ๊ทผํ ์ ์์ต๋๋ค.
249219 return ` Error ${state .code } downloading ` ;
250220 case " success" :
251221 return ` Downloaded ${state .response .title } - ${state .response .summary } ` ;
@@ -263,7 +233,7 @@ That means an object of this type will have all members of all three types.
263233
264234For example, if you had networking requests with consistent error handling then you could separate out the error handling into it's own type which is merged with types which correspond to a single response type.
265235
266- ``` ts twoslash
236+ ``` ts
267237interface ErrorHandling {
268238 success: boolean ;
269239 error? : { message: string };
@@ -297,7 +267,7 @@ const handleArtistsResponse = (response: ArtistsResponse) => {
297267
298268Intersections are used to implement the [ mixin pattern] ( /docs/handbook/mixins.html ) :
299269
300- ``` ts twoslash
270+ ``` ts
301271class Person {
302272 constructor (public name : string ) {}
303273}
@@ -333,4 +303,4 @@ function extend<First extends {}, Second extends {}>(
333303
334304const jim = extend (new Person (" Jim" ), ConsoleLogger .prototype );
335305jim .log (jim .name );
336- ```
306+ ` ` ` -
0 commit comments