-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTypescript introduction(Ⅰ).md
213 lines (152 loc) · 5.54 KB
/
Typescript introduction(Ⅰ).md
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
## What is Typescript
> Typescript - Javascript that scales
## Why Typescript
### Static type system
TypeScript can do static type checking at compile time. Types are erased before emitting compiled Javascript, result in zero run-time overhead to program execution.
### Scalability
It's pretty useful for large-scale system and makes code refactoring easier and under control. E.g.
```javascript
// You have no idea what parameter the greet funciton accpets without peaking into the function implementation
function greet(person) {
if (person.sex === 'male') {
return `Mr ${person.first} ${person.second}`
} else if (person.sex === 'female') {
return `Mis ${person.first} ${person.second}`
}
return `${person.first} ${person.second}`
}
```
compared to the Typescript version
```typescript
interface Person {
sex?: string
first: string
second: string
}
function greet(person: Person): string {
// ...
}
```
### IDE support - symbol based navigation + statement completion
## Starter
In Typescript, types can be associated with variables through explicit type annotations, such as
```tyepscript
let x: number;
```
or through implicit type inference, as in
```typescript
let x = 1;
```
### Types and Values
Typescript is a superset of Javascript. What works in Javascript should also work in Typescript. The addon from Typescript, as its name indicates, is `type`. In Typescript, there are `values` and `types`. TypeScript erases all `types` information before emiting JavaScript while `values` are preserved as in Javascript.
#### What results in `types`:
- A type alias declaration (type sn = number | string;)
- An interface declaration (interface I { x: number[]; })
- A class declaration (class C { })
- An enum declaration (enum E { A, B, C })
- An import declaration which refers to a type
#### What results in `values`:
- let, const, and var declarations
- A namespace or module declaration which contains a value
- An enum declaration
- A class declaration
- An import declaration which refers to a value
- A function declaration
## Basic Types
* Boolean
```typescript
let isChecked: boolean = false;
```
* Number
```typescript
let num: number = 6;
```
* String
```typescript
let color: string = "blue";
```
* Array
```typescript
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3]; // This doesnt work in JSX as `<` and `>` are used in element tag
```
* Tuple
```typescript
// Declare a tuple type
let x: [string, number];
// Initialize it
x = ["hello", 10]; // OK
// Initialize it incorrectly
x = [10, "hello"]; // Error
```
* Enum
```typescript
enum Color {Red = 1, Green, Blue}
let c: Color = Color.Green;
```
> Note that `Enum` produce both a `value` and a `type`.
```
// compiled code
var Color;
(function (Color) {
Color[Color["Red"] = 1] = "Red";
Color[Color["Green"] = 2] = "Green";
Color[Color["Blue"] = 3] = "Blue";
})(Color || (Color = {}));
var c = Color.Green;
```
* Any - try to avoid
It's useful when migrate from Javascript to Typescript.
* Void
`void` is a little like the opposite of `any`: the absence of having any type at all. You may commonly see this as the return type of functions that do not return a value:
```typescript
function warnUser(): void {
console.log("This is my warning message");
}
```
* Object:
> Note: `object` is not the same as `Object`.
* `object` is a basic type that represents the non-primitive type, i.e. any thing that is not number, string, boolean, symbol, null, or undefined.
* `Object` is a built-in interface as shown below. It is almost any type.
```
interface Object {
/** The initial value of Object.prototype.constructor is the standard built-in Object constructor. */
constructor: Function;
/** Returns a string representation of an object. */
toString(): string;
/** Returns a date converted to a string using the current locale. */
toLocaleString(): string;
/** Returns the primitive value of the specified object. */
valueOf(): Object;
/**
* Determines whether an object has a property with the specified name.
* @param v A property name.
*/
hasOwnProperty(v: PropertyKey): boolean;
/**
* Determines whether an object exists in another object's prototype chain.
* @param v Another object whose prototype chain is to be checked.
*/
isPrototypeOf(v: Object): boolean;
/**
* Determines whether a specified property is enumerable.
* @param v A property name.
*/
propertyIsEnumerable(v: PropertyKey): boolean;
}
```
Check the [demo](https://www.typescriptlang.org/play/index.html#src=function%20logWithBasicObject(obj%3A%20object)%20%7B%0D%0A%20%20%20%20console.log(obj.hasOwnProperty)%3B%0D%0A%7D%0D%0A%0D%0AlogWithBasicObject(%7Ba%3A%20'b'%7D)%0D%0AlogWithBasicObject('ab')%0D%0A%0D%0A%0D%0Afunction%20logWithInterfaceObject(obj%3A%20Object)%20%7B%0D%0A%20%20%20%20console.log(obj.hasOwnProperty)%3B%0D%0A%7D%0D%0A%0D%0AlogWithInterfaceObject(%7Ba%3A%20'b'%7D)%0D%0AlogWithInterfaceObject('ab')) for difference:
```
function logWithBasicObject(obj: object) {
console.log(obj.hasOwnProperty);
}
logWithBasicObject({a: 'b'}); // OK
logWithBasicObject('ab'); // Error: Argument of type '"ab"' is not assignable to parameter of type 'object'
function logWithInterfaceObject(obj: Object) {
console.log(obj.hasOwnProperty);
}
logWithInterfaceObject({a: 'b'}); // OK
logWithInterfaceObject('ab'); // OK
```
## Notice
* If you want to follow the latest news/articles for the series of my blogs, Please [「Watch」](https://github.com/n0ruSh/blogs/)to Subscribe.