-
Notifications
You must be signed in to change notification settings - Fork 143
/
Copy pathBasic Types.html
1167 lines (574 loc) Β· 61.9 KB
/
Basic 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="Variable Declarations.html" />
<link rel="prev" href="tutorials/React & Webpack.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 active" 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 " 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>
<ol>
<li><a href="#소개-introduction">소개 (Introduction)</a></li>
<li><a href="#불리언-boolean">불리언 (Boolean)</a></li>
<li><a href="#숫자-number">숫자 (Number)</a></li>
<li><a href="#문자열-string">문자열 (String)</a></li>
<li><a href="#배열-array">배열 (Array)</a></li>
<li><a href="#튜플-tuple">튜플 (Tuple)</a></li>
<li><a href="#열거-enum">열거 (Enum)</a></li>
<li><a href="#any">Any</a></li>
<li><a href="#void">Void</a></li>
<li><a href="#null-and-undefined">Null and Undefined</a></li>
<li><a href="#never">Never</a></li>
<li><a href="#객체-object">객체 (Object)</a></li>
<li><a href="#타입-단언-type-assertions">타입 단언 (Type assertions)</a></li>
<li><a href="#let에-관하여">'let'에 관하여</a></li>
</ol>
<h1 id="소개-introduction">소개 (Introduction)</h1>
<p>프로그램이 유용하려면 숫자, 문자열, 구조체, 불리언 값과 같은 간단한 데이터 단위가 필요합니다.
TypeScript는 JavaScript와 거의 동일한 데이터 타입을 지원하며, 열거 타입을 사용하여 더 편리하게 사용할 수 있습니다.</p>
<h1 id="불리언-boolean">불리언 (Boolean)</h1>
<p>가장 기본적인 데이터 타입은 JavaScript, TypeScript에서 <code>boolean</code> 값이라고 일컫는 참/거짓(true/false) 값입니다.</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">isDone</span>: <span class="cm-variable-3">boolean</span> <span class="cm-operator">=</span> <span class="cm-atom">false</span>;
</code></pre>
<h1 id="숫자-number">숫자 (Number)</h1>
<p>JavaScript처럼, TypeScript의 모든 숫자는 부동 소수 값입니다.
부동 소수에는 <code>number</code>라는 타입이 붙혀집니다.
TypeScript는 16진수, 10진수 리터럴에 더불어, ECMAScript 2015에 소개된 2진수, 8진수 리터럴도 지원합니다.</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">decimal</span>: <span class="cm-variable-3">number</span> <span class="cm-operator">=</span> <span class="cm-number">6</span>;
<span class="cm-keyword">let</span> <span class="cm-variable">hex</span>: <span class="cm-variable-3">number</span> <span class="cm-operator">=</span> <span class="cm-number">0xf00d</span>;
<span class="cm-keyword">let</span> <span class="cm-variable">binary</span>: <span class="cm-variable-3">number</span> <span class="cm-operator">=</span> <span class="cm-number">0</span><span class="cm-variable">b1010</span>;
<span class="cm-keyword">let</span> <span class="cm-variable">octal</span>: <span class="cm-variable-3">number</span> <span class="cm-operator">=</span> <span class="cm-number">0</span><span class="cm-variable">o744</span>;
</code></pre>
<h1 id="문자열-string">문자열 (String)</h1>
<p>웹 페이지, 서버 같은 프로그램을 JavaScript로 만들 때 기본적으로 텍스트 데이터를 다루는 작업이 필요합니다.
다른 언어들처럼, TypeScript에서는 텍스트 테이터 타입을 <code>string</code>으로 표현합니다.
JavaScript처럼 TypeScript도 큰따옴표 (<code>"</code>)나 작은따옴표 (<code>'</code>)를 문자열 데이터를 감싸는데 사용합니다.</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">color</span>: <span class="cm-variable-3">string</span> <span class="cm-operator">=</span> <span class="cm-string">"blue"</span>;
<span class="cm-variable">color</span> <span class="cm-operator">=</span> <span class="cm-string">'red'</span>;
</code></pre>
<p>또한 <em>템플릿 문자열</em> 을 사용하면 여러 줄에 걸쳐 문자열을 작성할 수 있으며, 표현식을 포함시킬 수도 있습니다.
이 문자열은 백틱/백쿼트 (<code>` </code> ) 문자로 감싸지며, <code>${ expr }</code>과 같은 형태로 표현식을 포함시킬 수 있습니다.</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">fullName</span>: <span class="cm-variable-3">string</span> <span class="cm-operator">=</span> <span class="cm-string-2">`Bob Bobbington`</span>;
<span class="cm-keyword">let</span> <span class="cm-variable">age</span>: <span class="cm-variable-3">number</span> <span class="cm-operator">=</span> <span class="cm-number">37</span>;
<span class="cm-keyword">let</span> <span class="cm-variable">sentence</span>: <span class="cm-variable-3">string</span> <span class="cm-operator">=</span> <span class="cm-string-2">`Hello, my name is ${</span> <span class="cm-variable">fullName</span> <span class="cm-string-2">}</span><span class="cm-string-2">.</span>
<span class="cm-string-2">I'll be ${</span> <span class="cm-variable">age</span> <span class="cm-operator">+</span> <span class="cm-number">1</span> <span class="cm-string-2">}</span> <span class="cm-string-2">years old next month.`</span>;
</code></pre>
<p>위는 아래 <code>sentence</code>선언과 동일합니다:</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">sentence</span>: <span class="cm-variable-3">string</span> <span class="cm-operator">=</span> <span class="cm-string">"Hello, my name is "</span> <span class="cm-operator">+</span> <span class="cm-variable">fullName</span> <span class="cm-operator">+</span> <span class="cm-string">".\n\n"</span> <span class="cm-operator">+</span>
<span class="cm-string">"I'll be "</span> <span class="cm-operator">+</span> (<span class="cm-variable">age</span> <span class="cm-operator">+</span> <span class="cm-number">1</span>) <span class="cm-operator">+</span> <span class="cm-string">" years old next month."</span>;
</code></pre>
<h1 id="배열-array">배열 (Array)</h1>
<p>TypeScript는 JavaScript처럼 값들을 배열로 다룰 수 있게 해줍니다.
배열 타입은 두 가지 방법으로 쓸 수 있습니다.
첫 번째 방법은, 배열 요소들을 나타내는 타입 뒤에 <code>[]</code>를 쓰는 것입니다:</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">list</span>: <span class="cm-variable-3">number</span>[] <span class="cm-operator">=</span> [<span class="cm-number">1</span>, <span class="cm-number">2</span>, <span class="cm-number">3</span>];
</code></pre>
<p>두 번째 방법은 제네릭 베열 타입을 쓰는 것입니다. <code>Array<elemType></code>:</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">list</span>: <span class="cm-variable-3">Array</span><span class="cm-operator"><</span><span class="cm-variable-3">number</span><span class="cm-operator">></span> <span class="cm-operator">=</span> [<span class="cm-number">1</span>, <span class="cm-number">2</span>, <span class="cm-number">3</span>];
</code></pre>
<h1 id="튜플-tuple">튜플 (Tuple)</h1>
<p>튜플 타입을 사용하면, 요소의 타입과 개수가 고정된 배열을 표현할 수 있습니다. 단 요소들의 타입이 모두 같을 필요는 없습니다. 예를 들어, <code>number</code>, <code>string</code> 이 쌍으로 있는 값을 나타내고 싶을 수 있습니다:</p>
<pre><code class="lang-ts"><span class="cm-comment">// 튜플 타입으로 선언</span>
<span class="cm-keyword">let</span> <span class="cm-variable">x</span>: [<span class="cm-variable-3">string</span>, <span class="cm-variable-3">number</span>];
<span class="cm-comment">// 초기화</span>
<span class="cm-variable">x</span> <span class="cm-operator">=</span> [<span class="cm-string">"hello"</span>, <span class="cm-number">10</span>]; <span class="cm-comment">// 성공</span>
<span class="cm-comment">// 잘못된 초기화</span>
<span class="cm-variable">x</span> <span class="cm-operator">=</span> [<span class="cm-number">10</span>, <span class="cm-string">"hello"</span>]; <span class="cm-comment">// 오류</span>
</code></pre>
<p>정해진 인덱스에 위치한 요소에 접근하면 해당 타입이 나타납니다.</p>
<pre><code class="lang-ts"><span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable">x</span>[<span class="cm-number">0</span>].<span class="cm-property">substring</span>(<span class="cm-number">1</span>)); <span class="cm-comment">// 성공</span>
<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable">x</span>[<span class="cm-number">1</span>].<span class="cm-property">substring</span>(<span class="cm-number">1</span>)); <span class="cm-comment">// 오류, 'number'에는 'substring' 이 없습니다.</span>
</code></pre>
<p>정해진 인덱스 외에 다른 인덱스에 있는 요소에 접근하면, 오류가 발생하며 실패합니다.</p>
<pre><code class="lang-ts"><span class="cm-variable">x</span>[<span class="cm-number">3</span>] <span class="cm-operator">=</span> <span class="cm-string">"world"</span>; <span class="cm-comment">// 오류, '[string, number]' 타입에는 프로퍼티 '3'이 없습니다.</span>
<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable">x</span>[<span class="cm-number">5</span>].<span class="cm-property">toString</span>()); <span class="cm-comment">// '[string, number]' 타입에는 프로퍼티 '5'가 없습니다.</span>
</code></pre>
<h1 id="열거-enum">열거 (Enum)</h1>
<p>JavaScript의 표준 자료형 집합과 사용하면 도움이 될만한 데이터 형은 <code>enum</code>입니다.
C# 같은 언어처럼, <code>enum</code>은 값의 집합에 더 나은 이름을 붙여줄 수 있습니다.</p>
<pre><code class="lang-ts"><span class="cm-variable">enum</span> <span class="cm-variable">Color</span> {<span class="cm-variable">Red</span>, <span class="cm-variable">Green</span>, <span class="cm-variable">Blue</span>}
<span class="cm-keyword">let</span> <span class="cm-variable">c</span>: <span class="cm-variable-3">Color</span> <span class="cm-operator">=</span> <span class="cm-variable">Color</span>.<span class="cm-property">Green</span>;
</code></pre>
<p>기본적으로, <code>enum</code>은 <code>0</code>부터 시작하여 멤버들의 번호를 매깁니다.
멤버 중 하나의 값을 수동으로 설정하여 번호를 바꿀 수 있습니다.
예를 들어, 위 예제를 <code>0</code>대신 <code>1</code>부터 시작해 번호를 매기도록 바꿀 수 있습니다.</p>
<pre><code class="lang-ts"><span class="cm-variable">enum</span> <span class="cm-variable">Color</span> {<span class="cm-variable">Red</span> <span class="cm-operator">=</span> <span class="cm-number">1</span>, <span class="cm-variable">Green</span>, <span class="cm-variable">Blue</span>}
<span class="cm-keyword">let</span> <span class="cm-variable">c</span>: <span class="cm-variable-3">Color</span> <span class="cm-operator">=</span> <span class="cm-variable">Color</span>.<span class="cm-property">Green</span>;
</code></pre>
<p>또는, 모든 값을 수동으로 설정할 수 있습니다:</p>
<pre><code class="lang-ts"><span class="cm-variable">enum</span> <span class="cm-variable">Color</span> {<span class="cm-variable">Red</span> <span class="cm-operator">=</span> <span class="cm-number">1</span>, <span class="cm-variable">Green</span> <span class="cm-operator">=</span> <span class="cm-number">2</span>, <span class="cm-variable">Blue</span> <span class="cm-operator">=</span> <span class="cm-number">4</span>}
<span class="cm-keyword">let</span> <span class="cm-variable">c</span>: <span class="cm-variable-3">Color</span> <span class="cm-operator">=</span> <span class="cm-variable">Color</span>.<span class="cm-property">Green</span>;
</code></pre>
<p><code>enum</code>의 유용한 기능 중 하나는 매겨진 값을 사용해 enum 멤버의 이름을 알아낼 수 있다는 것입니다.
예를 들어, 위의 예제에서 <code>2</code>라는 값이 위의 어떤 <code>Color</code> enum 멤버와 매칭되는지 알 수 없을 때, 이에 일치하는 이름을 알아낼 수 있습니다:</p>
<pre><code class="lang-ts"><span class="cm-variable">enum</span> <span class="cm-variable">Color</span> {<span class="cm-variable">Red</span> <span class="cm-operator">=</span> <span class="cm-number">1</span>, <span class="cm-variable">Green</span>, <span class="cm-variable">Blue</span>}
<span class="cm-keyword">let</span> <span class="cm-variable">colorName</span>: <span class="cm-variable-3">string</span> <span class="cm-operator">=</span> <span class="cm-variable">Color</span>[<span class="cm-number">2</span>];
<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-variable">colorName</span>); <span class="cm-comment">// 값이 2인 'Green'이 출력됩니다.</span>
</code></pre>
<h1 id="any">Any</h1>
<p>애플리케이션을 만들 때, 알지 못하는 타입을 표현해야 할 수도 있습니다.
이 값들은 동적인 콘텐츠에서 올 수도 있습니다. 예) 사용자로부터 받은 데이터. 혹은 3rd party library.
이 경우 타입 검사를 하지 않고, 그 값들이 컴파일 시간에 검사를 통과하길 원합니다.
이를 위해, <code>any</code> 타입을 사용할 수 있습니다:</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">notSure</span>: <span class="cm-variable-3">any</span> <span class="cm-operator">=</span> <span class="cm-number">4</span>;
<span class="cm-variable">notSure</span> <span class="cm-operator">=</span> <span class="cm-string">"maybe a string instead"</span>;
<span class="cm-variable">notSure</span> <span class="cm-operator">=</span> <span class="cm-atom">false</span>; <span class="cm-comment">// 성공, 분명히 부울입니다.</span>
</code></pre>
<p><code>any</code> 타입은 기존에 JavaScript로 작업할 수 있는 강력한 방법으로, 컴파일 중에 점진적으로 타입 검사를 하거나 하지 않을 수 있습니다.
혹 다른 언어에서 그렇듯, <code>Object</code>가 비슷한 역할을 할 수 있을 것 같다고 생각할 수도 있습니다.
그런데, <code>Object</code>로 선언된 변수들은 오직 어떤 값이든 그 변수에 할당할 수 있게 해주지만 실제로 메서드가 존재하더라도, 임의로 호출할 수는 없습니다:</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">notSure</span>: <span class="cm-variable-3">any</span> <span class="cm-operator">=</span> <span class="cm-number">4</span>;
<span class="cm-variable">notSure</span>.<span class="cm-property">ifItExists</span>(); <span class="cm-comment">// 성공, ifItExists 는 런타임엔 존재할 것입니다.</span>
<span class="cm-variable">notSure</span>.<span class="cm-property">toFixed</span>(); <span class="cm-comment">// 성공, toFixed는 존재합니다. (하지만 컴파일러는 검사하지 않음)</span>
<span class="cm-keyword">let</span> <span class="cm-variable">prettySure</span>: <span class="cm-variable-3">Object</span> <span class="cm-operator">=</span> <span class="cm-number">4</span>;
<span class="cm-variable">prettySure</span>.<span class="cm-property">toFixed</span>(); <span class="cm-comment">// 오류: 프로퍼티 'toFixed'는 'Object'에 존재하지 않습니다.</span>
</code></pre>
<blockquote>
<p>Note: <a href="https://www.typescriptlang.org/docs/handbook/declaration-files/do-s-and-don-ts.html#general-types" target="_blank">Do's and Don'ts</a>에 설명 했듯이 <code>Object</code>를 no-primitive <code>object</code> 대신에 사용하지 마세요.</p>
</blockquote>
<p>또한, any 타입은 타입의 일부만 알고 전체는 알지 못할 때 유용합니다.
예를 들어, 여러 다른 타입이 섞인 배열을 다룰 수 있습니다.</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">list</span>: <span class="cm-variable-3">any</span>[] <span class="cm-operator">=</span> [<span class="cm-number">1</span>, <span class="cm-atom">true</span>, <span class="cm-string">"free"</span>];
<span class="cm-variable">list</span>[<span class="cm-number">1</span>] <span class="cm-operator">=</span> <span class="cm-number">100</span>;
</code></pre>
<h1 id="void">Void</h1>
<p><code>void</code>는 어떤 타입도 존재할 수 없음을 나타내기 때문에, <code>any</code>의 반대 타입 같습니다.
<code>void</code>는 보통 함수에서 반환 값이 없을 때 반환 타입을 표현하기 위해 쓰이는 것을 볼 수 있습니다:</p>
<pre><code class="lang-ts"><span class="cm-keyword">function</span> <span class="cm-variable">warnUser</span>(): <span class="cm-variable">void</span> {
<span class="cm-variable">console</span>.<span class="cm-property">log</span>(<span class="cm-string">"This is my warning message"</span>);
}
</code></pre>
<p><code>void</code>를 타입 변수를 선언하는 것은 유용하지 않은데, 왜냐하면 그 변수에는 <code>null</code>(<code>--strictNullChecks</code>을 사용하지 않을 때만 해당, 자세한 건 다음 섹션을 참고)또는 <code>undefined</code> 만 할당할 수 있기 때문입니다:</p>
<pre><code class="lang-ts"><span class="cm-keyword">let</span> <span class="cm-variable">unusable</span>: <span class="cm-variable-3">void</span> <span class="cm-operator">=</span> <span class="cm-atom">undefined</span>;
<span class="cm-variable">unusable</span> <span class="cm-operator">=</span> <span class="cm-atom">null</span>; <span class="cm-comment">// 성공 `--strictNullChecks` 을 사용하지 않을때만</span>