Skip to content

Commit 70c5b47

Browse files
authored
Union types: first commit (#144)
* Union types: first commit * union types: second commit * union types: third commit * union types: fourth commit
1 parent ecb4e62 commit 70c5b47

File tree

1 file changed

+58
-88
lines changed

1 file changed

+58
-88
lines changed

โ€Žpages/unions-and-intersections.md

Lines changed: 58 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,21 @@ permalink: /docs/handbook/unions-and-intersections.html
55
oneline: 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
*/
2424
function 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
4141
declare function padLeft(value: string, padding: any): string;
42-
// ---cut---
43-
// passes at compile time, fails at runtime.
42+
// ---์ƒ๋žต---
43+
// ์ปดํŒŒ์ผ ํƒ€์ž„์—๋Š” ํ†ต๊ณผํ•˜์ง€๋งŒ, ๋Ÿฐํƒ€์ž„์—๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
4444
let 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
*/
6262
function padLeft(value: string, padding: string | number) {
6363
// ...
@@ -66,14 +66,14 @@ function padLeft(value: string, padding: string | number) {
6666
let 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

7979
interface Bird {
@@ -91,19 +91,19 @@ declare function getSmallPet(): Fish | Bird;
9191
let pet = getSmallPet();
9292
pet.layEggs();
9393

94-
// Only available in one of the two possible types
94+
// ๋‘ ๊ฐœ์˜ ์ž ์žฌ์ ์ธ ํƒ€์ž… ์ค‘ ํ•˜๋‚˜์—์„œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
9595
pet.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
109109
type 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+
// ๊ทธ๊ฒƒ์ด ๋ฌด์—‡์— ํ•ด๋‹นํ•˜๋Š”์ง€ ์•„์ง ํ™•์‹คํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
129129
type 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
210180
type NetworkLoadingState = {
211181
state: "loading";
@@ -224,28 +194,28 @@ type NetworkSuccessState = {
224194
summary: string;
225195
};
226196
};
227-
// ---cut---
197+
// ---์ƒ๋žต---
228198
type NetworkState =
229199
| NetworkLoadingState
230200
| NetworkFailedState
231201
| NetworkSuccessState;
232202

233203
function 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

264234
For 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
267237
interface ErrorHandling {
268238
success: boolean;
269239
error?: { message: string };
@@ -297,7 +267,7 @@ const handleArtistsResponse = (response: ArtistsResponse) => {
297267

298268
Intersections are used to implement the [mixin pattern](/docs/handbook/mixins.html):
299269

300-
```ts twoslash
270+
```ts
301271
class Person {
302272
constructor(public name: string) {}
303273
}
@@ -333,4 +303,4 @@ function extend<First extends {}, Second extends {}>(
333303

334304
const jim = extend(new Person("Jim"), ConsoleLogger.prototype);
335305
jim.log(jim.name);
336-
```
306+
```-

0 commit comments

Comments
ย (0)