├── .gitignore ├── README.md ├── loppo.yml ├── chapters.yml ├── package.json ├── .github └── workflows │ └── wangdoc.yml └── docs ├── react.md ├── npm.md ├── es6.md ├── symbol.md ├── comment.md ├── tsc.md ├── array.md ├── intro.md ├── tuple.md ├── any.md ├── namespace.md ├── narrowing.md ├── mapping.md ├── basic.md ├── type-operations.md ├── declare.md ├── module.md ├── d.ts.md ├── enum.md ├── generics.md ├── interface.md ├── assert.md ├── operator.md ├── decorator-legacy.md └── object.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | dist/ 3 | package-lock.json 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | TypeScript 开源教程,介绍基本概念和用法,面向初学者。 2 | 3 | ![](https://cdn.beekka.com/blogimg/asset/202308/bg2023080705.webp) 4 | 5 | -------------------------------------------------------------------------------- /loppo.yml: -------------------------------------------------------------------------------- 1 | dir: docs 2 | output: dist 3 | site: TypeScript 教程 4 | theme: wangdoc 5 | customization: false 6 | themeDir: loppo-theme 7 | direction: ltr 8 | id: typescript 9 | branch: main 10 | hasComments: true 11 | isTutorial: true 12 | -------------------------------------------------------------------------------- /chapters.yml: -------------------------------------------------------------------------------- 1 | - intro.md: 简介 2 | - basic.md: 基本用法 3 | - any.md: any 类型 4 | - types.md: 类型系统 5 | - array.md: 数组 6 | - tuple.md: 元组 7 | - symbol.md: symbol 类型 8 | - function.md: 函数 9 | - object.md: 对象 10 | - interface.md: interface 11 | - class.md: 类 12 | - generics.md: 泛型 13 | - enum.md: Enum 类型 14 | - assert.md: 类型断言 15 | - module.md: 模块 16 | - namespace.md: namespace 17 | - decorator.md: 装饰器 18 | - decorator-legacy.md: 装饰器(旧语法) 19 | - declare.md: declare 关键字 20 | - d.ts.md: d.ts 类型声明文件 21 | - operator.md: 类型运算符 22 | - mapping.md: 类型映射 23 | - utility.md: 类型工具 24 | - comment.md: 注释指令 25 | - tsconfig.json.md: tsconfig.json 文件 26 | - tsc.md: tsc 命令 27 | 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "typescript-tutorial", 3 | "version": "1.0.0", 4 | "description": "TypeScript 开源教程,介绍基本概念和用法,面向初学者。", 5 | "main": "index.js", 6 | "directories": { 7 | "doc": "docs" 8 | }, 9 | "scripts": { 10 | "build": "loppo --site \"TypeScript 教程\" --id typescript --theme wangdoc", 11 | "build-and-commit": "npm run build && npm run commit", 12 | "commit": "gh-pages --dist dist --dest dist/typescript --branch master --repo git@github.com:wangdoc/website.git", 13 | "chapter": "loppo chapter", 14 | "server": "loppo server" 15 | }, 16 | "keywords": [ 17 | "TypeScript", 18 | "tutorial" 19 | ], 20 | "author": "Ruan Yifeng", 21 | "license": "CC-BY-SA-4.0", 22 | "dependencies": { 23 | "gh-pages": "6.x", 24 | "loppo": "^0.6.25", 25 | "loppo-theme-wangdoc": "^0.7.3" 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /.github/workflows/wangdoc.yml: -------------------------------------------------------------------------------- 1 | name: TypeScript tutorial CI 2 | on: 3 | push: 4 | branches: 5 | - main 6 | 7 | jobs: 8 | page-generator: 9 | name: Generating pages 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Checkout 13 | uses: actions/checkout@v4 14 | with: 15 | persist-credentials: false 16 | - name: Setup Node.js 17 | uses: actions/setup-node@v4 18 | with: 19 | node-version: 'latest' 20 | - name: Install dependencies 21 | run: npm install 22 | - name: Build pages 23 | run: npm run build 24 | - name: Deploy to website 25 | uses: JamesIves/github-pages-deploy-action@v4 26 | with: 27 | git-config-name: wangdoc-bot 28 | git-config-email: yifeng.ruan@gmail.com 29 | repository-name: wangdoc/website 30 | token: ${{ secrets.WANGDOC_BOT_TOKEN }} 31 | branch: master # The branch the action should deploy to. 32 | folder: dist # The folder the action should deploy. 33 | target-folder: dist/typescript 34 | clean: true # Automatically remove deleted files from the deploy branch 35 | commit-message: update from TypeScript tutorial 36 | 37 | -------------------------------------------------------------------------------- /docs/react.md: -------------------------------------------------------------------------------- 1 | # TypeScript 的 React 支持 2 | 3 | ## JSX 语法 4 | 5 | JSX 是 React 库引入的一种语法,可以在 JavaScript 脚本中直接书写 HTML 风格的标签。 6 | 7 | TypeScript 支持 JSX 语法,但是必须将脚本后缀名改成`.tsx`。 8 | 9 | `.tsx`文件中,类型断言一律使用`as`形式,因为尖括号的写法会与 JSX 冲突。 10 | 11 | ```typescript 12 | // 使用 13 | var x = foo as any; 14 | 15 | // 不使用 16 | var x = foo; 17 | ``` 18 | 19 | 上面示例中,变量`foo`被断言为类型`any`,在`.tsx`文件中只能使用第一种写法,不使用第二种写法。 20 | 21 | ## React 库 22 | 23 | TypeScript 使用 React 库必须引入 React 的类型定义。 24 | 25 | ```typescript 26 | /// 27 | interface Props { 28 | name: string; 29 | } 30 | class MyComponent extends React.Component { 31 | render() { 32 | return {this.props.name}; 33 | } 34 | } 35 | ; // OK 36 | ; // error, `name` is not a number 37 | ``` 38 | 39 | ## 内置元素 40 | 41 | 内置元素使用`JSX.IntrinsicElements`接口。默认情况下,内置元素不进行类型检查。但是,如果给出了接口定义,就会进行类型检查。 42 | 43 | ```typescript 44 | declare namespace JSX { 45 | interface IntrinsicElements { 46 | foo: any; 47 | } 48 | } 49 | ; // ok 50 | ; // error 51 | ``` 52 | 53 | 上面示例中,``不符合接口定义,所以报错。 54 | 55 | 一种解决办法就是,在接口中定义一个通用元素。 56 | 57 | ```typescript 58 | declare namespace JSX { 59 | interface IntrinsicElements { 60 | [elemName: string]: any; 61 | } 62 | } 63 | ``` 64 | 65 | 上面示例中, 元素名可以是任意字符串。 66 | 67 | ## 组件的写法 68 | 69 | ```typescript 70 | interface FooProp { 71 | name: string; 72 | X: number; 73 | Y: number; 74 | } 75 | declare function AnotherComponent(prop: { name: string }); 76 | function ComponentFoo(prop: FooProp) { 77 | return ; 78 | } 79 | const Button = (prop: { value: string }, context: { color: string }) => ( 80 |