Skip to content

Commit 77f21eb

Browse files
annkimmκΉ€λ―Όμ§€dvlprsh
authored
docs: to Discriminated Unions from Contextual typing #114 (#160)
* docs: to Discriminated Unions from Contextual typing * docs: fix text * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <dvlprsh103@gmail.com> Co-authored-by: κΉ€λ―Όμ§€ <ll@llui-MacBookPro.local> Co-authored-by: Seohee Park <dvlprsh103@gmail.com>
1 parent 6f7e492 commit 77f21eb

File tree

1 file changed

+59
-59
lines changed

1 file changed

+59
-59
lines changed

β€Žpages/tutorials/ts-for-functional-programmers.md

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -293,91 +293,91 @@ let s: "left" | "right" = "right";
293293
pad("hi", 10, s);
294294
```
295295

296-
# Concepts similar to Haskell
296+
# Haskellκ³Ό λΉ„μŠ·ν•œ κ°œλ… (Concepts similar to Haskell)
297297

298-
## Contextual typing
298+
## λ¬Έλ§₯적인 타이핑 (Contextual typing)
299299

300-
TypeScript has some obvious places where it can infer types, like
301-
variable declarations:
300+
TypeScriptλŠ” λ³€μˆ˜ μ„ μ–Έκ³Ό 같이 νƒ€μž…μ„ μΆ”λ‘ ν•  수 μžˆλŠ”
301+
λͺ‡ κ°€μ§€ λΆ„λͺ…ν•œ 방법이 μžˆμŠ΅λ‹ˆλ‹€:
302302

303-
```ts twoslash
303+
```ts
304304
let s = "I'm a string!";
305305
```
306306

307-
But it also infers types in a few other places that you may not expect
308-
if you've worked with other C-syntax languages:
307+
ν•˜μ§€λ§Œ λ‹€λ₯Έ C-계열 μ–Έμ–΄λ‘œ μž‘μ—…ν•œ 적이 μžˆλ‹€λ©΄ μ˜ˆμƒν•˜μ§€ λͺ»ν–ˆλ˜
308+
λ‹€λ₯Έ λ°©λ²•μœΌλ‘œ νƒ€μž… 좔둠이 κ°€λŠ₯ν•©λ‹ˆλ‹€:
309309

310-
```ts twoslash
310+
```ts
311311
declare function map<T, U>(f: (t: T) => U, ts: T[]): U[];
312312
let sns = map((n) => n.toString(), [1, 2, 3]);
313313
```
314314

315-
Here, `n: number` in this example also, despite the fact that `T` and `U`
316-
have not been inferred before the call. In fact, after `[1,2,3]` has
317-
been used to infer `T=number`, the return type of `n => n.toString()`
318-
is used to infer `U=string`, causing `sns` to have the type
319-
`string[]`.
315+
μ—¬κΈ°μ—μ„œ, 이 μ˜ˆμ‹œμ˜ `n: number`μ—μ„œ λ˜ν•œ, `T` κ³Ό `U`λŠ” 호좜 전에
316+
μΆ”λ‘ λ˜μ§€ μ•Šμ•˜μŒμ—λ„ λΆˆκ΅¬ν•˜κ³ .
317+
μ‹€μ œλ‘œ `[1,2,3]` 으둜 `T=number`을 μΆ”λ‘ ν•œ λ‹€μŒμ—,
318+
`n => n.toString()`의 리턴 νƒ€μž…μœΌλ‘œ `U=string`을 μΆ”λ‘ ν•˜μ—¬,
319+
`sns`κ°€ `string[]` νƒ€μž…μ„ 가지도둝 ν•©λ‹ˆλ‹€.
320320

321-
Note that inference will work in any order, but intellisense will only
322-
work left-to-right, so TypeScript prefers to declare `map` with the
323-
array first:
321+
좔둠은 μ–΄λ–€ μˆœμ„œλ‘œλ“  μž‘λ™ν•˜μ§€λ§Œ, intellisenseλŠ” μ™Όμͺ½μ—μ„œ 였λ₯Έμͺ½μœΌλ‘œλ§Œ
322+
μž‘λ™ν•˜λ―€λ‘œ, TypeScriptλŠ” λ°°μ—΄κ³Ό ν•¨κ»˜ `map`을 λ¨Όμ € μ„ μ–Έν•˜λŠ” 것을
323+
μ„ ν˜Έν•©λ‹ˆλ‹€:
324324

325-
```ts twoslash
325+
```ts
326326
declare function map<T, U>(ts: T[], f: (t: T) => U): U[];
327327
```
328328

329-
Contextual typing also works recursively through object literals, and
330-
on unit types that would otherwise be inferred as `string` or
331-
`number`. And it can infer return types from context:
329+
λ¬Έλ§₯적인 타이핑은 λ˜ν•œ 객체 λ¦¬ν„°λŸ΄μ„ 톡해 μž¬κ·€μ μœΌλ‘œ μž‘λ™ν•˜λ©°, κ·Έλ ‡μ§€ μ•ŠμœΌλ©΄
330+
`string`μ΄λ‚˜ `number`둜 μΆ”λ‘  κ°€λŠ₯ν•œ μœ λ‹› νƒ€μž…μœΌλ‘œ μž‘λ™ν•©λ‹ˆλ‹€.
331+
그리고 λ¬Έλ§₯을 ν†΅ν•΄μ„œ 리턴 νƒ€μž…μ„ μΆ”λ‘ ν•  수 μžˆμŠ΅λ‹ˆλ‹€:
332332

333-
```ts twoslash
333+
```ts
334334
declare function run<T>(thunk: (t: T) => void): T;
335335
let i: { inference: string } = run((o) => {
336336
o.inference = "INSERT STATE HERE";
337337
});
338338
```
339339

340-
The type of `o` is determined to be `{ inference: string }` because
340+
`o` 의 νƒ€μž…μ€ `{ inference: string }` 으둜 κ²°μ •λ˜μ—ˆμŠ΅λ‹ˆλ‹€. μ™œλƒν•˜λ©΄
341341

342-
1. Declaration initialisers are contextually typed by the
343-
declaration's type: `{ inference: string }`.
344-
2. The return type of a call uses the contextual type for inferences,
345-
so the compiler infers that `T={ inference: string }`.
346-
3. Arrow functions use the contextual type to type their parameters,
347-
so the compiler gives `o: { inference: string }`.
342+
1. μ„ μ–Έ μ΄λ‹ˆμ…œλΌμ΄μ €λŠ” μ„ μ–Έ νƒ€μž…: `{ inference: string }`에 λ”°λΌμ„œ
343+
λ¬Έλ§₯적으둜 νƒ€μž…μ΄ μ •ν•΄μ§‘λ‹ˆλ‹€.
344+
2. 호좜의 리턴 νƒ€μž…μ€ 좔둠을 μœ„ν•΄ λ¬Έλ§₯적인 νƒ€μž…μ„ μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ—,
345+
μ»΄νŒŒμΌλŸ¬λŠ” `T={ inference: string }`으둜 μΆ”λ‘ ν•©λ‹ˆλ‹€.
346+
3. ν™”μ‚΄ν‘œ ν•¨μˆ˜λŠ” λ§€κ°œλ³€μˆ˜μ— νƒ€μž…μ„ μ§€μ •ν•˜κΈ° μœ„ν•΄ λ¬Έλ§₯적인 νƒ€μž…μ„ μ‚¬μš©ν•˜λ―€λ‘œ,
347+
μ»΄νŒŒμΌλŸ¬λŠ” `o: { inference: string }`λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€.
348348

349-
And it does so while you are typing, so that after typing `o.`, you
350-
get completions for the property `inference`, along with any other
351-
properties you'd have in a real program.
352-
Altogether, this feature can make TypeScript's inference look a bit
353-
like a unifying type inference engine, but it is not.
349+
μž…λ ₯ν•˜λŠ” λ™μ•ˆ, `o.` λ₯Ό 타이핑 후에,
350+
μ‹€μ œ ν”„λ‘œκ·Έλž¨μ— μžˆλŠ” λ‹€λ₯Έ 속성과 ν•¨κ»˜ 속성 `inference` 으둜
351+
보완할 수 μžˆμŠ΅λ‹ˆλ‹€.
352+
이 κΈ°λŠ₯은 TypeScript의 좔둠을 톡해 톡합적인 νƒ€μž… μΆ”λ‘  μ—”μ§„μ²˜λŸΌ
353+
λ³΄μ΄κ² μ§€λ§Œ, κ·Έλ ‡μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
354354

355-
## Type aliases
355+
## νƒ€μž… 별칭 (Type aliases)
356356

357-
Type aliases are mere aliases, just like `type` in Haskell. The
358-
compiler will attempt to use the alias name wherever it was used in
359-
the source code, but does not always succeed.
357+
νƒ€μž… 별칭은 Haskell의 `type`κ³Ό λ§ˆμ°¬κ°€μ§€λ‘œ λ‹¨μˆœν•œ λ³„μΉ­μž…λ‹ˆλ‹€.
358+
μ»΄νŒŒμΌλŸ¬λŠ” μ†ŒμŠ€ μ½”λ“œμ—μ„œ μ‚¬μš©λœ 별칭 이름을 μ‚¬μš©ν•˜λ €κ³ 
359+
μ‹œλ„ν•˜μ§€λ§Œ 항상 μ„±κ³΅ν•˜μ§€λŠ” μ•ŠμŠ΅λ‹ˆλ‹€.
360360

361-
```ts twoslash
361+
```ts
362362
type Size = [number, number];
363363
let x: Size = [101.1, 999.9];
364364
```
365365

366-
The closest equivalent to `newtype` is a _tagged intersection_:
366+
`newtype`κ³Ό κ°€μž₯ μœ μ‚¬ν•œ 것은 _νƒœκ·Έλœ ꡐ차 νƒ€μž…(tagged intersection)_ μž…λ‹ˆλ‹€:
367367

368368
```ts
369369
type FString = string & { __compileTimeOnly: any };
370370
```
371371

372-
An `FString` is just like a normal string, except that the compiler
373-
thinks it has a property named `__compileTimeOnly` that doesn't
374-
actually exist. This means that `FString` can still be assigned to
375-
`string`, but not the other way round.
372+
`FString`은 μ»΄νŒŒμΌλŸ¬κ°€ μ‹€μ œλ‘œλŠ” μ‘΄μž¬ν•˜μ§€ μ•ŠλŠ” `__compileTimeOnly`λΌλŠ”
373+
ν”„λ‘œνΌν‹°λ₯Ό κ°€μ§€κ³  μžˆλ‹€κ³  μƒκ°ν•˜λŠ” 점을 μ œμ™Έν•˜λ©΄ 일반 λ¬Έμžμ—΄κ³Ό κ°™μŠ΅λ‹ˆλ‹€.
374+
`FString`은 μ—¬μ „νžˆ `string`에 ν• λ‹Ή κ°€λŠ₯ν•˜μ§€λ§Œ,
375+
κ·Έ λ°˜λŒ€λŠ” λΆˆκ°€λŠ₯ν•˜λ‹€λŠ” 것을 μ˜λ―Έν•©λ‹ˆλ‹€.
376376

377-
## Discriminated Unions
377+
## νŒλ³„ μœ λ‹ˆμ–Έ (Discriminated Unions)
378378

379-
The closest equivalent to `data` is a union of types with discriminant
380-
properties, normally called discriminated unions in TypeScript:
379+
`data`와 κ°€μž₯ μœ μ‚¬ν•œ 것은 보톡 TypeScriptμ—μ„œ νŒλ³„ μœ λ‹ˆμ–Έμ΄λΌ λΆˆλ¦¬λŠ”,
380+
νŒλ³„ ν”„λ‘œνΌν‹°λ₯Ό κ°–λŠ” νƒ€μž…μ˜ μœ λ‹ˆμ–Έμž…λ‹ˆλ‹€:
381381

382382
```ts
383383
type Shape =
@@ -386,13 +386,13 @@ type Shape =
386386
| { kind: "triangle"; x: number; y: number };
387387
```
388388

389-
Unlike Haskell, the tag, or discriminant, is just a property in each
390-
object type. Each variant has an identical property with a different
391-
unit type. This is still a normal union type; the leading `|` is
392-
an optional part of the union type syntax. You can discriminate the
393-
members of the union using normal JavaScript code:
389+
ν•˜μŠ€μΌˆκ³Ό 달리, νƒœκ·Έ λ˜λŠ” νŒλ³„μ€ 각각 객체 νƒ€μž…μ—μ„œ 단지 속성에 λΆˆκ΅¬ν•©λ‹ˆλ‹€.
390+
특이 μΌ€μ΄μŠ€λŠ” λ‹€λ₯Έ μœ λ‹› νƒ€μž…κ³Ό ν•¨κ»˜ λ™μΌν•œ 속성을 κ°€μ§‘λ‹ˆλ‹€.
391+
아직 ν‰λ²”ν•œ μœ λ‹ˆμ–Ένƒ€μž…μž…λ‹ˆλ‹€; λ¦¬λ“œν•˜λŠ” `|` λŠ”
392+
μœ λ‹ˆμ–Έ νƒ€μž… ꡬ문의 선택적인 λΆ€λΆ„μž…λ‹ˆλ‹€. μœ λ‹ˆμ–Έμ„ μ‚¬μš©ν•˜λŠ” ν‰λ²”ν•œ JavaScript
393+
μ½”λ“œλ‘œ ꡬ별가λŠ₯ν•©λ‹ˆλ‹€:
394394

395-
```ts twoslash
395+
```ts
396396
type Shape =
397397
| { kind: "circle"; radius: number }
398398
| { kind: "square"; x: number }
@@ -409,14 +409,14 @@ function area(s: Shape) {
409409
}
410410
```
411411

412-
Note that the return type of `area` is inferred to be `number` because
413-
TypeScript knows the function is total. If some variant is not
414-
covered, the return type of `area` will be `number | undefined` instead.
412+
`area` 의 리턴 νƒ€μž…μ€ `number` λ₯Ό λ‚˜νƒ€λ‚΄λŠ”λ°, TypeScriptκ°€ ν•¨μˆ˜κ°€ μ „μ²΄λΌλŠ”
413+
κ±Έ μ•Œκ³  있기 λ•Œλ¬Έμ— μœ μ˜ν•΄μ•Όν•  ν•„μš”κ°€ μžˆμŠ΅λ‹ˆλ‹€. λͺ‡λͺ‡ 특이 μΌ€μ΄μŠ€κ°€ μ»€λ²„λ˜μ§€ μ•ŠμœΌλ©΄
414+
`area` 의 리턴 νƒ€μž…μ€ `number | undefined` 으둜 λŒ€μ‹ λ  κ²ƒμž…λ‹ˆλ‹€.
415415

416-
Also, unlike Haskell, common properties show up in any union, so you
417-
can usefully discriminate multiple members of the union:
416+
λ˜ν•œ, ν•˜μŠ€μΌˆκ³Ό 달리 ν”ν•œ 속성듀은 μ–΄λ–€ μœ λ‹ˆμ–Έμ—λ„ λ‚˜νƒ€λ‚˜λ©°,
417+
κ·Έλž˜μ„œ μœ μš©ν•˜κ²Œ μ—¬λŸ¬ 개의 μœ λ‹ˆμ–Έ ꡬ뢄가λŠ₯ν•©λ‹ˆλ‹€:
418418

419-
```ts twoslash
419+
```ts
420420
type Shape =
421421
| { kind: "circle"; radius: number }
422422
| { kind: "square"; x: number }
@@ -589,4 +589,4 @@ a[0] = 101; // error
589589
이 λ¬Έμ„œλŠ” 일상적인 μ½”λ“œμ—μ„œ 높은 μˆ˜μ€€μ˜ ꡬ문과 νƒ€μž…μ— λŒ€ν•œ κ°œμš”λ₯Ό λ‹΄κ³  μžˆμŠ΅λ‹ˆλ‹€. μ—¬κΈ°μ„œλΆ€ν„°λŠ” μ•„λž˜λ₯Ό μ°Έκ³ ν•˜μ‹œλ©΄ λ©λ‹ˆλ‹€:
590590

591591
*-* 전체 ν•Έλ“œλΆμ„ [μ²˜μŒλΆ€ν„° λκΉŒμ§€](/docs/handbook/intro.html) μ½μœΌμ„Έμš” (30m)
592-
*-* [Playground μ˜ˆμ‹œ](/play#show-examples)λ₯Ό λ³΄μ„Έμš”.
592+
*-* [Playground μ˜ˆμ‹œ](/play#show-examples)λ₯Ό λ³΄μ„Έμš”.

0 commit comments

Comments
Β (0)