Skip to content

Commit 6f7e492

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

File tree

1 file changed

+60
-60
lines changed

1 file changed

+60
-60
lines changed

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

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -146,32 +146,32 @@ let sepsis = anys[0] + anys[1]; // this could mean anything
146146
To get an error when TypeScript produces an `any`, use
147147
`"noImplicitAny": true`, or `"strict": true` in `tsconfig.json`.
148148

149-
## Structural typing
149+
## ꡬ쑰적인 타이핑 (Structural typing)
150150

151-
Structural typing is a familiar concept to most functional
152-
programmers, although Haskell and most MLs are not
153-
structurally typed. Its basic form is pretty simple:
151+
비둝 ν•˜μŠ€μΌˆκ³Ό λŒ€λΆ€λΆ„μ˜ ML은 ꡬ쑰적으둜 νƒ€μ΄ν•‘ν•˜μ§€ μ•Šμ§€λ§Œ,
152+
ꡬ쑰적 타이핑은 λŒ€λΆ€λΆ„μ˜ ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ¨Έμ—κ²ŒλŠ” μ΅μˆ™ν•œ κ°œλ…μž…λ‹ˆλ‹€.
153+
κΈ°λ³Έ ν˜•νƒœλŠ” μ•„μ£Ό κ°„λ‹¨ν•©λ‹ˆλ‹€:
154154

155155
```ts
156156
// @strict: false
157-
let o = { x: "hi", extra: 1 }; // ok
158-
let o2: { x: string } = o; // ok
157+
let o = { x: "hi", extra: 1 }; // 성곡
158+
let o2: { x: string } = o; // 성곡
159159
```
160160

161-
Here, the object literal `{ x: "hi", extra: 1 }` has a matching
162-
literal type `{ x: string, extra: number }`. That
163-
type is assignable to `{ x: string }` since
164-
it has all the required properties and those properties have
165-
assignable types. The extra property doesn't prevent assignment, it
166-
just makes it a subtype of `{ x: string }`.
161+
μ—¬κΈ°μ„œ, 객체 λ¦¬ν„°λŸ΄ `{ x: "hi", extra : 1 }`에 λ§€μΉ˜λ˜λŠ”
162+
`{ x : string, extra : number }` κ°€ μžˆμŠ΅λ‹ˆλ‹€. 이
163+
νƒ€μž…μ€ ν•„μˆ˜ ν”„λ‘œνΌν‹°κ°€ λͺ¨λ‘ 있고 ν•΄λ‹Ή ν”„λ‘œνΌν‹°μ— ν• λ‹Ή κ°€λŠ₯ν•œ νƒ€μž…μ΄ μžˆμœΌλ―€λ‘œ
164+
`{ x : string }` 에 ν• λ‹Ήν•  수 μžˆμŠ΅λ‹ˆλ‹€.
165+
λ‚˜λ¨Έμ§€ ν”„λ‘œνΌν‹°λŠ” 할당을 막지 μ•Šκ³ , `{x : string}`의 μ„œλΈŒνƒ€μž…μœΌλ‘œ
166+
λ§Œλ“­λ‹ˆλ‹€.
167167

168-
Named types just give a name to a type; for assignability purposes
169-
there's no difference between the type alias `One` and the interface
170-
type `Two` below. They both have a property `p: string`. (Type aliases
171-
behave differently from interfaces with respect to recursive
172-
definitions and type parameters, however.)
168+
λ„€μž„λ“œ νƒ€μž…λ“€μ€ νƒ€μž…μ—μ„œ 이름을 뢙일 λΏμž…λ‹ˆλ‹€. 할당을 μœ„ν•΄μ„œλΌλ©΄ νƒ€μž… 별칭
169+
`One` κ³Ό μΈν„°νŽ˜μ΄μŠ€ νƒ€μž… `Two` μ‚¬μ΄μ—λŠ” 별 λ‹€λ₯Έ 점이 μ—†μŠ΅λ‹ˆλ‹€.
170+
λ‘˜ λ‹€ `p: string` ν”„λ‘œνΌν‹°λ₯Ό κ°€μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€.
171+
(단, νƒ€μž… 별칭은 μž¬κ·€ μ •μ˜μ™€ νƒ€μž… λ§€κ°œλ³€μˆ˜μ— κ΄€λ ¨ν•œ μΈν„°νŽ˜μ΄μŠ€μ—μ„œλŠ” λ‹€λ₯΄κ²Œ
172+
λ™μž‘ν•©λ‹ˆλ‹€.)
173173

174-
```ts twoslash
174+
```ts
175175
// @errors: 2322
176176
type One = { p: string };
177177
interface Two {
@@ -186,17 +186,17 @@ let two: Two = x;
186186
two = new Three();
187187
```
188188

189-
## Unions
189+
## μœ λ‹ˆμ–Έ (Unions)
190190

191-
In TypeScript, union types are untagged. In other words, they are not
192-
discriminated unions like `data` in Haskell. However, you can often
193-
discriminate types in a union using built-in tags or other properties.
191+
TypeScriptμ—μ„œ μœ λ‹ˆμ–Έ νƒ€μž…μ€ νƒœκ·Έλ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. λ‹€λ₯΄κ²Œ λ§ν•˜λ©΄,
192+
ν•˜μŠ€μΌˆμ—μ„œ `data` 와 달리 μœ λ‹ˆμ–Έμ€ κ΅¬λ³„ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.
193+
κ·ΈλŸ¬λ‚˜ λ‹€λ₯Έ ν”„λ‘œνΌν‹°λ‚˜ λ‚΄μž₯된 νƒœκ·Έλ₯Ό μ‚¬μš©ν•˜λŠ” μœ λ‹ˆμ–ΈμœΌλ‘œ νƒ€μž…μ„ ꡬ별할 수 μžˆμŠ΅λ‹ˆλ‹€.
194194

195-
```ts twoslash
195+
```ts
196196
function start(
197197
arg: string | string[] | (() => string) | { s: string }
198198
): string {
199-
// this is super common in JavaScript
199+
// JavaScriptμ—μ„œ μ•„μ£Ό μΌλ°˜μ μž…λ‹ˆλ‹€
200200
if (typeof arg === "string") {
201201
return commonCase(arg);
202202
} else if (Array.isArray(arg)) {
@@ -208,21 +208,21 @@ function start(
208208
}
209209

210210
function commonCase(s: string): string {
211-
// finally, just convert a string to another string
211+
// λ§ˆμ§€λ§‰μœΌλ‘œ, λ‹€λ₯Έ λ¬Έμžμ—΄λ‘œ λ³€ν™˜ν•©λ‹ˆλ‹€
212212
return s;
213213
}
214214
}
215215
```
216216

217-
`string`, `Array` and `Function` have built-in type predicates,
218-
conveniently leaving the object type for the `else` branch. It is
219-
possible, however, to generate unions that are difficult to
220-
differentiate at runtime. For new code, it's best to build only
221-
discriminated unions.
217+
`string`, `Array` 와 `Function` 은 Β νƒ€μž… μ‘°κ±΄μžκ°€
218+
λ‚΄μž₯λ˜μ–΄ 있고, `else` 브랜치λ₯Ό μœ„ν•œ 객체 νƒ€μž…μ€ 편의λ₯Ό μœ„ν•΄
219+
λ‚¨κ²¨λ‘λŠ” 게 μ’‹μŠ΅λ‹ˆλ‹€.
220+
κ·ΈλŸ¬λ‚˜ λŸ°νƒ€μž„μ— κ΅¬λ³„ν•˜κΈ° μ–΄λ €μš΄ μœ λ‹ˆμ–Έμ„ 생성할 수 μžˆμŠ΅λ‹ˆλ‹€.
221+
μƒˆλ‘œμš΄ μ½”λ“œμ˜ 경우, κ΅¬λ³„ν•˜λŠ” μœ λ‹ˆμ–Έλ§Œ κ΅¬μΆ•ν•˜λŠ” 게 κ°€μž₯ μ’‹μŠ΅λ‹ˆλ‹€.
222222

223-
The following types have built-in predicates:
223+
λ‹€μŒ νƒ€μž…λ“€μ€ 쑰건자λ₯Ό κ°€μ§€κ³  μžˆλ‹€:
224224

225-
| Type | Predicate |
225+
| νƒ€μž… | 쑰건자 |
226226
| --------- | ---------------------------------- |
227227
| string | `typeof s === "string"` |
228228
| number | `typeof n === "number"` |
@@ -234,59 +234,59 @@ The following types have built-in predicates:
234234
| array | `Array.isArray(a)` |
235235
| object | `typeof o === "object"` |
236236

237-
Note that functions and arrays are objects at runtime, but have their
238-
own predicates.
237+
ν•¨μˆ˜μ™€ 배열은 λŸ°νƒ€μž„μ—μ„œ κ°μ²΄μ΄μ§€λ§Œ 고유의 쑰건자λ₯Ό κ°€μ§€κ³  μžˆλ‹€λŠ” κ±Έ
238+
κΈ°λ‘ν•©μ‹œλ‹€.
239239

240-
### Intersections
240+
### ꡐ집합
241241

242-
In addition to unions, TypeScript also has intersections:
242+
μœ λ‹ˆμ–Έκ³Ό λ”λΆˆμ–΄ TypeScript은 κ΅μ§‘ν•©κΉŒμ§€ κ°€μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€:
243243

244-
```ts twoslash
244+
```ts
245245
type Combined = { a: number } & { b: string };
246246
type Conflicting = { a: number } & { a: string };
247247
```
248248

249-
`Combined` has two properties, `a` and `b`, just as if they had been
250-
written as one object literal type. Intersection and union are
251-
recursive in case of conflicts, so `Conflicting.a: number & string`.
249+
`Combined` 은 마치 ν•˜λ‚˜μ˜ 객체 λ¦¬ν„°λŸ΄ νƒ€μž…μœΌλ‘œ μž‘μ„±λœ 것 처럼
250+
`a` 와 `b` 두 개의 속성을 κ°€μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€. ꡐ집합과 μœ λ‹ˆμ–Έμ€ μž¬κ·€μ μΈ
251+
μΌ€μ΄μŠ€μ—μ„œ μΆ©λŒμ„ μΌμœΌμΌœμ„œ `Conflicting.a: number & string` μž…λ‹ˆλ‹€.
252252

253-
## Unit types
253+
## μœ λ‹ˆμ–Έ νƒ€μž… (Unit types)
254254

255-
Unit types are subtypes of primitive types that contain exactly one
256-
primitive value. For example, the string `"foo"` has the type
257-
`"foo"`. Since JavaScript has no built-in enums, it is common to use a set of
258-
well-known strings instead. Unions of string literal types allow
259-
TypeScript to type this pattern:
255+
μœ λ‹ˆμ–Έ νƒ€μž…μ€ μ •ν™•νžˆ ν•˜λ‚˜μ˜ μ›μ‹œ 값을 ν¬ν•¨ν•˜κ³  μžˆλŠ” μ›μ‹œ νƒ€μž…μ˜ μ„œλΈŒνƒ€μž…μž…λ‹ˆλ‹€.
256+
예λ₯Ό λ“€λ©΄, λ¬Έμžμ—΄ `"foo"` λŠ” νƒ€μž… `"foo"`λ₯Ό κ°€μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€.
257+
JavaScriptλŠ” λ‚΄μž₯된 enum이 μ—†κΈ° λ•Œλ¬Έμ— 잘 μ•Œλ €μ§„ λ¬Έμžμ—΄ μ„ΈνŠΈλ₯Ό
258+
λŒ€μ‹ ν•΄μ„œ μ“°λŠ”κ²Œ ν”ν•©λ‹ˆλ‹€.
259+
λ¬Έμžμ—΄ λ¦¬ν„°λŸ΄ νƒ€μž… μœ λ‹ˆμ–Έμ€ TypeScriptμ—μ„œ 이 νŒ¨ν„΄μ„ λ”°λΌκ°‘λ‹ˆλ‹€:
260260

261-
```ts twoslash
261+
```ts
262262
declare function pad(s: string, n: number, direction: "left" | "right"): string;
263263
pad("hi", 10, "left");
264264
```
265265

266-
When needed, the compiler _widens_ &mdash; converts to a
267-
supertype &mdash; the unit type to the primitive type, such as `"foo"`
268-
to `string`. This happens when using mutability, which can hamper some
269-
uses of mutable variables:
266+
ν•„μš”ν•œ κ²½μš°μ— 컴파일러둜 ν™•μž₯κ°€λŠ₯ν•©λ‹ˆλ‹€_ &mdash; μƒμœ„ νƒ€μž…μœΌλ‘œ
267+
λ³€ν™˜ν•©λ‹ˆλ‹€ &mdash; μ›μ‹œ νƒ€μž…μ—μ„œ μœ λ‹› νƒ€μž…μœΌλ‘œ, `string`μ—μ„œ `"foo"`으둜
268+
μˆ˜μ •κ°€λŠ₯ν•  λ•Œ μΌμ–΄λ‚˜λ©°, μˆ˜μ •κ°€λŠ₯ν•œ λ³€μˆ˜λ₯Ό 일뢀 μ‚¬μš©ν•  λ•Œ μ œλŒ€λ‘œ
269+
λ™μž‘ν•˜μ§€ μ•Šμ„ 수 μžˆμŠ΅λ‹ˆλ‹€:
270270

271-
```ts twoslash
271+
```ts
272272
// @errors: 2345
273273
declare function pad(s: string, n: number, direction: "left" | "right"): string;
274274
// ---cut---
275275
let s = "right";
276-
pad("hi", 10, s); // error: 'string' is not assignable to '"left" | "right"'
276+
pad("hi", 10, s); // 였λ₯˜: 'string'은 '"left" | "right"'에 ν• λ‹Ήν•  수 μ—†μŠ΅λ‹ˆλ‹€.
277277
```
278278

279-
Here's how the error happens:
279+
이런 μ—λŸ¬κ°€ λ‚˜νƒ€λ‚  수 μžˆμŠ΅λ‹ˆλ‹€:
280280

281281
*-* `"right": "right"`
282-
*-* `s: string` because `"right"` widens to `string` on assignment to a mutable variable.
283-
*-* `string` is not assignable to `"left" | "right"`
282+
*-* `s: string` 은 `"right"` κ°€ μˆ˜μ •κ°€λŠ₯ν•œ λ³€μˆ˜μ— 할당될 λ•Œ `string` 으둜 ν™•μž₯이 κ°€λŠ₯ν•©λ‹ˆλ‹€.
283+
*-* `string` 은 `"left" | "right"`에 ν• λ‹Ήν•  수 μ—†μŠ΅λ‹ˆλ‹€.
284284

285-
You can work around this with a type annotation for `s`, but that
286-
in turn prevents assignments to `s` of variables that are not of type
287-
`"left" | "right"`.
285+
`s`에 νƒ€μž… ν‘œκΈ°λ₯Ό μ‚¬μš©ν•˜μ—¬ ν•΄κ²° κ°€λŠ₯ν•˜μ§€λ§Œ,
286+
κ·Έ κ²°κ³Ό `"left" | "right"` νƒ€μž…μ΄ μ•„λ‹Œ λ³€μˆ˜κ°€
287+
`s`에 ν• λ‹Ήλ˜λŠ” 것을 λ°©μ§€ν•˜κ²Œ λ©λ‹ˆλ‹€.
288288

289-
```ts twoslash
289+
```ts
290290
declare function pad(s: string, n: number, direction: "left" | "right"): string;
291291
// ---cut---
292292
let s: "left" | "right" = "right";

0 commit comments

Comments
Β (0)