@@ -146,32 +146,32 @@ let sepsis = anys[0] + anys[1]; // this could mean anything
146146To 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
176176type One = { p: string };
177177interface Two {
@@ -186,17 +186,17 @@ let two: Two = x;
186186two = 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
196196function 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
245245type Combined = { a: number } & { b: string };
246246type 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
262262declare function pad(s : string , n : number , direction : " left" | " right" ): string ;
263263pad (" 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
273273declare function pad(s : string , n : number , direction : " left" | " right" ): string ;
274274// ---cut---
275275let 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
290290declare function pad(s : string , n : number , direction : " left" | " right" ): string ;
291291// ---cut---
292292let s: " left" | " right" = " right" ;
0 commit comments