Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c4966cb
Deploy to GitHub Pages setup (#95)
gibbok Jan 18, 2024
9f00987
Fix links in Astro (#96)
gibbok Jan 18, 2024
70ff24f
Gix publish on GitHub pages (#97)
gibbok Jan 18, 2024
f424443
Fix formatting in mds (#98)
gibbok Jan 18, 2024
97aa5c7
Generated books (#99)
gibbok Jan 18, 2024
c3f8b1a
Add links to the website (#100)
gibbok Jan 18, 2024
b3ad641
Minor improvements to split page script (#101)
gibbok Jan 18, 2024
ae9149e
Add more checks in the pipeline (#102)
gibbok Jan 18, 2024
a2e07b6
Fix heading (#103)
gibbok Jan 18, 2024
88df63f
Verification (#104)
gibbok Jan 24, 2024
fee22f3
Revert "Verification" (#105)
gibbok Jan 24, 2024
1e49a56
Minor changes to introduction and Update make file with preview websi…
gibbok May 27, 2024
1495c68
Fix invalid epubs (#111)
gibbok May 28, 2024
e5cfd0d
add epub validation to ebooks (#112)
gibbok May 28, 2024
01f05fb
Create PDF books from Markdown files (#113)
gibbok May 29, 2024
3a1a351
Add PDFs generation with Table Of Content (#114)
gibbok May 30, 2024
3d34432
Add donation paragraph in CN version (#115)
gibbok May 30, 2024
ac90c7f
Updates for TypeScript 5.3 and 5.4 (#116)
gibbok Jun 2, 2024
edd5b68
Add more information on `declare` keyword (#117)
gibbok Jun 4, 2024
291a422
Social network minor update (#118)
gibbok Oct 15, 2024
feb2cb8
Fix missing full stop (#119)
gibbok Jul 14, 2025
0bd86b4
Updated the 'Type From Value' section to accurately provide an explan…
Nov 2, 2025
7792ada
Wrapped comment lines in 'Type From Value' section
Nov 2, 2025
f5c9e2d
Shortened example to just use const x
Nov 3, 2025
82d0fa3
Updated "Type From Value" section
gibbok Nov 3, 2025
fcc1990
Fixed gramatical issue in the 'Utility Types' -> 'Awaited' section
Nov 4, 2025
e7ab1fc
Fixed gramatical issue from 'Interators' to 'Iterators'
Nov 4, 2025
c26680e
Fixed gramatical issue in the 'Utility Types' -> 'Awaited' section
gibbok Nov 4, 2025
d172cc4
Chines version minor updates (#124)
gibbok Nov 15, 2025
6a0f721
Add Cloudflare Privacy-first analytics (#125)
gibbok Nov 15, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/lint.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
set -e
echo "Linting all Markdown content ..."
echo "Linting Markdown and Code files ..."
cd tools
npm ci
npm run lint
npm run lint
npm run lint:md
97 changes: 76 additions & 21 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,32 @@

本书完全免费且开源。

如果您发现这本 TypeScript 书籍很有价值并希望做出贡献,请考虑通过 PayPal 支持我的努力。谢谢!

[![Donate](https://img.shields.io/badge/Donate-PayPal-green.svg)](https://www.paypal.com/donate/?business=QW82ZS956XLFY&no_recurring=0&currency_code=EUR)

## 翻译

本书已被翻译成多种语言版本,包括:

* [中文](https://github.com/gibbok/typescript-book/blob/main/README-zh_CN.md)
[中文](https://github.com/gibbok/typescript-book/blob/main/README-zh_CN.md)

## 下载
## 下载和网站

您还可以在这里下载 Epub 版本:
您还可以下载 Epub 版本:

[https://github.com/gibbok/typescript-book/tree/main/downloads](https://github.com/gibbok/typescript-book/tree/main/downloads)

在线版本可在以下位置获得:

[https://gibbok.github.io/typescript-book](https://gibbok.github.io/typescript-book)

## 目录表

<!-- markdownlint-disable MD004 -->
- [简洁的TypeScript之书](#简洁的typescript之书)
- [翻译](#翻译)
- [下载](#下载)
- [下载和网站](#下载和网站)
- [目录表](#目录表)
- [介绍](#介绍)
- [关于作者](#关于作者)
Expand All @@ -34,7 +42,7 @@
- [TypeScript 入门](#typescript-入门)
- [安装](#安装)
- [配置](#配置)
- [TypeScript 的配置文件 tsconfig.json](#typescript-的配置文件-tsconfigjson)
- [TypeScript 的配置文件](#typescript-的配置文件)
- [target](#target)
- [lib](#lib)
- [strict](#strict)
Expand Down Expand Up @@ -195,6 +203,7 @@
- [Lowercase\<T\>](#lowercaset)
- [Capitalize\<T\>](#capitalizet)
- [Uncapitalize\<T\>](#uncapitalizet)
- [NoInfer\<T\>](#noinfert)
- [其他](#其他)
- [错误和异常处理](#错误和异常处理)
- [混合类](#混合类)
Expand All @@ -206,17 +215,17 @@
- [ES6 模块](#es6-模块)
- [ES7 求幂运算符](#es7-求幂运算符)
- [for-await-of 语句](#for-await-of-语句)
- [New.target](#newtarget)
- [New target 元属性](#new-target-元属性)
- [动态导入表达式](#动态导入表达式)
- ["tsc –watch"](#tsc-watch)
- [默认声明](#默认声明)
- [可选链](#可选链)
- [空合并运算符 (??)](#空合并运算符-)
- [空合并运算符](#空合并运算符)
- [模板字符串类型](#模板字符串类型)
- [函数重载](#函数重载)
- [递归类型](#递归类型)
- [递归条件类型](#递归条件类型)
- [Node.js 中的 ECMAScript 模块支持](#nodejs-中的-ecmascript-模块支持)
- [Node 中的 ECMAScript 模块支持](#node-中的-ecmascript-模块支持)
- [断言函数](#断言函数)
- [可变参数元组类型](#可变参数元组类型)
- [装箱类型](#装箱类型)
Expand All @@ -227,6 +236,7 @@
- [仅类型导入和导出](#仅类型导入和导出)
- [使用声明和显式资源管理](#使用声明和显式资源管理)
- [使用声明等待](#使用声明等待)
- [导入属性](#导入属性)
<!-- markdownlint-enable MD004 -->

## 介绍
Expand All @@ -243,7 +253,7 @@ Simone Poggiali 是一位经验丰富的高级前端开发人员,自 90 年代

* 领英: [https://www.linkedin.com/in/simone-poggiali](https://www.linkedin.com/in/simone-poggiali)
* GitHub: [https://github.com/gibbok](https://github.com/gibbok)
* 推特: [https://twitter.com/gibbok_coding](https://twitter.com/gibbok_coding)
* X.com: [https://x.com/gibbok_coding](https://x.com/gibbok_coding)
* 电子邮箱: gibbok.coding📧gmail.com

## TypeScript简介
Expand All @@ -252,7 +262,7 @@ Simone Poggiali 是一位经验丰富的高级前端开发人员,自 90 年代

TypeScript 是一种基于 JavaScript 构建的强类型编程语言。它最初由 Anders Hejlsberg 于 2012 年设计,目前由 Microsoft 作为开源项目开发和维护。

TypeScript 编译为 JavaScript,并且可以在任何 JavaScript 引擎(例如浏览器或服务器 Node.js)中执行。
TypeScript 编译为 JavaScript,并且可以在任何 JavaScript 运行时(例如浏览器或服务器 Node.js)中执行。

TypeScript 支持多种编程范式,例如函数式、泛型、命令式和面向对象。TypeScript 既不是解释型语言,也不是编译型语言。

Expand Down Expand Up @@ -284,7 +294,7 @@ TypeScript是用`.ts`或`.tsx`文件编写的, 而JavaScript是用`.js`或`.jsx`
const sum = (a, b) => a + b;
```

该函数可以通过将文件扩展名更改为 .TypeScript 来转换和使用.ts。但是,如果同一个函数使用 TypeScript 类型进行注释,则未经编译就无法在任何 JavaScript 引擎中执行。如果未编译以下 TypeScript 代码,将会产生语法错误
该函数可以通过将文件扩展名更改为 .TypeScript 来转换和使用.ts。但是,如果同一个函数使用 TypeScript 类型进行注释,则未经编译就无法在任何 JavaScript 运行时中执行。如果未编译以下 TypeScript 代码,将会产生语法错误

<!-- skip -->
```typescript
Expand Down Expand Up @@ -328,7 +338,7 @@ const result = 1 + true; // 在JavaScript中, 结果等于2

### TypeScript 代码生成

TypeScript 编译器有两个主要职责:检查类型错误和编译为 JavaScript。这两个过程是相互独立的。类型不会影响 JavaScript 引擎中代码的执行,因为它们在编译过程中会被完全擦除。即使存在类型错误,TypeScript 仍然可以输出 JavaScript。以下是存在类型错误的 TypeScript 代码示例:
TypeScript 编译器有两个主要职责:检查类型错误和编译为 JavaScript。这两个过程是相互独立的。类型不会影响 JavaScript 运行时中代码的执行,因为它们在编译过程中会被完全擦除。即使存在类型错误,TypeScript 仍然可以输出 JavaScript。以下是存在类型错误的 TypeScript 代码示例:

<!-- skip -->
```typescript
Expand Down Expand Up @@ -523,7 +533,7 @@ tsc src/*.ts // 将 'src' 文件夹下任意的 .ts 文件编译成 JavaScript
tsc app.ts util.ts --outfile index.js // 将 2 个 TypeScript 文件 (app.ts 和 util.ts) 编译成 1 个 JavaScript 文件 (index.js)
```

### TypeScript 的配置文件 tsconfig.json
### TypeScript 的配置文件

tsconfig.json 文件用于配置 TypeScript 编译器 (tsc)。通常,它与文件一起添加到项目的根目录中package.json。

Expand Down Expand Up @@ -590,6 +600,7 @@ TypeScript 可以为各种模块系统生成代码,包括 UMD、System、ESNex
<!-- markdownlint-disable MD049 -->
"include"属性向编译器指示我们想要包含的文件列表。此属性允许类似 glob 的模式,例如 "\*_" 表示任何子目录,"_" 表示任何文件名,"?" 表示可选字符。
<!-- markdownlint-enable MD049 -->

#### exclude

"exclude"属性向编译器指示不应包含在编译中的文件列表。这可以包括"node_modules"等文件或测试文件
Expand Down Expand Up @@ -1129,6 +1140,8 @@ npm install --save-dev @types/library-name

即使在 JavaScript 文件中,您也可以通过 `// @ts-check` 使用环境声明。

`declare` 关键字可以为现有的 JavaScript 代码启用类型定义,而无需导入它,作为来自另一个文件或全局的类型的占位符。

### 属性检测和多余属性检测

TypeScript 基于结构类型系统,但过多的属性检查是 TypeScript 的一个属性,它允许它检查对象是否具有类型中指定的确切属性。
Expand Down Expand Up @@ -1285,7 +1298,7 @@ TypeScript 提供了控制加宽过程的方法,例如使用"const"。
For example:

```typescript
const x = 'x'; // TypeScript 将 x 的类型推断为 'x',一种较窄的类型
const x = 'x'; // TypeScript 将 'x' 推断为带有 'const'(不可变)的字符串字面量,但将其扩展为带有 'let'(可重新赋值)的 'string'。
let y: 'y' | 'x' = 'y';
y = x; // 有效: x的类型推断为 'x'
```
Expand Down Expand Up @@ -1698,7 +1711,6 @@ interface B extends A {
}
```


## 字面量类型

文字类型是来自集体类型的单个元素集,它定义了一个非常精确的值,即 JavaScript 原始数据。
Expand Down Expand Up @@ -1765,7 +1777,6 @@ let o = {
};
```


## 严格空检查

`strictNullChecks` 是一个 TypeScript 编译器选项,强制执行严格的 null 检查。启用此选项后,只有在变量和参数已使用联合类型 `null` | `undefined` 显式声明为该类型时,才可以对其进行赋值`null` 或者 `undefined`。如果变量或参数未显式声明为可为空,TypeScript 将生成错误以防止潜在的运行时错误。
Expand Down Expand Up @@ -3749,7 +3760,7 @@ type MyType = MyTuple[2]; // boolean

#### Awaited\<T\>

构造一个递归解包 Promise 的类型
构造一个递归解包 Promise 类型的类型

```typescript
type A = Awaited<Promise<string>>; // string
Expand Down Expand Up @@ -4002,6 +4013,32 @@ type MyType = Capitalize<'abc'>; // "Abc"
type MyType = Uncapitalize<'Abc'>; // "abc"
```

#### NoInfer\<T\>

NoInfer 是一种实用类型,旨在阻止泛型函数范围内类型的自动推断。

示例:

```typescript
// 泛型函数范围内类型的自动推断。
function fn<T extends string>(x: T[], y: T) {
return x.concat(y);
}
const r = fn(['a', 'b'], 'c'); // 此处的类型为 ("a" | "b" | "c")[]
```

使用 NoInfer:

<!-- skip -->
```typescript
// 使用 NoInfer 阻止类型推断的示例函数
function fn2<T extends string>(x: T[], y: NoInfer<T>) {
return x.concat(y);
}

const r2 = fn2(["a", "b"], "c"); // 错误:类型为“c”的类型参数不能分配给类型为“a”|“b”的参数。
```

## 其他

### 错误和异常处理
Expand Down Expand Up @@ -4137,7 +4174,7 @@ WebSocket:

### 迭代器和生成器

TypeScript 很好地支持交互器和生成器
TypeScript 对迭代器和生成器都提供了很好的支持

迭代器是实现迭代器协议的对象,提供了一种逐个访问集合或序列元素的方法。它是一个包含指向迭代中下一个元素的指针的结构。他们有一个 `next()` 方法返回序列中的下一个值以及指示序列是否为 的布尔值 `done` 。

Expand Down Expand Up @@ -4300,7 +4337,7 @@ async function* asyncNumbers(): AsyncIterableIterator<number> {
})();
```

### New.target
### New target 元属性

您可以在 TypeScript 中使用 `new.target` 元属性,该属性使您能够确定是否使用 new 运算符调用函数或构造函数。它允许您检测对象是否是由于构造函数调用而创建的。

Expand Down Expand Up @@ -4383,7 +4420,7 @@ const person: Person = {
console.log(person.address?.city); // undefined
```

### 空合并运算符 (??)
### 空合并运算符

如果 `??` 左侧是 `null` 或者 `undefined` ,则空合并运算符返回右侧值,否则,它返回左侧值。

Expand Down Expand Up @@ -4474,7 +4511,7 @@ type NestedArray = [1, [2, [3, 4], 5], 6];
type FlattenedArray = Flatten<NestedArray>; // 2 | 3 | 4 | 5 | 1 | 6
```

### Node.js 中的 ECMAScript 模块支持
### Node 中的 ECMAScript 模块支持

Node.js 从 15.3.0 版本开始添加了对 ECMAScript 模块的支持,而 TypeScript 从 4.7 版本开始增加了对 Node.js 的 ECMAScript 模块支持。可以通过将 `tsconfig.json` 文件中的`module`属性的值设置为 `nodenext` 来启用此支持。这是一个例子:

Expand Down Expand Up @@ -4912,3 +4949,21 @@ Connection closed.
```

语句中允许使用"using"和"await using"声明:"for"、"for-in"、"for-of"、"for-await-of"、"switch"。

### 导入属性

TypeScript 5.3 的导入属性(导入标签)告诉运行时如何处理模块(JSON 等)。这通过确保干净的导入来提高安全性,并与内容安全策略 (CSP) 保持一致,以实现更安全的资源加载。TypeScript 确保它们有效,但让运行时处理它们的解释以进行特定的模块处理。

示例:

<!-- skip -->
```typescript
import config from './config.json' with { type: 'json' };
```

使用动态导入:

<!-- skip -->
```typescript
const config = import("./config.json", { with: { type: "json" } })
```
Loading