Skip to content

Commit af93ec8

Browse files
committed
translate for ko
1 parent 4db337c commit af93ec8

File tree

1 file changed

+45
-46
lines changed

1 file changed

+45
-46
lines changed

docs/documentation/ko/handbook-v2/Type Manipulation/Conditional Types.md

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
---
22
title: Conditional Types
33
layout: docs
4-
permalink: /docs/handbook/2/conditional-types.html
5-
oneline: "Create types which act like if statements in the type system."
4+
permalink: /ko/docs/handbook/2/conditional-types.html
5+
oneline: "타입 시스템에서 if문 처럼 동작하는 타입 생성하기."
66
---
77

8-
At the heart of most useful programs, we have to make decisions based on input.
9-
JavaScript programs are no different, but given the fact that values can be easily introspected, those decisions are also based on the types of the inputs.
10-
_Conditional types_ help describe the relation between the types of inputs and outputs.
8+
대부분 유용한 프로그램의 핵심은, 입력에 따라 결정할 수 있어야 합니다.
9+
JavaScript 프로그램도 비슷하게 값들이 쉽게 검토될 수 있다는 사실을 고려할 때, 이러한 결정들 또한 입력에 한 종류로 볼 수 있습니다.
10+
_조건부 타입_ 은 입력과 출력 타입간의 관계를 설명하는 데 도움을 줄 수 있습니다.
1111

1212
```ts twoslash
1313
interface Animal {
@@ -24,7 +24,7 @@ type Example2 = RegExp extends Animal ? number : string;
2424
// ^?
2525
```
2626

27-
Conditional types take a form that looks a little like conditional expressions (`condition ? trueExpression : falseExpression`) in JavaScript:
27+
조건부 타입은 JavaScript에 있는 삼항 연산자 조건문 (`condition ? trueExpression : falseExpression`) 같은 형태를 가집니다.
2828

2929
```ts twoslash
3030
type SomeType = any;
@@ -36,12 +36,12 @@ type Stuff =
3636
SomeType extends OtherType ? TrueType : FalseType;
3737
```
3838

39-
When the type on the left of the `extends` is assignable to the one on the right, then you'll get the type in the first branch (the "true" branch); otherwise you'll get the type in the latter branch (the "false" branch).
39+
`extends`를 기준으로 왼쪽에 있는 타입이 오른쪽 타입에 할당할 수 있다면 첫 번째 분기(참값 분기)를, 그렇지 않다면 뒤의 분기(거짓값 분기)를 얻게 됩니다.
4040

41-
From the examples above, conditional types might not immediately seem useful - we can tell ourselves whether or not `Dog extends Animal` and pick `number` or `string`!
42-
But the power of conditional types comes from using them with generics.
41+
`Dog extends Animal` 에 따라 `number``string`인지 알려주는 것 말곤, 위의 예제에서 조건부 타입은 그다지 유용해 보이지 않습니다!
42+
하지만 제네릭과 함께 사용될 때 조건부 타입은 강력한 힘을 갖습니다.
4343

44-
For example, let's take the following `createLabel` function:
44+
예를 들어, 다음 `createLabel` 함수를 살펴보겠습니다.
4545

4646
```ts twoslash
4747
interface IdLabel {
@@ -59,12 +59,12 @@ function createLabel(nameOrId: string | number): IdLabel | NameLabel {
5959
}
6060
```
6161

62-
These overloads for createLabel describe a single JavaScript function that makes a choice based on the types of its inputs. Note a few things:
62+
createLabel의 오버로드들은 입력 타입에 따른 단일 JavaScript 함수를 나타냅니다. 다음을 주목하세요.
6363

64-
1. If a library has to make the same sort of choice over and over throughout its API, this becomes cumbersome.
65-
2. We have to create three overloads: one for each case when we're _sure_ of the type (one for `string` and one for `number`), and one for the most general case (taking a `string | number`). For every new type `createLabel` can handle, the number of overloads grows exponentially.
64+
1. 만약 라이브러리가 매번 API 전체에서 비슷한 종류의 함수를 만들어야 한다면 번거로워집니다.
65+
2. 우린 3가지 오버로드 즉, 각 케이스별로 _확실한_ 타입을 가지거나 (각각 `number``string`) 그리고 일반적인 케이스(`string | number`) 가져야 합니다. `createLabel`의 새로운 타입을 다루기 위해선 오버로드의 수는 기하급수적으로 증가합니다.
6666

67-
Instead, we can encode that logic in a conditional type:
67+
대신에 조건부 타입으로 로직을 인코딩할 수 있습니다.
6868

6969
```ts twoslash
7070
interface IdLabel {
@@ -79,7 +79,7 @@ type NameOrId<T extends number | string> = T extends number
7979
: NameLabel;
8080
```
8181

82-
We can then use that conditional type to simplify our overloads down to a single function with no overloads.
82+
조건부 타입을 사용하면 단일 함수까지 오버로드 없이 단순화 시킬 수 있습니다.
8383

8484
```ts twoslash
8585
interface IdLabel {
@@ -106,20 +106,20 @@ let c = createLabel(Math.random() ? "hello" : 42);
106106
// ^?
107107
```
108108

109-
### Conditional Type Constraints
109+
### 조건부 타입으로 제한하기
110110

111-
Often, the checks in a conditional type will provide us with some new information.
112-
Just like with narrowing with type guards can give us a more specific type, the true branch of a conditional type will further constrain generics by the type we check against.
111+
종종, 조건부 타입의 검사에서 새로운 정보를 얻을 수 있습니다.
112+
타입 가드가 더 구체적인 타입으로 좁혀주듯이, 조건부 타입의 참값 분기는 대조하는 타입에 따라서 제네릭을 더 제한할 수 있습니다.
113113

114-
For example, let's take the following:
114+
다음 예를 살펴보겠습니다.
115115

116116
```ts twoslash
117117
// @errors: 2536
118118
type MessageOf<T> = T["message"];
119119
```
120120

121-
In this example, TypeScript errors because `T` isn't known to have a property called `message`.
122-
We could constrain `T`, and TypeScript would no longer complain:
121+
위 에제에서, `T``message` 프로퍼티를 가지고 있는지 알 수 없기 때문에 TypeScript에서 오류가 발생합니다.
122+
`T`의 타입을 제한해서 TypeScript가 더이상 오류를 내지 않도록 만들 수 있습니다.
123123

124124
```ts twoslash
125125
type MessageOf<T extends { message: unknown }> = T["message"];
@@ -132,8 +132,8 @@ type EmailMessageContents = MessageOf<Email>;
132132
// ^?
133133
```
134134

135-
However, what if we wanted `MessageOf` to take any type, and default to something like `never` if a `message` property isn't available?
136-
We can do this by moving the constraint out and introducing a conditional type:
135+
하지만 `MessageOf` 가 아무 타입이나 받을 수 있고, `message` 프로퍼티가 없으면 `never` 타입으로 결정하도록 만들 수 있을까요?
136+
여기서 제약 조건을 외부로 옮기고, 조건부 타입을 적용하면 가능합니다.
137137

138138
```ts twoslash
139139
type MessageOf<T> = T extends { message: unknown } ? T["message"] : never;
@@ -153,9 +153,9 @@ type DogMessageContents = MessageOf<Dog>;
153153
// ^?
154154
```
155155

156-
Within the true branch, TypeScript knows that `T` _will_ have a `message` property.
156+
참값 분기내에서는 TypeScript는 `T``message` 프로퍼티를 가지고 _있을 것을_ 알 수 있습니다.
157157

158-
As another example, we could also write a type called `Flatten` that flattens array types to their element types, but leaves them alone otherwise:
158+
또 다른 예제에서 배열 타입이면 요소 타입으로 평평하게 맞추지만, 배열 타입이 아니면 그대로 유지하는 `Flatten` 타입을 만들 수 있습니다.
159159

160160
```ts twoslash
161161
type Flatten<T> = T extends any[] ? T[number] : T;
@@ -169,26 +169,26 @@ type Num = Flatten<number>;
169169
// ^?
170170
```
171171

172-
When `Flatten` is given an array type, it uses an indexed access with `number` to fetch out `string[]`'s element type.
173-
Otherwise, it just returns the type it was given.
172+
`Flatten`에 배열 타입이 주어지면, `number`를 사용한 인덱스 접근을 통해 `string[]`의 요소 타입을 가져올 수 있습니다.
173+
그렇지 않으면, 주어진 타입을 반환합니다.
174174

175-
### Inferring Within Conditional Types
175+
### 조건부 타입 내에서 추론하기
176176

177-
We just found ourselves using conditional types to apply constraints and then extract out types.
178-
This ends up being such a common operation that conditional types make it easier.
177+
위에서 제약 조건을 가진 조건부 타입을 이용해서 타입을 추출할 수 있다는 점을 살펴봤습니다.
178+
이 부분은 조건부 타입을 더 쉽게 만드는 평범한 작업이 됩니다.
179179

180-
Conditional types provide us with a way to infer from types we compare against in the true branch using the `infer` keyword.
181-
For example, we could have inferred the element type in `Flatten` instead of fetching it out "manually" with an indexed access type:
180+
조건부 타입은 `infer` 키워드를 사용해서 참값 분기에서 비교하는 타입을 추론할 수 있습니다.
181+
예를 들어, `Flatten`에서 인덱싱된 접근 타입으로 "직접" 추출하지 않고 요소 타입을 추론할 수 있습니다.
182182

183183
```ts twoslash
184184
type Flatten<Type> = Type extends Array<infer Item> ? Item : Type;
185185
```
186186

187-
Here, we used the `infer` keyword to declaratively introduce a new generic type variable named `Item` instead of specifying how to retrieve the element type of `T` within the true branch.
188-
This frees us from having to think about how to dig through and probing apart the structure of the types we're interested in.
187+
여기서 참값 분기에서 `T`의 요소 타입을 어떻게 제시할 필요 없이, `infer` 키워드를 새 제네릭 타입 변수 `Item`에 선언적으로 사용했습니다.
188+
이 방식은 관심 있는 타입의 구조를 깊게 분석하지 않아도 되도록 만들어줍니다.
189189

190-
We can write some useful helper type aliases using the `infer` keyword.
191-
For example, for simple cases, we can extract the return type out from function types:
190+
`infer` 키워드를 사용해서 유용한 헬퍼 타입 별칭을 사용할 수 있습니다.
191+
예를 들어 함수 타입에서 리턴 타입을 추출하는 간단한 케이스를 살펴보겠습니다.
192192

193193
```ts twoslash
194194
type GetReturnType<Type> = Type extends (...args: never[]) => infer Return
@@ -205,7 +205,7 @@ type Bools = GetReturnType<(a: boolean, b: boolean) => boolean[]>;
205205
// ^?
206206
```
207207

208-
When inferring from a type with multiple call signatures (such as the type of an overloaded function), inferences are made from the _last_ signature (which, presumably, is the most permissive catch-all case). It is not possible to perform overload resolution based on a list of argument types.
208+
여러 호출 시그니처 (오버로트 함수 타입 같이)를 가진 타입을 추론할 때, _마지막_ 시그니처 (아마, 모든 케이스에 허용되는)로 추론하게 됩니다. 인자 타입의 목록에 기반해서 오버로드들을 처리할 수는 없습니다.
209209

210210
```ts twoslash
211211
declare function stringOrNum(x: string): number;
@@ -216,16 +216,16 @@ type T1 = ReturnType<typeof stringOrNum>;
216216
// ^?
217217
```
218218

219-
## Distributive Conditional Types
219+
## 분산적인 조건부 타입
220220

221-
When conditional types act on a generic type, they become _distributive_ when given a union type.
222-
For example, take the following:
221+
제네릭 타입 위에서 조건부 타입은 유니언 타입을 만나면 _분산적으로_ 동작합니다.
222+
예를 들어 다음을 보겠습니다.
223223

224224
```ts twoslash
225225
type ToArray<Type> = Type extends any ? Type[] : never;
226226
```
227227

228-
If we plug a union type into `ToArray`, then the conditional type will be applied to each member of that union.
228+
`ToArray`에 유니언 타입을 넘기면 조건부 타입은 유니언의 각 멤버에 적용됩니다.
229229

230230
```ts twoslash
231231
type ToArray<Type> = Type extends any ? Type[] : never;
@@ -234,15 +234,15 @@ type StrArrOrNumArr = ToArray<string | number>;
234234
// ^?
235235
```
236236

237-
What happens here is that `StrArrOrNumArr ` distributes on:
237+
`StrArrOrNumArr`이 동작하는 방식은 다음과 같습니다.
238238

239239
```ts twoslash
240240
type StrArrOrNumArr =
241241
// ---cut---
242242
string | number;
243243
```
244244

245-
and maps over each member type of the union, to what is effectively:
245+
유니언의 각 멤버 타입은 효율적으로 매핑됩니다.
246246

247247
```ts twoslash
248248
type ToArray<Type> = Type extends any ? Type[] : never;
@@ -251,16 +251,15 @@ type StrArrOrNumArr =
251251
ToArray<string> | ToArray<number>;
252252
```
253253

254-
which leaves us with:
254+
그리고 다음과 같이 결과가 나옵니다.
255255

256256
```ts twoslash
257257
type StrArrOrNumArr =
258258
// ---cut---
259259
string[] | number[];
260260
```
261261

262-
Typically, distributivity is the desired behavior.
263-
To avoid that behavior, you can surround each side of the `extends` keyword with square brackets.
262+
일반적으로 분산성이 원하는 동작입니다. 이러한 동작을 방지하려면 `extends`키워드의 양 옆을 대괄호로 감싸면 됩니다.
264263

265264
```ts twoslash
266265
type ToArrayNonDist<Type> = [Type] extends [any] ? Type[] : never;

0 commit comments

Comments
 (0)