-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathAdvanced Types.html
1973 lines (1337 loc) Β· 211 KB
/
Advanced Types.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE HTML>
<html lang="" >
<head>
<meta charset="UTF-8">
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<title>κ³ κΈ νμ
Β· GitBook</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="description" content="">
<meta name="generator" content="GitBook 3.2.3">
<meta property="og:title" content="TypeScript νκΈ λ¬Έμ">
<meta property="og:image" content="https://avatars1.githubusercontent.com/u/37781342?s=200&v=4">
<meta property="og:description" content="TypeScript νκΈ λ²μ λ¬Έμμ
λλ€">
<link rel="stylesheet" href="../gitbook/style.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight-1/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-forkmegithub/plugin.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
<link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
<link rel="stylesheet" href="../assets/css/website.css">
<link rel="stylesheet" type="text/css" href="../assets/css/atom-one-dark.css" />
<meta name="HandheldFriendly" content="true"/>
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
<link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
<link rel="next" href="Symbols.html" />
<link rel="prev" href="Type Compatibility.html" />
</head>
<body>
<div class="book">
<div class="book-summary">
<div id="book-search-input" role="search">
<input type="text" placeholder="Type to search" />
</div>
<nav role="navigation">
<ul class="summary">
<li class="chapter " data-level="1.1" data-path="../">
<a href="../">
μκ°
</a>
</li>
<li class="header">νν 리μΌ</li>
<li class="chapter " data-level="2.1" data-path="tutorials/TypeScript in 5 minutes.html">
<a href="tutorials/TypeScript in 5 minutes.html">
5λΆ μμ 보λ TypeScript
</a>
</li>
<li class="chapter " data-level="2.2" data-path="tutorials/ASP.NET Core.html">
<a href="tutorials/ASP.NET Core.html">
ASP.NET Core
</a>
</li>
<li class="chapter " data-level="2.3" data-path="tutorials/Gulp.html">
<a href="tutorials/Gulp.html">
κ±Έν
</a>
</li>
<li class="chapter " data-level="2.4" data-path="tutorials/Migrating from JavaScript.html">
<a href="tutorials/Migrating from JavaScript.html">
JavaScriptμμ λ§μ΄κ·Έλ μ΄μ
</a>
</li>
<li class="chapter " data-level="2.5" data-path="tutorials/React & Webpack.html">
<a href="tutorials/React & Webpack.html">
리μ‘νΈ & μΉν©
</a>
</li>
<li class="header">νΈλλΆ</li>
<li class="chapter " data-level="3.1" data-path="Basic Types.html">
<a href="Basic Types.html">
κΈ°λ³Έ νμ
</a>
</li>
<li class="chapter " data-level="3.2" data-path="Variable Declarations.html">
<a href="Variable Declarations.html">
λ³μ μ μΈ
</a>
</li>
<li class="chapter " data-level="3.3" data-path="Interfaces.html">
<a href="Interfaces.html">
μΈν°νμ΄μ€
</a>
</li>
<li class="chapter " data-level="3.4" data-path="Classes.html">
<a href="Classes.html">
ν΄λμ€
</a>
</li>
<li class="chapter " data-level="3.5" data-path="Functions.html">
<a href="Functions.html">
ν¨μ
</a>
</li>
<li class="chapter " data-level="3.6" data-path="Generics.html">
<a href="Generics.html">
μ λ€λ¦
</a>
</li>
<li class="chapter " data-level="3.7" data-path="Enums.html">
<a href="Enums.html">
μ΄κ±°ν
</a>
</li>
<li class="chapter " data-level="3.8" data-path="Type Inference.html">
<a href="Type Inference.html">
νμ
μΆλ‘
</a>
</li>
<li class="chapter " data-level="3.9" data-path="Type Compatibility.html">
<a href="Type Compatibility.html">
νμ
νΈνμ±
</a>
</li>
<li class="chapter active" data-level="3.10" data-path="Advanced Types.html">
<a href="Advanced Types.html">
κ³ κΈ νμ
</a>
</li>
<li class="chapter " data-level="3.11" data-path="Symbols.html">
<a href="Symbols.html">
μ¬λ³Ό
</a>
</li>
<li class="chapter " data-level="3.12" data-path="Iterators and Generators.html">
<a href="Iterators and Generators.html">
μ΄ν°λ μ΄ν°μ μ λ€λ μ΄ν°
</a>
</li>
<li class="chapter " data-level="3.13" data-path="Modules.html">
<a href="Modules.html">
λͺ¨λ
</a>
</li>
<li class="chapter " data-level="3.14" data-path="Namespaces.html">
<a href="Namespaces.html">
λ€μμ€νμ΄μ€
</a>
</li>
<li class="chapter " data-level="3.15" data-path="Namespaces and Modules.html">
<a href="Namespaces and Modules.html">
λ€μμ€νμ΄μ€μ λͺ¨λ
</a>
</li>
<li class="chapter " data-level="3.16" data-path="Module Resolution.html">
<a href="Module Resolution.html">
λͺ¨λ ν΄μ
</a>
</li>
<li class="chapter " data-level="3.17" data-path="Declaration Merging.html">
<a href="Declaration Merging.html">
μ μΈ λ³ν©
</a>
</li>
<li class="chapter " data-level="3.18" data-path="JSX.html">
<a href="JSX.html">
JSX
</a>
</li>
<li class="chapter " data-level="3.19" data-path="Decorators.html">
<a href="Decorators.html">
λ°μ½λ μ΄ν°
</a>
</li>
<li class="chapter " data-level="3.20" data-path="Mixins.html">
<a href="Mixins.html">
λ―Ήμ€μΈ
</a>
</li>
<li class="chapter " data-level="3.21" data-path="Triple-Slash Directives.html">
<a href="Triple-Slash Directives.html">
νΈλ¦¬ν-μ¬λμ μ§μμ
</a>
</li>
<li class="chapter " data-level="3.22" data-path="Type Checking JavaScript Files.html">
<a href="Type Checking JavaScript Files.html">
JavaScript νμΌ νμ
κ²μ¬
</a>
</li>
<li class="chapter " data-level="3.23" data-path="Utility Types.html">
<a href="Utility Types.html">
μ νΈλ¦¬ν° νμ
</a>
</li>
<li class="header">μ μΈ νμΌ</li>
<li class="chapter " data-level="4.1" data-path="declaration files/Introduction.html">
<a href="declaration files/Introduction.html">
μκ°
</a>
</li>
<li class="chapter " data-level="4.2" data-path="declaration files/Library Structures.html">
<a href="declaration files/Library Structures.html">
Library Structures
</a>
</li>
<li class="chapter " data-level="4.3" data-path="declaration files/By Example.html">
<a href="declaration files/By Example.html">
By Example
</a>
</li>
<li class="chapter " data-level="4.4" data-path="declaration files/Do's and Don'ts.html">
<a href="declaration files/Do's and Don'ts.html">
Do's and Don'ts
</a>
</li>
<li class="chapter " data-level="4.5" data-path="declaration files/Deep Dive.html">
<a href="declaration files/Deep Dive.html">
Deep Dive
</a>
</li>
<li class="chapter " data-level="4.6" data-path="declaration files/Templates.html">
<a href="declaration files/Templates.html">
Templates
</a>
<ul class="articles">
<li class="chapter " data-level="4.6.1" data-path="declaration files/templates/global-modifying-module.d.ts.html">
<a href="declaration files/templates/global-modifying-module.d.ts.html">
global-modifying-module.d.ts
</a>
</li>
<li class="chapter " data-level="4.6.2" data-path="declaration files/templates/global-plugin.d.ts.html">
<a href="declaration files/templates/global-plugin.d.ts.html">
global-plugin.d.ts
</a>
</li>
<li class="chapter " data-level="4.6.3" data-path="declaration files/templates/global.d.ts.html">
<a href="declaration files/templates/global.d.ts.html">
global.d.ts
</a>
</li>
<li class="chapter " data-level="4.6.4" data-path="declaration files/templates/module-class.d.ts.html">
<a href="declaration files/templates/module-class.d.ts.html">
module-class.d.ts
</a>
</li>
<li class="chapter " data-level="4.6.5" data-path="declaration files/templates/module-function.d.ts.html">
<a href="declaration files/templates/module-function.d.ts.html">
module-function.d.ts
</a>
</li>
<li class="chapter " data-level="4.6.6" data-path="declaration files/templates/module-plugin.d.ts.html">
<a href="declaration files/templates/module-plugin.d.ts.html">
module-plugin.d.ts
</a>
</li>
<li class="chapter " data-level="4.6.7" data-path="declaration files/templates/module.d.ts.html">
<a href="declaration files/templates/module.d.ts.html">
module.d.ts
</a>
</li>
</ul>
</li>
<li class="chapter " data-level="4.7" data-path="declaration files/Publishing.html">
<a href="declaration files/Publishing.html">
Publishing
</a>
</li>
<li class="chapter " data-level="4.8" data-path="declaration files/Consumption.html">
<a href="declaration files/Consumption.html">
Consumption
</a>
</li>
<li class="header">νλ‘μ νΈ νκ²½μ€μ </li>
<li class="chapter " data-level="5.1" data-path="tsconfig.json.html">
<a href="tsconfig.json.html">
tsconfig.json
</a>
</li>
<li class="chapter " data-level="5.2" data-path="Compiler Options.html">
<a href="Compiler Options.html">
μ»΄νμΌλ¬ μ΅μ
</a>
</li>
<li class="chapter " data-level="5.3" data-path="Project References.html">
<a href="Project References.html">
νλ‘μ νΈ λ νΌλ°μ€
</a>
</li>
<li class="chapter " data-level="5.4" data-path="Compiler Options in MSBuild.html">
<a href="Compiler Options in MSBuild.html">
MSBuildμμμ μ»΄νμΌλ¬ μ΅μ
</a>
</li>
<li class="chapter " data-level="5.5" data-path="Integrating with Build Tools.html">
<a href="Integrating with Build Tools.html">
λΉλ λꡬμ ν΅ν©
</a>
</li>
<li class="chapter " data-level="5.6" data-path="Nightly Builds.html">
<a href="Nightly Builds.html">
Nightly λΉλ
</a>
</li>
<li class="header">λ¦΄λ¦¬μ¦ λ
ΈνΈ</li>
<li class="chapter " data-level="6.1" data-path="release notes/TypeScript 3.9.html">
<a href="release notes/TypeScript 3.9.html">
TypeScript 3.9
</a>
</li>
<li class="chapter " data-level="6.2" data-path="release notes/TypeScript 3.8.html">
<a href="release notes/TypeScript 3.8.html">
TypeScript 3.8
</a>
</li>
<li class="divider"></li>
<li>
<a href="https://www.gitbook.com" target="blank" class="gitbook-link">
Published with GitBook
</a>
</li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<!-- Title -->
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i>
<a href=".." >κ³ κΈ νμ
</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<div id="book-search-results">
<div class="search-noresults">
<section class="normal markdown-section">
<h1 id="목차-table-of-contents">목차 (Table of contents)</h1>
<p><a href="#교차-타입-intersection-types">교차 타입 (Intersection Types)</a></p>
<p><a href="#유니언-타입-union-types">유니언 타입 (Union Types)</a></p>
<p><a href="#타입-가드와-차별-타입-type-guards-and-differentiating-types">타입 가드와 차별 타입 (Type Guards and Differentiating Types)</a></p>
<ul>
<li><a href="#사용자-정의-타입-가드-user-defined-type-guards">사용자-정의 타입 가드 (User-Defined Type Guards)</a><ul>
<li><a href="#타입-서술어-사용하기-using-type-predicates">타입 서술어 사용하기 (Using type predicates)</a></li>
<li><a href="#in-연산자-사용하기-using-the-in-operator"><code>in</code> 연산자 사용하기 (Using the <code>in</code> operator)</a></li>
</ul>
</li>
<li><a href="#typeof-타입-가드-typeof-type-guards"><code>typeof</code> 타입 가드 (<code>typeof</code> type guards)</a></li>
<li><a href="#instanceof-타입-가드-instanceof-type-guards"><code>instanceof</code> 타입 가드 (<code>instanceof</code> type guards)</a></li>
</ul>
<p><a href="#널러블-타입-nullable-types">널러블 타입 (Nullable types)</a></p>
<ul>
<li><a href="#선택적-매개변수와-프로퍼티-optional-parameters-and-properties">선택적 매개변수와 프로퍼티 (Optional parameters and properties)</a></li>
<li><a href="#타입-가드와-타입-단언-type-guards-and-type-assertions">타입 가드와 타입 단언 (Type guards and type assertions)</a></li>
</ul>
<p><a href="#타입-별칭-type-aliases">타입 별칭 (Type Aliases)</a></p>
<ul>
<li><a href="#인터페이스-vs-타입-별칭-interfaces-vs-type-aliases">인터페이스 vs. 타입 별칭 (Interfaces vs. Type Aliases)</a></li>
</ul>
<p><a href="#문자열-리터럴-타입-string-literal-types">문자열 리터럴 타입 (String Literal Types)</a></p>
<p><a href="#숫자-리터럴-타입-numeric-literal-types">숫자 리터럴 타입 (Numeric Literal Types)</a></p>
<p><a href="#열거형-멤버-타입-enum-member-types">열거형 멤버 타입 (Enum Member Types)</a></p>
<p><a href="#판별-유니언-discriminated-unions">판별 유니언</a></p>
<ul>
<li><a href="#엄격한-검사-exhaustiveness-checking">엄격한 검사</a></li>
</ul>
<p><a href="#다형성-this-타입-polymorphic-this-types">다형성 <code>this</code> 타입</a></p>
<p><a href="#인덱스-타입-index-types">인덱스 타입</a></p>
<ul>
<li><a href="#인덱스-타입과-인덱스-시그니처-index-types-and-index-signatures">인덱스 타입과 인덱스 시그니처</a></li>
</ul>
<p><a href="#매핑-타입-mapped-types">매핑 타입</a></p>
<ul>
<li><a href="#매핑-타입의-추론-inference-from-mapped-types">매핑 타입의 추론</a></li>
</ul>
<p><a href="#조건부-타입-conditional-types">조건부 타입</a></p>
<ul>
<li><a href="#분산-조건부-타입-distributive-conditional-types">분산 조건부 타입</a></li>
<li><a href="#조건부-타입의-타입-추론-type-inference-in-conditional-types">조건부 타입의 타입 추론</a></li>
<li><a href="#미리-정의된-조건부-타입-predefined-conditional-types">미리 정의된 조건부 타입</a></li>
</ul>
<h1 id="교차-타입-intersection-types">교차 타입 (Intersection Types)</h1>
<p>교차 타입은 여러 타입을 하나로 결합합니다.
기존 타입을 합쳐 필요한 모든 기능을 가진 하나의 타입을 얻을 수 있습니다.
예를 들어, <code>Person & Serializable & Loggable</code>은 <code>Person</code> <em>과</em> <code>Serializable</code> <em>그리고</em> <code>Loggable</code>입니다.
즉, 이 타입의 객체는 세 가지 타입의 모든 멤버를 갖게 됩니다.</p>
<p>기존 객체-지향 틀과는 맞지 않는 믹스인이나 다른 컨셉들에서 교차 타입이 사용되는 것을 볼 수 있습니다.
(JavaScript에는 이런 것들이 많습니다!)
믹스인 만드는 방법을 간단한 예제를 통해 살펴보겠습니다:</p>
<pre><code class="lang-ts"><span class="cm-keyword">function</span> <span class="cm-variable">extend</span><span class="cm-operator"><</span><span class="cm-variable">First</span>, <span class="cm-variable">Second</span><span class="cm-operator">></span>(<span class="cm-variable">first</span>: <span class="cm-variable">First</span>, <span class="cm-variable">second</span>: <span class="cm-variable">Second</span>): <span class="cm-variable">First</span> <span class="cm-operator">&</span> <span class="cm-variable">Second</span> {
<span class="cm-keyword">const</span> <span class="cm-variable">result</span>: <span class="cm-variable-3">Partial</span><span class="cm-operator"><</span><span class="cm-variable">First</span> <span class="cm-operator">&</span> <span class="cm-variable">Second</span><span class="cm-operator">></span> <span class="cm-operator">=</span> {};
<span class="cm-keyword">for</span> (<span class="cm-keyword">const</span> <span class="cm-variable">prop</span> <span class="cm-keyword">in</span> <span class="cm-variable">first</span>) {
<span class="cm-keyword">if</span> (<span class="cm-variable">first</span>.<span class="cm-property">hasOwnProperty</span>(<span class="cm-variable">prop</span>)) {
(<span class="cm-variable">result</span> <span class="cm-variable">as</span> <span class="cm-variable">First</span>)[<span class="cm-variable">prop</span>] <span class="cm-operator">=</span> <span class="cm-variable">first</span>[<span class="cm-variable">prop</span>];
}
}
<span class="cm-keyword">for</span> (<span class="cm-keyword">const</span> <span class="cm-variable">prop</span> <span class="cm-keyword">in</span> <span class="cm-variable">second</span>) {
<span class="cm-keyword">if</span> (<span class="cm-variable">second</span>.<span class="cm-property">hasOwnProperty</span>(<span class="cm-variable">prop</span>)) {
(<span class="cm-variable">result</span> <span class="cm-variable">as</span> <span class="cm-variable">Second</span>)[<span class="cm-variable">prop</span>] <span class="cm-operator">=</span> <span class="cm-variable">second</span>[<span class="cm-variable">prop</span>];
}
}
<span class="cm-keyword">return</span> <span class="cm-variable">result</span> <span class="cm-variable">as</span> <span class="cm-variable">First</span> <span class="cm-operator">&</span> <span class="cm-variable">Second</span>;
}
<span class="cm-keyword">class</span> <span class="cm-variable">Person</span> {
<span class="cm-keyword">constructor</span>(<span class="cm-keyword">public</span> <span class="cm-variable">name</span>: <span class="cm-variable-3">string</span>) { }
}
<span class="cm-keyword">interface</span> <span class="cm-variable">Loggable</span> {
<span class="cm-variable">log</span>(<span class="cm-variable">name</span>: <span class="cm-variable-3">string</span>): <span class="cm-variable">void</span>;
}
<span class="cm-keyword">class</span> <span class="cm-variable">ConsoleLogger</span> <span class="cm-variable">implements</span> <span class="cm-variable">Loggable</span> {
<span class="cm-variable">log</span>(<span class="cm-variable">name</span>) {
<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-string-2">`Hello, I'm ${</span><span class="cm-variable">name</span><span class="cm-string-2">}</span><span class="cm-string-2">.`</span>);
}
}
<span class="cm-keyword">const</span> <span class="cm-variable">jim</span> <span class="cm-operator">=</span> <span class="cm-variable">extend</span>(<span class="cm-keyword">new</span> <span class="cm-variable">Person</span>(<span class="cm-string">'Jim'</span>), <span class="cm-variable">ConsoleLogger</span>.<span class="cm-property">prototype</span>);
<span class="cm-variable">jim</span>.<span class="cm-property">log</span>(<span class="cm-variable">jim</span>.<span class="cm-property">name</span>);
</code></pre>
<h1 id="유니언-타입-union-types">유니언 타입 (Union Types)</h1>
<p>유니언 타입은 교차 타입과 밀접하게 관련되어 있지만, 매우 다르게 사용됩니다.
가끔, <code>숫자</code>나 <code>문자열</code>을 매개변수로 기대하는 라이브러리를 사용할 때가 있습니다.
예를 들어, 다음 함수를 사용할 때입니다:</p>
<pre><code class="lang-ts"><span class="cm-comment">/**</span>
<span class="cm-comment"> * 문자열을 받고 왼쪽에 "padding"을 추가합니다.</span>
<span class="cm-comment"> * 만약 'padding'이 문자열이라면, 'padding'은 왼쪽에 더해질 것입니다.</span>
<span class="cm-comment"> * 만약 'padding'이 숫자라면, 그 숫자만큼의 공백이 왼쪽에 더해질 것입니다.</span>
<span class="cm-comment"> */</span>
<span class="cm-keyword">function</span> <span class="cm-variable">padLeft</span>(<span class="cm-def">value</span>: <span class="cm-variable-3">string</span>, <span class="cm-def">padding</span>: <span class="cm-variable-3">any</span>) {
<span class="cm-keyword">if</span> (<span class="cm-keyword">typeof</span> <span class="cm-variable-2">padding</span> <span class="cm-operator">===</span> <span class="cm-string">"number"</span>) {
<span class="cm-keyword">return</span> <span class="cm-variable">Array</span>(<span class="cm-variable-2">padding</span> <span class="cm-operator">+</span> <span class="cm-number">1</span>).<span class="cm-property">join</span>(<span class="cm-string">" "</span>) <span class="cm-operator">+</span> <span class="cm-variable-2">value</span>;
}
<span class="cm-keyword">if</span> (<span class="cm-keyword">typeof</span> <span class="cm-variable-2">padding</span> <span class="cm-operator">===</span> <span class="cm-string">"string"</span>) {
<span class="cm-keyword">return</span> <span class="cm-variable-2">padding</span> <span class="cm-operator">+</span> <span class="cm-variable-2">value</span>;
}
<span class="cm-keyword">throw</span> <span class="cm-keyword">new</span> <span class="cm-variable">Error</span>(<span class="cm-string-2">`Expected string or number, got '${</span><span class="cm-variable-2">padding</span><span class="cm-string-2">}</span><span class="cm-string-2">'.`</span>);
}
<span class="cm-variable">padLeft</span>(<span class="cm-string">"Hello world"</span>, <span class="cm-number">4</span>); <span class="cm-comment">// " Hello world"를 반환합니다.</span>
</code></pre>
<p><code>padLeft</code>의 문제는 매개변수 <code>padding</code>이 <code>any</code> 타입으로 되어있다는 것입니다.
즉, <code>숫자</code>나 <code>문자열</code> 둘 다 아닌 인수로 호출할 수 있다는 것이고, TypeScript는 이를 괜찮다고 받아들일 것입니다.</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">indentedString</span> <span class="cm-operator">=</span> <span class="cm-variable">padLeft</span>(<span class="cm-string">"Hello world"</span>, <span class="cm-atom">true</span>); <span class="cm-comment">// 컴파일 타임에 통과되고, 런타임에 오류.</span>
</code></pre>
<p>전통적인 객체지향 코드에서, 타입의 계층을 생성하여 두 타입을 추상화할 수 있습니다.
이는 더 명시적일 수는 있지만, 좀 과하다고 할 수도 있습니다.
전통적인 방법의 <code>padLeft</code>에서 좋은 점은 그냥 원시 값을 전달할 수 있다는 것입니다.
즉 사용법이 간단하고 간결합니다.
이 새로운 방법은 다른 곳에서 이미 존재하는 함수를 사용하려 할 때, 도움이 되지 않습니다.</p>
<p><code>any</code> 대신에, <em>유니언 타입</em>을 매개변수 <code>padding</code>에 사용할 수 있습니다:</p>
<pre><code class="lang-ts"><span class="cm-comment">/**</span>
<span class="cm-comment"> * 문자열을 받고 왼쪽에 "padding"을 추가합니다.</span>
<span class="cm-comment"> * 만약 'padding'이 문자열이라면, 'padding'은 왼쪽에 더해질 것입니다.</span>
<span class="cm-comment"> * 만약 'padding'이 숫자라면, 그 숫자만큼의 공백이 왼쪽에 더해질 것입니다.</span>
<span class="cm-comment"> */</span>
<span class="cm-keyword">function</span> <span class="cm-variable">padLeft</span>(<span class="cm-def">value</span>: <span class="cm-variable-3">string</span>, <span class="cm-def">padding</span>: <span class="cm-variable-3">string</span> <span class="cm-operator">|</span> <span class="cm-variable-3">number</span>) {
<span class="cm-comment">// ...</span>
}
<span class="cm-keyword">let</span> <span class="cm-variable">indentedString</span> <span class="cm-operator">=</span> <span class="cm-variable">padLeft</span>(<span class="cm-string">"Hello world"</span>, <span class="cm-atom">true</span>); <span class="cm-comment">// 컴파일 중에 오류</span>
</code></pre>
<p>유니언 타입은 값이 여러 타입 중 하나임을 설명합니다.
세로 막대 (<code>|</code>)로 각 타입을 구분하여 <code>number | string | boolean</code>은 값의 타입이 <code>number</code>, <code>string</code> 혹은 <code>boolean</code>이 될 수 있음을 나타냅니다.</p>
<p>유니언 타입을 값으로 가지고 있으면, 유니언에 있는 모든 타입에 공통인 멤버에만 접근할 수 있습니다.</p>
<pre><code class="lang-ts"><span class="cm-keyword">interface</span> <span class="cm-variable">Bird</span> {
<span class="cm-variable">fly</span>();
<span class="cm-variable">layEggs</span>();
}
<span class="cm-keyword">interface</span> <span class="cm-variable">Fish</span> {
<span class="cm-variable">swim</span>();
<span class="cm-variable">layEggs</span>();
}