OSDN Git Service

Regular updates
[twpd/master.git] / typescript.md
1 ---
2 title: TypeScript
3 category: JavaScript libraries
4 ---
5
6 TypeScript is just like ES2015 with type-checking. All ES2015 (classes, etc) should work.
7
8 ## Basic types
9
10 ```ts
11 any
12 void
13
14 boolean
15 number
16 string
17
18 null
19 undefined
20
21 string[]          /* or Array<string> */
22 [string, number]  /* tuple */
23
24 string | null | undefined   /* union */
25
26 never  /* unreachable */
27 ```
28
29 ```ts
30 enum Color {Red, Green, Blue = 4}
31 let c: Color = Color.Green
32 ```
33
34 ## Declarations
35
36 ```ts
37 let isDone: boolean
38 let isDone: boolean = false
39 ```
40
41 ```ts
42 function add (a: number, b: number): number {
43   return a + b
44 }
45
46 // Return type is optional
47 function add (a: number, b: number) { ... }
48 ```
49
50 ## Type assertions
51
52 #### Variables
53 ```ts
54 let len: number = (input as string).length
55 let len: number = (<string> input).length  /* not allowed in JSX */
56 ```
57
58 #### Functions
59 ```ts
60 function object(this: {a: number, b: number}, a: number, b: number) {
61   this.a = a;
62   this.b = b;
63   return this;
64 }
65
66 // this is used only for type declaration
67 let a = object(1,2);
68 // a has type {a: number, b: number}
69 ```
70
71
72 ## Interfaces
73
74 ### Inline
75
76 ```ts
77 function printLabel (options: { label: string }) {
78   console.log(options.label)
79 }
80
81 // Note the semicolon
82 function getUser (): { name: string; age?: number } {
83 }
84 ```
85
86 ### Explicit
87
88 ```ts
89 interface LabelOptions {
90   label: string
91 }
92
93 function printLabel(options: LabelOptions) { ... }
94 ```
95
96 ### Optional properties
97
98 ```ts
99 interface User {
100   name: string,
101   age?: number
102 }
103 ```
104
105 ### Read only
106
107 ```ts
108 interface User {
109   readonly name: string
110 }
111 ```
112
113 ### Dynamic keys
114
115 ```ts
116 {
117   [key: string]: Object[]
118 }
119 ```
120
121 ## Type aliases
122
123 ```ts
124 type Name = string | string[]
125 ```
126
127 ## Function types
128
129 ```ts
130 interface User { ... }
131
132 function getUser(callback: (user: User) => any) { callback({...}) }
133
134 getUser(function (user: User) { ... })
135 ```
136
137 ## Classes
138
139 ```ts
140 class Point {
141   x: number
142   y: number
143   static instances = 0
144   constructor(x: number, y: number) {
145     this.x = x
146     this.y = y
147   }
148 }
149 ```
150
151 #### Inheritance
152
153 ```ts
154 class Point {...}
155
156 class Point3D extends Point {...}
157
158 interface Colored {...}
159
160 class Pixel extends Point implements Colored {...}
161 ```
162
163 #### Short fields initialisation
164
165 ```ts
166 class Point {
167   static instances = 0;
168   constructor(
169     public x: number,
170     public y: number,
171   ){}
172 }
173 ```
174
175 #### Fields which do not require initialisation
176 ```ts
177 class Point {
178   public someUselessValue!: number;
179   ...
180 }
181 ```
182
183 ## Generics
184
185 ```ts
186 class Greeter<T> {
187   greeting: T
188   constructor(message: T) {
189     this.greeting = message
190   }
191 }
192
193 let greeter = new Greeter<string>('Hello, world')
194 ```
195
196 ## Modules
197
198 ```ts
199 export interface User { ... }
200 ```
201
202 ## Type extraction
203
204 ```ts
205 interface Building {
206   room: {
207     door: string,
208     walls: string[],
209   };
210 }
211
212 type Walls = Building['room']['walls']; // string[]
213 ```