@@ -5,59 +5,59 @@ permalink: /docs/handbook/typescript-in-5-minutes-func.html
5
5
oneline : Learn TypeScript if you have a background in functional programming
6
6
---
7
7
8
- TypeScript began its life as an attempt to bring traditional object-oriented types
9
- to JavaScript so that the programmers at Microsoft could bring
10
- traditional object-oriented programs to the web. As it has developed, TypeScript's type
11
- system has evolved to model code written by native JavaScripters. The
12
- resulting system is powerful, interesting and messy .
8
+ TypeScriptλ μΉμ μ ν΅μ μΈ κ°μ²΄ μ§ν₯ νλ‘κ·Έλ¨λ₯Ό κ°μ Έμ€κΈ° μν΄μ
9
+ λ§μ΄ν¬λ‘μννΈ νλ‘κ·Έλλ¨Έλ€μ΄ JavaScriptμ μ ν΅μ μΈ κ°μ²΄ μ§ν₯ νμ
μ
10
+ κ°μ Έμ€κΈ° μν μλλ‘ μμλμμ΅λλ€. κ°λ°λμ΄ κ°λ©΄μ TypeScriptμ
11
+ νμ
μμ€ν
μ λ€μ΄ν°λΈ μλ°μ€ν¬λ¦½ν°κ° μμ±ν λͺ¨λΈ μ½λλ‘ λ°μ λμμ΅λλ€.
12
+ κ²°κ³Όμ μΈ μμ€ν
μ κ°λ ₯νλ©΄μ ν₯λ―Έλ‘κ³ μ§μ λΆν©λλ€ .
13
13
14
- This introduction is designed for working Haskell or ML programmers
15
- who want to learn TypeScript. It describes how the type system of
16
- TypeScript differs from Haskell's type system. It also describes
17
- unique features of TypeScript's type system that arise from its
18
- modelling of JavaScript code .
14
+ μ΄ μκ°λ TypeScriptλ₯Ό λ°°μ°κ³ μ νλ Haskell λλ ML νλ‘κ·Έλλ¨Έλ₯Ό
15
+ μν΄ λ§λ€μ΄μ‘μ΅λλ€. Haskell νμ
μμ€ν
κ³Ό TypeScript νμ
μμ€ν
μ
16
+ μ°¨μ΄λ₯Ό μ€λͺ
ν©λλ€.
17
+ λν JavaScript μ½λμ λͺ¨λΈλ§μμ λ°μνλ TypeScript νμ
μμ€ν
μ
18
+ λ
νΉν νΉμ§μ μ€λͺ
ν©λλ€ .
19
19
20
- This introduction does not cover object-oriented programming. In
21
- practice, object-oriented programs in TypeScript are similar to those
22
- in other popular languages with OO features .
20
+ μ΄ μκ°μμλ κ°μ²΄ μ§ν₯ νλ‘κ·Έλλ°μ λ€λ£¨μ§ μμ΅λλ€.
21
+ μ€μ λ‘, TypeScriptμ κ°μ²΄ μ§ν₯ νλ‘κ·Έλ¨μ OO κΈ°λ₯μ΄ μλ λ€λ₯Έ μΈκΈ° μΈμ΄μ
22
+ νλ‘κ·Έλ¨κ³Ό μ μ¬ν©λλ€ .
23
23
24
- # Prerequisites
24
+ # μ μ 쑰건 ( Prerequisites)
25
25
26
- In this introduction, I assume you know the following :
26
+ λ³Έ μλ‘ μμλ λ€μ μ¬νμ μκ³ μλ€κ³ κ°μ ν©λλ€ :
27
27
28
- * How to program in JavaScript, the good parts .
29
- * Type syntax of a C-descended language .
28
+ * JavaScriptλ‘ νλ‘κ·Έλλ° νκΈ° μν ν΅μ¬ κ°λ
.
29
+ * C κ³μ΄ μΈμ΄μ νμ
ꡬ문 .
30
30
31
- If you need to learn the good parts of JavaScript, read
32
- [ JavaScript: The Good Parts] ( http://shop.oreilly.com/product/9780596517748.do ) .
33
- You may be able to skip the book if you know how to write programs in
34
- a call-by-value lexically scoped language with lots of mutability and
35
- not much else .
36
- [ R<sup >4</sup >RS Scheme] ( https://people.csail.mit.edu/jaffer/r4rs.pdf ) is a good example .
31
+ JavaScriptμ ν΅μ¬ κ°λ
μ λ°°μ°κ³ μΆλ€λ©΄
32
+ [ JavaScript: The Good Parts] ( http://shop.oreilly.com/product/9780596517748.do ) λ₯Ό μΆμ²ν©λλ€ .
33
+ λ§μ κ°λ³μ±μ κ°μ§ call-by-value λ μ컬ν μ€μ½ν μΈμ΄λ‘
34
+ νλ‘κ·Έλ¨μ μμ±νλ λ°©λ²μ μκ³ μλ€λ©΄ κ΅³μ΄ μ±
μ μ μ½μ΄λ
35
+ μκ΄μμ΅λλ€ .
36
+ [ R<sup >4</sup >RS Scheme] ( https://people.csail.mit.edu/jaffer/r4rs.pdf ) κ° μ’μ μμ
λλ€ .
37
37
38
- [ The C++ Programming Language ] ( http://www.stroustrup.com/4th.html ) is
39
- a good place to learn about C-style type syntax. Unlike C++,
40
- TypeScript uses postfix types, like so : ` x: string` instead of ` string x ` .
38
+ [ C++ νλ‘κ·Έλλ° μΈμ΄ ] ( http://www.stroustrup.com/4th.html ) λ
39
+ C-μ€νμΌ νμ
ꡬ문μ λν΄μ λ°°μ°κΈ° μ’μ΅λλ€.
40
+ C++ λ¬λ¦¬ TypeScriptλ νμ νμ
μ μ¬μ©ν©λλ€, μλ₯Ό λ€λ©΄ : ` string x ` λμ μ ` x: string ` .
41
41
42
- # Concepts not in Haskell
42
+ # Haskellμλ μλ κ°λ
( Concepts not in Haskell)
43
43
44
- ## Built-in types
44
+ ## λ΄μ₯ νμ
( Built-in types)
45
45
46
- JavaScript defines 7 built-in types :
46
+ JavaScriptμμλ 7κ°μ λ΄μ₯ νμ
μ μ μν©λλ€ :
47
47
48
- | Type | Explanation |
48
+ | νμ
| μ€λͺ
|
49
49
| ----------- | ------------------------------------------- |
50
- | ` Number ` | a double-precision IEEE 754 floating point. |
51
- | ` String ` | an immutable UTF-16 string. |
52
- | ` Boolean ` | ` true ` and ` false ` . |
53
- | ` Symbol ` | a unique value usually used as a key. |
54
- | ` Null ` | equivalent to the unit type. |
55
- | ` Undefined ` | also equivalent to the unit type. |
56
- | ` Object ` | similar to records. |
50
+ | ` Number ` | λ°°μ λ°λ IEEE 754 λΆλμμμ . |
51
+ | ` String ` | μμ λΆκ°λ₯ν UTF-16 λ¬Έμμ΄. |
52
+ | ` Boolean ` | ` true ` μ ` false ` . |
53
+ | ` Symbol ` | λ³΄ν΅ ν€λ‘ μ¬μ©νλ κ³ μ ν κ°. |
54
+ | ` Null ` | λ¨μ νμ
κ³Ό λλ±. |
55
+ | ` Undefined ` | λν λ¨μ νμ
κ³Ό λλ±. |
56
+ | ` Object ` | λ μ½λμ μ μ¬ν κ². |
57
57
58
- [ See the MDN page for more detail ] ( https://developer.mozilla.org/docs/Web/JavaScript/Data_structures ) .
58
+ [ μμΈν λ΄μ©μ MDN νμ΄μ§λ₯Ό μ°Έκ³ νμΈμ ] ( https://developer.mozilla.org/docs/Web/JavaScript/Data_structures ) .
59
59
60
- TypeScript has corresponding primitive types for the built-in types :
60
+ TypeScriptμλ κΈ°λ³Έ λ΄μ₯λ νμ
μ ν΄λΉνλ μμ νμ
μ΄ μμ΅λλ€ :
61
61
62
62
* ` number `
63
63
* ` string `
@@ -67,84 +67,84 @@ TypeScript has corresponding primitive types for the built-in types:
67
67
* ` undefined `
68
68
* ` object `
69
69
70
- ### Other important TypeScript types
70
+ ### λ€λ₯Έ μ€μν TypeScript νμ
( Other important TypeScript types)
71
71
72
- | Type | Explanation |
72
+ | νμ
| μ€λͺ
|
73
73
| -------------- | ----------------------------------------------------------- |
74
- | ` unknown ` | the top type. |
75
- | ` never ` | the bottom type. |
76
- | object literal | eg ` { property: Type } ` |
77
- | ` void ` | a subtype of ` undefined ` intended for use as a return type. |
78
- | ` T[] ` | mutable arrays, also written ` Array<T> ` |
79
- | ` [T, T] ` | tuples, which are fixed-length but mutable |
80
- | ` (t: T) => U ` | functions |
74
+ | ` unknown ` | μ΅μμ νμ
. |
75
+ | ` never ` | νμ νμ
. |
76
+ | κ°μ²΄ 리ν°λ΄ | μ, ` { property: Type } ` |
77
+ | ` void ` | λ¦¬ν΄ νμ
μΌλ‘ μ¬μ©νκΈ° μν΄ μλλ ` undefined ` μ μλΈνμ
. |
78
+ | ` T[] ` | μμ κ°λ₯ν λ°°μ΄λ€, λν ` Array<T> ` μΌλ‘ μ¬μ©κ°λ₯ |
79
+ | ` [T, T] ` | κ³ μ λ κΈΈμ΄μ§λ§ μμ κ°λ₯ν νν |
80
+ | ` (t: T) => U ` | ν¨μ |
81
81
82
- Notes :
82
+ μ μνμΈμ :
83
83
84
- 1 . Function syntax includes parameter names. This is pretty hard to get used to !
84
+ 1 . ν¨μ ꡬ문μλ 맀κ°λ³μ μ΄λ¦μ΄ ν¬ν¨λμ΄ μμ΅λλ€. μ΅μν΄μ§κΈ° κ½€ μ΄λ ΅μ΅λλ€ !
85
85
86
86
``` ts
87
87
let fst: (a : any , d : any ) => any = (a , d ) => a ;
88
- // or more precisely :
88
+ // λλ μ’ λ μ ννκ² λ§νμλ©΄ :
89
89
let snd: <T , U >(a : T , d : U ) => U = (a , d ) => d ;
90
90
```
91
91
92
- 2 . Object literal type syntax closely mirrors object literal value syntax :
92
+ 2 . κ°μ²΄ 리ν°λ΄ νμ
κ΅¬λ¬Έμ΄ κ°μ²΄ 리ν°λ΄ κ° κ΅¬λ¬Έκ³Ό κ½€ μ μ¬ν©λλ€ :
93
93
94
94
``` ts
95
95
let o: { n: number ; xs: object [] } = { n: 1 , xs: [] };
96
96
```
97
97
98
- 3 . ` [T, T] ` is a subtype of ` T[] ` . This is different than Haskell, where tuples are not related to lists .
98
+ 3 . ` [T, T] ` λ ` T[] ` μ μλΈνμ
μ
λλ€. Haskellκ³Ό λ¬λ¦¬, ννμ 리μ€νΈμ κ΄λ ¨μ΄ μμ΅λλ€ .
99
99
100
- ### Boxed types
100
+ ### λ°μ€ νν νμ
( Boxed types)
101
101
102
- JavaScript has boxed equivalents of primitive types that contain the
103
- methods that programmers associate with those types. TypeScript
104
- reflects this with, for example, the difference between the primitive
105
- type ` number ` and the boxed type ` Number ` . The boxed types are rarely
106
- needed, since their methods return primitives .
102
+ JavaScriptλ νλ‘κ·Έλλ¨Έλ€μ΄ ν΄λΉ νμ
μ μ κ·Όν μ μλ λ©μλλ₯Ό
103
+ ν¬ν¨νλ μμνμ
μ λλ±νκ² λ°μ€ν΄ μμ΅λλ€. μλ₯Ό λ€λ©΄, μμ ννμ
104
+ ` number ` κ³Ό λ°μ€ νν νμ
μ ` Number ` μ λ€λ₯Έ μ μ TypeScriptλ
105
+ λ°μν΄μμ΅λλ€.
106
+ λ°μ€ νν νμ
μ λ©μλκ° μμ νμ
μ λ°νν λ μμ£Ό λλ¬Όκ² νμν©λλ€ .
107
107
108
108
``` ts
109
109
(1 ).toExponential ();
110
- // equivalent to
110
+ // λλ±νκ²
111
111
Number .prototype .toExponential .call (1 );
112
112
```
113
113
114
- Note that calling a method on a numeric literal requires it to be in
115
- parentheses to aid the parser .
114
+ μ«μ 리ν°λ΄μμ λ©μλλ₯Ό νΈμΆνλ €λ©΄ νμλ₯Ό μ§μνκΈ° μν΄ λ©μλλ₯Ό κ΄νΈ
115
+ μμ λ£μ΄μΌ νλ€λ μ μ μ μνμμμ€ .
116
116
117
- ## Gradual typing
117
+ ## μ μ§μ μΈ νμ΄ν ( Gradual typing)
118
118
119
- TypeScript uses the type ` any ` whenever it can't tell what the type of
120
- an expression should be. Compared to ` Dynamic ` , calling ` any ` a type
121
- is an overstatement. It just turns off the type checker
122
- wherever it appears. For example, you can push any value into an
123
- ` any[] ` without marking the value in any way :
119
+ TypeScriptλ ννμμ νμ
μ μ μ μμ λλ§λ€ ` any ` νμ
μ
120
+ μ¬μ©ν©λλ€. ` Dynamic ` μ λΉκ΅νλ©΄, ` any ` λ νμ
μ΄λΌκ³ λΆλ₯΄κΈ°μ
121
+ κ³Όνλ€κ³ ν μλ μμ΅λλ€.
122
+ μ΄ νμ
μ΄ λνλ λλ§λ€ νμ
μ 체ν¬νμ§ μμ΅λλ€. μλ₯Ό λ€μ΄, ` any[] `
123
+ μ μ΄λ€ κ°μ΄λ 체ν¬νμ§ μκ³ λ£μ΄λ μκ΄μμ΅λλ€ :
124
124
125
- ``` ts twoslash
126
- // with "noImplicitAny": false in tsconfig.json , anys: any[]
125
+ ``` ts
126
+ // tsconfig.json νμΌμ "noImplicitAny": false λ₯Ό μ½μ
, anys: any[]
127
127
const anys = [];
128
128
anys .push (1 );
129
129
anys .push (" oh no" );
130
130
anys .push ({ anything: " goes" });
131
131
```
132
132
133
- And you can use an expression of type ` any ` anywhere :
133
+ κ·Έλ¦¬κ³ ` any ` νμ
μ μ΄λμλ κ°μ μ¬μ©κ°λ₯ν©λλ€ :
134
134
135
135
``` ts
136
- anys .map (anys [1 ]); // oh no , "oh no" is not a function
136
+ anys .map (anys [1 ]); // μ€ μλμ£ , "oh no" ν¨μκ° μλλλ€.
137
137
```
138
138
139
- ` any ` is contagious, too &mdash ; if you initialise a variable with an
140
- expression of type ` any ` , the variable has type ` any ` too .
139
+ ` any ` μ μΌλ μ μλλ°, μμ &mdash ; λ§μ½μ ` any ` νμ
μ ννμκ³Ό ν¨κ» λ³μλ₯Ό μ΄κΈ°ννλ©΄,
140
+ λ³μ μμ ` any ` νμ
μ κ°μ§λλ€ .
141
141
142
142
``` ts
143
- let sepsis = anys [0 ] + anys [1 ]; // this could mean anything
143
+ let sepsis = anys [0 ] + anys [1 ]; // μ΄λ€ μλ―Έλ‘λ κ°λ₯ν©λλ€.
144
144
```
145
145
146
- To get an error when TypeScript produces an ` any ` , use
147
- ` "noImplicitAny": true ` , or ` "strict": true ` in ` tsconfig.json ` .
146
+ TypeScriptλ ` any ` λ₯Ό μ 곡ν λ μλ¬κ° λ°μλλ©΄,
147
+ ` tsconfig.json ` μμ ` "noImplicitAny": true ` λλ ` "strict": true ` λ₯Ό μ€μ ν΄μΌ ν©λλ€ .
148
148
149
149
## ꡬ쑰μ μΈ νμ΄ν (Structural typing)
150
150
0 commit comments