Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: Chuck-Lau/typescript-tutorial
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: wangdoc/typescript-tutorial
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: main
Choose a head ref
Able to merge. These branches can be automatically merged.

Commits on Aug 17, 2023

  1. docs: fix typo

    lean committed Aug 17, 2023
    Copy the full SHA
    8c0ed20 View commit details
  2. Merge pull request wangdoc#50 from Tsong-LC/docs/typo

    docs: fix typo
    ruanyf authored Aug 17, 2023
    Copy the full SHA
    658fe7b View commit details
  3. Merge pull request wangdoc#49 from byog/main

    docs: fix typo
    ruanyf authored Aug 17, 2023
    Copy the full SHA
    deb8522 View commit details
  4. docs: fix unexpected newline and space

    出现了意外的换行和空格
    lean committed Aug 17, 2023
    Copy the full SHA
    59e0b3b View commit details
  5. Merge pull request wangdoc#52 from byog/main

    docs: fix unexpected newline and space
    ruanyf authored Aug 17, 2023
    Copy the full SHA
    ee8976f View commit details

Commits on Aug 18, 2023

  1. change .Net to .NET

    gaufung committed Aug 18, 2023
    Copy the full SHA
    ed8175f View commit details
  2. fix more .Net

    gaufung committed Aug 18, 2023
    Copy the full SHA
    04d1a72 View commit details
  3. fix: typo

    nocodeempire committed Aug 18, 2023
    Copy the full SHA
    20effd3 View commit details
  4. fix: typo

    nocodeempire committed Aug 18, 2023
    Copy the full SHA
    e139724 View commit details
  5. fix:typo

    nextchentong committed Aug 18, 2023
    Copy the full SHA
    8009d3b View commit details
  6. fix:typo

    nextchentong committed Aug 18, 2023
    Copy the full SHA
    2d90457 View commit details
  7. Copy the full SHA
    0f7f99d View commit details

Commits on Aug 19, 2023

  1. docs(object): fixed wangdoc#58

    ruanyf committed Aug 19, 2023
    Copy the full SHA
    6bea461 View commit details
  2. Merge pull request wangdoc#57 from nextchentong/update-text

    docs: fix typo
    ruanyf authored Aug 19, 2023
    Copy the full SHA
    180177c View commit details
  3. docs(class): fixed wangdoc#59

    ruanyf committed Aug 19, 2023
    Copy the full SHA
    cdbbab4 View commit details
  4. docs(types): fixed wangdoc#55

    ruanyf committed Aug 19, 2023
    Copy the full SHA
    77bfab0 View commit details
  5. Merge pull request wangdoc#53 from gaufung/dotnet

    Change .Net to .NET
    ruanyf authored Aug 19, 2023
    Copy the full SHA
    9768e0e View commit details
  6. Merge pull request wangdoc#54 from nocodeempire/main

    fix: typo
    ruanyf authored Aug 19, 2023
    Copy the full SHA
    2bdcb0c View commit details
  7. Copy the full SHA
    eff43ce View commit details
  8. fix:typo

    hsworld-top committed Aug 19, 2023
    Copy the full SHA
    a39a954 View commit details

Commits on Aug 20, 2023

  1. docs(enum): fix enum overhaul

    ruanyf committed Aug 20, 2023
    Copy the full SHA
    947d40e View commit details
  2. Merge pull request wangdoc#61 from biubiubiu999/main

    fix:typo
    ruanyf authored Aug 20, 2023
    Copy the full SHA
    60ba53b View commit details
  3. Merge pull request wangdoc#60 from KiritaniAyaka/patch-2

    docs(class): add extra description for class and interface merging
    ruanyf authored Aug 20, 2023
    Copy the full SHA
    6b31cee View commit details
  4. docs(class): fix wangdoc#60

    ruanyf committed Aug 20, 2023
    Copy the full SHA
    e0cd6ed View commit details

Commits on Aug 21, 2023

  1. Update interface.md

    vikyd authored Aug 21, 2023
    Copy the full SHA
    885e906 View commit details
  2. docs(operator): fix keyof

    ruanyf committed Aug 21, 2023
    Copy the full SHA
    ec0a6de View commit details
  3. Merge pull request wangdoc#65 from vikyd/main

    fix: 错别字即应改为既
    ruanyf authored Aug 21, 2023
    Copy the full SHA
    9919b9d View commit details
  4. docs(mapping): fix typo

    ruanyf committed Aug 21, 2023
    Copy the full SHA
    abcc074 View commit details
  5. docs(types): fixed wangdoc#64

    ruanyf committed Aug 21, 2023
    Copy the full SHA
    5459b3c View commit details
  6. docs(tsconfig.json): fix jsx

    ruanyf committed Aug 21, 2023
    Copy the full SHA
    22ec0da View commit details

Commits on Aug 23, 2023

  1. docs(tuple): modify text

    ruanyf committed Aug 23, 2023
    Copy the full SHA
    7a4164f View commit details
  2. fix: typo

    nocodeempire committed Aug 23, 2023
    Copy the full SHA
    238fd31 View commit details
  3. fix: typo

    nocodeempire committed Aug 23, 2023
    Copy the full SHA
    5a14674 View commit details
  4. fix: typo

    nocodeempire committed Aug 23, 2023
    Copy the full SHA
    0b265d2 View commit details
  5. Copy the full SHA
    2e99e74 View commit details
  6. Merge pull request wangdoc#67 from nocodeempire/main

    fix: typo
    ruanyf authored Aug 23, 2023
    Copy the full SHA
    9966606 View commit details
  7. fix: typo

    nocodeempire committed Aug 23, 2023
    Copy the full SHA
    a3ca89f View commit details
  8. Merge pull request wangdoc#68 from nocodeempire/main

    fix: typo
    ruanyf authored Aug 23, 2023
    Copy the full SHA
    44a526e View commit details

Commits on Aug 25, 2023

  1. docs(basic): fix typo

    songjinke committed Aug 25, 2023
    Copy the full SHA
    7a31fb8 View commit details
  2. Merge pull request wangdoc#69 from songjinke/main

    docs(basic): fix typo
    ruanyf authored Aug 25, 2023
    Copy the full SHA
    3dc90af View commit details

Commits on Aug 26, 2023

  1. Copy the full SHA
    80afd4c View commit details
  2. Copy the full SHA
    4985019 View commit details

Commits on Aug 27, 2023

  1. Copy the full SHA
    a56d4e1 View commit details
  2. Merge pull request wangdoc#70 from 95cc/main

    docs(tsconfig.json): fix typo
    ruanyf authored Aug 27, 2023
    Copy the full SHA
    88073bd View commit details
  3. docs(enum): fix typo

    ruanyf committed Aug 27, 2023
    Copy the full SHA
    0d4d958 View commit details

Commits on Aug 29, 2023

  1. docs(interface):错别字

    alex12306 committed Aug 29, 2023
    Copy the full SHA
    2c46d5a View commit details
  2. Merge pull request wangdoc#71 from alex12306/main

    docs(interface):错别字
    ruanyf authored Aug 29, 2023
    Copy the full SHA
    b106d73 View commit details

Commits on Aug 30, 2023

  1. docs: perfect title

    thinkasany committed Aug 30, 2023
    Copy the full SHA
    eb98251 View commit details
  2. Merge pull request wangdoc#74 from thinkasany/main

       docs: perfect title
    ruanyf authored Aug 30, 2023
    Copy the full SHA
    542bf6b View commit details
  3. Copy the full SHA
    2fdbc00 View commit details
Showing with 509 additions and 218 deletions.
  1. +2 −2 .github/workflows/wangdoc.yml
  2. +1 −1 chapters.yml
  3. +1 −1 docs/any.md
  4. +2 −21 docs/assert.md
  5. +1 −1 docs/basic.md
  6. +185 −52 docs/class.md
  7. +1 −1 docs/comment.md
  8. +21 −6 docs/declare.md
  9. +24 −16 docs/decorator.md
  10. +11 −12 docs/enum.md
  11. +1 −1 docs/es6.md
  12. +38 −6 docs/function.md
  13. +1 −1 docs/generics.md
  14. +4 −4 docs/interface.md
  15. +2 −2 docs/intro.md
  16. +5 −5 docs/mapping.md
  17. +22 −20 docs/module.md
  18. +5 −6 docs/object.md
  19. +85 −15 docs/operator.md
  20. +55 −24 docs/tsconfig.json.md
  21. +26 −10 docs/tuple.md
  22. +9 −4 docs/types.md
  23. +5 −5 docs/utility.md
  24. +2 −2 package.json
4 changes: 2 additions & 2 deletions .github/workflows/wangdoc.yml
Original file line number Diff line number Diff line change
@@ -10,11 +10,11 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
persist-credentials: false
- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 'latest'
- name: Install dependencies
2 changes: 1 addition & 1 deletion chapters.yml
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
- decorator-legacy.md: 装饰器(旧语法)
- declare.md: declare 关键字
- d.ts.md: d.ts 类型声明文件
- operator.md: 运算符
- operator.md: 类型运算符
- mapping.md: 类型映射
- utility.md: 类型工具
- comment.md: 注释指令
2 changes: 1 addition & 1 deletion docs/any.md
Original file line number Diff line number Diff line change
@@ -240,7 +240,7 @@ let v2:string = f(); // 不报错
let v3:boolean = f(); // 不报错
```

上面示例中,函数`f()`会抛错,所以返回值类型可以写成`never`,即不可能返回任何值。各种其他类型的变量都可以赋值为`f()`的运行结果(`never`类型)。
上面示例中,函数`f()`会抛出错误,所以返回值类型可以写成`never`,即不可能返回任何值。各种其他类型的变量都可以赋值为`f()`的运行结果(`never`类型)。

为什么`never`类型可以赋值给任意其他类型呢?这也跟集合论有关,空集是任何集合的子集。TypeScript 就相应规定,任何类型都包含了`never`类型。因此,`never`类型是任何其他类型所共有的,TypeScript 把这种情况称为“底层类型”(bottom type)。

23 changes: 2 additions & 21 deletions docs/assert.md
Original file line number Diff line number Diff line change
@@ -114,15 +114,6 @@ const s2:string = value as string; // 正确

上面示例中,unknown 类型的变量`value`不能直接赋值给其他类型的变量,但是可以将它断言为其他类型,这样就可以赋值给别的变量了。

另外,类型断言也适合指定联合类型的值的具体类型。

```typescript
const s1:number|string = 'hello';
const s2:number = s1 as number;
```

上面示例中,变量`s1`是联合类型,可以断言其为联合类型里面的一种具体类型,再将其赋值给变量`s2`

## 类型断言的条件

类型断言并不意味着,可以把某个值断言为任意类型。
@@ -228,8 +219,7 @@ let s = 'JavaScript';
setLang(s as const); // 报错
```

上面示例中,`as
const`断言用于变量`s`,就报错了。下面的写法可以更清晰地看出这一点。
上面示例中,`as const`断言用于变量`s`,就报错了。下面的写法可以更清晰地看出这一点。

```typescript
let s1 = 'JavaScript';
@@ -500,18 +490,9 @@ function assertIsDefined<T>(

上面示例中,工具类型`NonNullable<T>`对应类型`T`去除空类型后的剩余类型。

如果要将断言函数用于函数表达式,可以采用下面的写法。
如果要将断言函数用于函数表达式,可以采用下面的写法。根据 TypeScript 的[要求](https://github.com/microsoft/TypeScript/pull/33622#issuecomment-575301357),这时函数表达式所赋予的变量,必须有明确的类型声明。

```typescript
// 写法一
const assertIsNumber = (
value:unknown
):asserts value is number => {
if (typeof value !== 'number')
throw Error('Not a number');
};

// 写法二
type AssertIsNumber =
(value:unknown) => asserts value is number;

2 changes: 1 addition & 1 deletion docs/basic.md
Original file line number Diff line number Diff line change
@@ -69,7 +69,7 @@ function toString(num:number) {

上面示例中,函数`toString()`没有声明返回值的类型,但是 TypeScript 推断返回的是字符串。正是因为 TypeScript 的类型推断,所以函数返回值的类型通常是省略不写的。

从这里可以看到,TypeScript 的设计思想是,类型声明是可选的,你可以加,也可以不加。即使不加类型声明,依然是有效的 TypeScript 代码,只是这时不能保证 TypeScript 会正确推断出类型。由于这个原因所有 JavaScript 代码都是合法的 TypeScript 代码。
从这里可以看到,TypeScript 的设计思想是,类型声明是可选的,你可以加,也可以不加。即使不加类型声明,依然是有效的 TypeScript 代码,只是这时不能保证 TypeScript 会正确推断出类型。由于这个原因所有 JavaScript 代码都是合法的 TypeScript 代码。

这样设计还有一个好处,将以前的 JavaScript 项目改为 TypeScript 项目时,你可以逐步地为老代码添加类型,即使有些代码没有添加,也不会无法运行。

Loading