@@ -146,32 +146,32 @@ let sepsis = anys[0] + anys[1]; // this could mean anything
146
146
To get an error when TypeScript produces an ` any ` , use
147
147
` "noImplicitAny": true ` , or ` "strict": true ` in ` tsconfig.json ` .
148
148
149
- ## Structural typing
149
+ ## ꡬ쑰μ μΈ νμ΄ν ( Structural typing)
150
150
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
+ κΈ°λ³Έ ννλ μμ£Ό κ°λ¨ν©λλ€ :
154
154
155
155
``` ts
156
156
// @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 ; // μ±κ³΅
159
159
```
160
160
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
+ λ§λλλ€ .
167
167
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
+ λμν©λλ€ .)
173
173
174
- ``` ts twoslash
174
+ ``` ts
175
175
// @errors: 2322
176
176
type One = { p: string };
177
177
interface Two {
@@ -186,17 +186,17 @@ let two: Two = x;
186
186
two = new Three ();
187
187
```
188
188
189
- ## Unions
189
+ ## μ λμΈ ( Unions)
190
190
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
+ κ·Έλ¬λ λ€λ₯Έ νλ‘νΌν°λ λ΄μ₯λ νκ·Έλ₯Ό μ¬μ©νλ μ λμΈμΌλ‘ νμ
μ ꡬλ³ν μ μμ΅λλ€ .
194
194
195
- ``` ts twoslash
195
+ ``` ts
196
196
function start(
197
197
arg : string | string [] | (() => string ) | { s: string }
198
198
): string {
199
- // this is super common in JavaScript
199
+ // JavaScriptμμ μμ£Ό μΌλ°μ μ
λλ€
200
200
if (typeof arg === " string" ) {
201
201
return commonCase (arg );
202
202
} else if (Array .isArray (arg )) {
@@ -208,21 +208,21 @@ function start(
208
208
}
209
209
210
210
function commonCase(s : string ): string {
211
- // finally, just convert a string to another string
211
+ // λ§μ§λ§μΌλ‘, λ€λ₯Έ λ¬Έμμ΄λ‘ λ³νν©λλ€
212
212
return s ;
213
213
}
214
214
}
215
215
```
216
216
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
+ μλ‘μ΄ μ½λμ κ²½μ°, ꡬλ³νλ μ λμΈλ§ ꡬμΆνλ κ² κ°μ₯ μ’μ΅λλ€ .
222
222
223
- The following types have built-in predicates :
223
+ λ€μ νμ
λ€μ 쑰건μλ₯Ό κ°μ§κ³ μλ€ :
224
224
225
- | Type | Predicate |
225
+ | νμ
| 쑰건μ |
226
226
| --------- | ---------------------------------- |
227
227
| string | ` typeof s === "string" ` |
228
228
| number | ` typeof n === "number" ` |
@@ -234,59 +234,59 @@ The following types have built-in predicates:
234
234
| array | ` Array.isArray(a) ` |
235
235
| object | ` typeof o === "object" ` |
236
236
237
- Note that functions and arrays are objects at runtime, but have their
238
- own predicates .
237
+ ν¨μμ λ°°μ΄μ λ°νμμμ κ°μ²΄μ΄μ§λ§ κ³ μ μ 쑰건μλ₯Ό κ°μ§κ³ μλ€λ κ±Έ
238
+ κΈ°λ‘ν©μλ€ .
239
239
240
- ### Intersections
240
+ ### κ΅μ§ν©
241
241
242
- In addition to unions, TypeScript also has intersections :
242
+ μ λμΈκ³Ό λλΆμ΄ TypeScriptμ κ΅μ§ν©κΉμ§ κ°μ§κ³ μμ΅λλ€ :
243
243
244
- ``` ts twoslash
244
+ ``` ts
245
245
type Combined = { a: number } & { b: string };
246
246
type Conflicting = { a: number } & { a: string };
247
247
```
248
248
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 ` μ
λλ€ .
252
252
253
- ## Unit types
253
+ ## μ λμΈ νμ
( Unit types)
254
254
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μμ μ΄ ν¨ν΄μ λ°λΌκ°λλ€ :
260
260
261
- ``` ts twoslash
261
+ ``` ts
262
262
declare function pad(s : string , n : number , direction : " left" | " right" ): string ;
263
263
pad (" hi" , 10 , " left" );
264
264
```
265
265
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
+ λμνμ§ μμ μ μμ΅λλ€ :
270
270
271
- ``` ts twoslash
271
+ ``` ts
272
272
// @errors: 2345
273
273
declare function pad(s : string , n : number , direction : " left" | " right" ): string ;
274
274
// ---cut---
275
275
let s = " right" ;
276
- pad (" hi" , 10 , s ); // error : 'string' is not assignable to '"left" | "right"'
276
+ pad (" hi" , 10 , s ); // μ€λ₯ : 'string'μ '"left" | "right"'μ ν λΉν μ μμ΅λλ€.
277
277
```
278
278
279
- Here's how the error happens :
279
+ μ΄λ° μλ¬κ° λνλ μ μμ΅λλ€ :
280
280
281
281
* -* ` "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" ` μ ν λΉν μ μμ΅λλ€.
284
284
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 ` μ ν λΉλλ κ²μ λ°©μ§νκ² λ©λλ€ .
288
288
289
- ``` ts twoslash
289
+ ``` ts
290
290
declare function pad(s : string , n : number , direction : " left" | " right" ): string ;
291
291
// ---cut---
292
292
let s: " left" | " right" = " right" ;
0 commit comments