sync from local backup
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
# TypeScript 基础
|
||||
|
||||
## 学习目标
|
||||
- 理解 TypeScript 的优势
|
||||
- 掌握类型注解
|
||||
- 学会接口和泛型
|
||||
|
||||
## TypeScript 简介
|
||||
|
||||
TypeScript 是 JavaScript 的超集,添加了静态类型系统。
|
||||
|
||||
## 类型注解
|
||||
|
||||
```typescript
|
||||
let name: string = "Tom"
|
||||
let age: number = 25
|
||||
let isActive: boolean = true
|
||||
let hobbies: string[] = ["coding", "reading"]
|
||||
```
|
||||
|
||||
## 接口
|
||||
|
||||
```typescript
|
||||
interface User {
|
||||
id: number
|
||||
name: string
|
||||
email?: string // 可选属性
|
||||
}
|
||||
|
||||
function printUser(user: User) {
|
||||
console.log(user.name)
|
||||
}
|
||||
```
|
||||
|
||||
## 泛型
|
||||
|
||||
```typescript
|
||||
function identity<T>(arg: T): T {
|
||||
return arg
|
||||
}
|
||||
|
||||
const result = identity<string>("Hello")
|
||||
```
|
||||
|
||||
## 动手练习
|
||||
|
||||
1. 为变量添加类型注解
|
||||
2. 定义用户接口
|
||||
3. 使用泛型创建通用函数
|
||||
Reference in New Issue
Block a user