TypeScript 基础
如果你熟悉 JavaScript,TypeScript 主要多了类型系统。以下是本项目用到的关键概念。
类型基础
1
2
3
4
5
6
7
8
9
10
11
12
13 | // 基本类型
let name: string = "hello";
let count: number = 42;
let flag: boolean = true;
// 接口
interface Message {
role: "user" | "assistant"; // 字面量联合类型
content: string;
}
// 类型别名
type ContentBlock = TextBlock | ToolUseBlock; // 联合类型
|
异步编程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | // async/await
async function fetchData(url: string): Promise<string> {
const response = await fetch(url);
return response.text();
}
// AsyncGenerator — 本项目的核心模式
async function* countdown(n: number): AsyncGenerator<number> {
while (n > 0) {
yield n; // 每次 yield 一个值
n--;
}
}
// 消费 AsyncGenerator
for await (const num of countdown(5)) {
console.log(num); // 5, 4, 3, 2, 1
}
|
推荐资源