@@ -293,91 +293,91 @@ let s: "left" | "right" = "right";
293
293
pad (" hi" , 10 , s );
294
294
```
295
295
296
- # Concepts similar to Haskell
296
+ # Haskell๊ณผ ๋น์ทํ ๊ฐ๋
( Concepts similar to Haskell)
297
297
298
- ## Contextual typing
298
+ ## ๋ฌธ๋งฅ์ ์ธ ํ์ดํ ( Contextual typing)
299
299
300
- TypeScript has some obvious places where it can infer types, like
301
- variable declarations :
300
+ TypeScript๋ ๋ณ์ ์ ์ธ๊ณผ ๊ฐ์ด ํ์
์ ์ถ๋ก ํ ์ ์๋
301
+ ๋ช ๊ฐ์ง ๋ถ๋ช
ํ ๋ฐฉ๋ฒ์ด ์์ต๋๋ค :
302
302
303
- ``` ts twoslash
303
+ ``` ts
304
304
let s = " I'm a string!" ;
305
305
```
306
306
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
+ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ํ์
์ถ๋ก ์ด ๊ฐ๋ฅํฉ๋๋ค :
309
309
310
- ``` ts twoslash
310
+ ``` ts
311
311
declare function map<T , U >(f : (t : T ) => U , ts : T []): U [];
312
312
let sns = map ((n ) => n .toString (), [1 , 2 , 3 ]);
313
313
```
314
314
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[] ` ํ์
์ ๊ฐ์ง๋๋ก ํฉ๋๋ค .
320
320
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
+ ์ ํธํฉ๋๋ค :
324
324
325
- ``` ts twoslash
325
+ ``` ts
326
326
declare function map<T , U >(ts : T [], f : (t : T ) => U ): U [];
327
327
```
328
328
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
+ ๊ทธ๋ฆฌ๊ณ ๋ฌธ๋งฅ์ ํตํด์ ๋ฆฌํด ํ์
์ ์ถ๋ก ํ ์ ์์ต๋๋ค :
332
332
333
- ``` ts twoslash
333
+ ``` ts
334
334
declare function run<T >(thunk : (t : T ) => void ): T ;
335
335
let i: { inference: string } = run ((o ) => {
336
336
o .inference = " INSERT STATE HERE" ;
337
337
});
338
338
```
339
339
340
- The type of ` o ` is determined to be ` { inference: string } ` because
340
+ ` o ` ์ ํ์
์ ` { inference: string } ` ์ผ๋ก ๊ฒฐ์ ๋์์ต๋๋ค. ์๋ํ๋ฉด
341
341
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 } ` ๋ฅผ ์ ๊ณตํฉ๋๋ค .
348
348
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
+ ๋ณด์ด๊ฒ ์ง๋ง, ๊ทธ๋ ์ง ์์ต๋๋ค .
354
354
355
- ## Type aliases
355
+ ## ํ์
๋ณ์นญ ( Type aliases)
356
356
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
+ ์๋ํ์ง๋ง ํญ์ ์ฑ๊ณตํ์ง๋ ์์ต๋๋ค .
360
360
361
- ``` ts twoslash
361
+ ``` ts
362
362
type Size = [number , number ];
363
363
let x: Size = [101.1 , 999.9 ];
364
364
```
365
365
366
- The closest equivalent to ` newtype ` is a _ tagged intersection _ :
366
+ ` newtype ` ๊ณผ ๊ฐ์ฅ ์ ์ฌํ ๊ฒ์ _ ํ๊ทธ๋ ๊ต์ฐจ ํ์
(tagged intersection) _ ์
๋๋ค :
367
367
368
368
``` ts
369
369
type FString = string & { __compileTimeOnly: any };
370
370
```
371
371
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
+ ๊ทธ ๋ฐ๋๋ ๋ถ๊ฐ๋ฅํ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค .
376
376
377
- ## Discriminated Unions
377
+ ## ํ๋ณ ์ ๋์ธ ( Discriminated Unions)
378
378
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
+ ํ๋ณ ํ๋กํผํฐ๋ฅผ ๊ฐ๋ ํ์
์ ์ ๋์ธ์
๋๋ค :
381
381
382
382
``` ts
383
383
type Shape =
@@ -386,13 +386,13 @@ type Shape =
386
386
| { kind: " triangle" ; x: number ; y: number };
387
387
```
388
388
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
+ ์ฝ๋๋ก ๊ตฌ๋ณ๊ฐ๋ฅํฉ๋๋ค :
394
394
395
- ``` ts twoslash
395
+ ``` ts
396
396
type Shape =
397
397
| { kind: " circle" ; radius: number }
398
398
| { kind: " square" ; x: number }
@@ -409,14 +409,14 @@ function area(s: Shape) {
409
409
}
410
410
```
411
411
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 ` ์ผ๋ก ๋์ ๋ ๊ฒ์
๋๋ค .
415
415
416
- Also, unlike Haskell, common properties show up in any union, so you
417
- can usefully discriminate multiple members of the union :
416
+ ๋ํ, ํ์ค์ผ๊ณผ ๋ฌ๋ฆฌ ํํ ์์ฑ๋ค์ ์ด๋ค ์ ๋์ธ์๋ ๋ํ๋๋ฉฐ,
417
+ ๊ทธ๋์ ์ ์ฉํ๊ฒ ์ฌ๋ฌ ๊ฐ์ ์ ๋์ธ ๊ตฌ๋ถ๊ฐ๋ฅํฉ๋๋ค :
418
418
419
- ``` ts twoslash
419
+ ``` ts
420
420
type Shape =
421
421
| { kind: " circle" ; radius: number }
422
422
| { kind: " square" ; x: number }
@@ -589,4 +589,4 @@ a[0] = 101; // error
589
589
์ด ๋ฌธ์๋ ์ผ์์ ์ธ ์ฝ๋์์ ๋์ ์์ค์ ๊ตฌ๋ฌธ๊ณผ ํ์
์ ๋ํ ๊ฐ์๋ฅผ ๋ด๊ณ ์์ต๋๋ค. ์ฌ๊ธฐ์๋ถํฐ๋ ์๋๋ฅผ ์ฐธ๊ณ ํ์๋ฉด ๋ฉ๋๋ค:
590
590
591
591
* -* ์ ์ฒด ํธ๋๋ถ์ [ ์ฒ์๋ถํฐ ๋๊น์ง] ( /docs/handbook/intro.html ) ์ฝ์ผ์ธ์ (30m)
592
- * -* [ Playground ์์] ( /play#show-examples ) ๋ฅผ ๋ณด์ธ์.
592
+ * -* [ Playground ์์] ( /play#show-examples ) ๋ฅผ ๋ณด์ธ์.
0 commit comments