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 <[email protected]> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <[email protected]> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <[email protected]> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <[email protected]> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <[email protected]> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <[email protected]> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <[email protected]> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <[email protected]> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <[email protected]> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <[email protected]> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <[email protected]> * Update pages/tutorials/ts-for-functional-programmers.md Co-authored-by: Seohee Park <[email protected]> Co-authored-by: ๊น€๋ฏผ์ง€ <[email protected]> Co-authored-by: Seohee Park <[email protected]>
1 parent 6f7e492 commit 77f21eb

File tree

1 file changed

+59
-59
lines changed

1 file changed

+59
-59
lines changed

Diff for: โ€Žpages/tutorials/ts-for-functional-programmers.md

+59-59
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)