├── .gitignore ├── README.md ├── SUMMARY copy.md ├── SUMMARY.md ├── advanced ├── README.md ├── basic-types.md ├── declaration-files.md ├── declaration-merging.md ├── decorators.md ├── iterators-and-generators.md ├── symbols.md └── variable-declarations.md ├── basics ├── README.md ├── basic-types.md ├── class-and-interfaces.md ├── classes.md ├── functions.md ├── generics.md └── interfaces.md ├── book.json ├── introduction ├── get-typescript.md ├── hello-typescript.md └── what-is-typescript.md └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | _book 2 | node_modules 3 | .DS_Store -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 前言 2 | 3 | > 本小册是《千锋大前端小册》系列之 TypeScript 部分。通过本小册,可以系统学习TypeScript基础知识,为将来TypeScript在大前端项目的应用打下坚实的基础。***—— 作者:古艺散人*** -------------------------------------------------------------------------------- /SUMMARY copy.md: -------------------------------------------------------------------------------- 1 | # Summary 2 | 3 | - [前言](README.md) 4 | - [简介](introduction/README.md) 5 | - [什么是 TypeScript](introduction/what-is-typescript.md) 6 | - [安装 TypeScript](introduction/get-typescript.md) 7 | - [5分钟了解 TypeScript](introduction/hello-typescript.md) 8 | - [基础](basics/README.md) 9 | - [基础类型](basics/basic-types.md) 10 | - [函数](basics/functions.md) 11 | - [接口](basics/interfaces.md) 12 | - [类](basics/classes.md) 13 | - [类和接口](basics/class-and-interfaces.md) 14 | - [泛型](basics/generics.md) 15 | - [进阶](advanced/README.md) 16 | - [基础类型](advanced/basic-types.md) 17 | - [变量声明](advanced/variable-declarations.md) 18 | - [枚举](basics/enums.md) 19 | - [类型推论](basics/type-inference.md) 20 | - [类型兼容性](basics/type-compatibility.md) 21 | - [高级类型](basics/advanced-types.md) 22 | - [Symbols](basics/symbols.md) 23 | - [迭代器和生成器](basics/iterators-and-generators.md) 24 | - [模块](basics/modules.md) 25 | - [命名空间](basics/namespaces.md) 26 | - [命名空间和模块](basics/namespaces-and-modules.md) 27 | - [模块解析](basics/module-resolution.md) 28 | - [声明合并](basics/declaration-merging.md) 29 | - [JSX](basics/jsx.md) 30 | - [装饰器](basics/decorators.md) 31 | - [Mixins](basics/mixins.md) 32 | - [三斜线指令](basics/type-slash-directives.md) 33 | - [JavaScript类型检查](basics/type-checking-javascript-files.md) 34 | - [工具类型](basics/utility-types.md) 35 | 36 | - [变量声明](advanced/variable-declarations.md) 37 | - [声明文件](advanced/declaration-files.md) 38 | 39 | - [声明文件](engineering/README.md) 40 | 41 | - [感谢](thanks/README.md) -------------------------------------------------------------------------------- /SUMMARY.md: -------------------------------------------------------------------------------- 1 | # Summary 2 | 3 | - [前言](README.md) 4 | 5 | - [简介](introduction/README.md) 6 | - [什么是 TypeScript](introduction/what-is-typescript.md) 7 | - [安装 TypeScript](introduction/get-typescript.md) 8 | - [5分钟了解 TypeScript](introduction/hello-typescript.md) 9 | 10 | - [基础](basics/README.md) 11 | - [基础类型-基础](basics/basic-types.md) 12 | - [函数](basics/functions.md) 13 | - [接口](basics/interfaces.md) 14 | - [类](basics/classes.md) 15 | - [类和接口](basics/class-and-interfaces.md) 16 | - [泛型](basics/generics.md) 17 | 18 | - [进阶](advanced/README.md) 19 | - [基础类型-高级](advanced/basic-types.md) 20 | - [Symbols](advanced/symbols.md) 21 | - [迭代器和生成器](advanced/iterators-and-generators.md) 22 | - [装饰器](advanced/decorators.md) 23 | - [声明文件](advanced/declaration-files.md) 24 | - [声明合并](advanced/declaration-merging.md) 25 | - [变量声明](advanced/variable-declarations.md) 26 | 27 | - [感谢](thanks/README.md) -------------------------------------------------------------------------------- /advanced/README.md: -------------------------------------------------------------------------------- 1 | # 高级 2 | ## 基础类型 -------------------------------------------------------------------------------- /advanced/basic-types.md: -------------------------------------------------------------------------------- 1 | # 基础类型-高级 2 | ## 类型推断 3 | 如果没有明确的指定类型,那么 TypeScript 会依照类型推论(Type Inference)的规则推断出一个类型。 4 | ### 什么是类型推断 5 | 以下代码虽然没有指定类型,但是会在编译的时候报错: 6 | ``` 7 | let lunarDay = '初一' 8 | lunarDay = 1 9 | // Type '1' is not assignable to type 'string'. 10 | ``` 11 | 事实上,它等价于: 12 | ``` 13 | let lunarDay: string = '初一' 14 | lunarDay = 1 15 | ``` 16 | 17 | TypeScript 会在没有明确的指定类型的时候推测出一个类型,这就是类型推论。 18 | 如果定义的时候没有赋值,不管之后有没有赋值,都会被推断成 any 类型而完全不被类型检查: 19 | let myFavoriteNumber 20 | myFavoriteNumber = 'seven' 21 | myFavoriteNumber = 7 22 | 23 | let someValue: any = "this is a string" 24 | 25 | let strLength: number = (someValue).length 26 | 另一个为as语法: 27 | 28 | let someValue: any = "this is a string" 29 | 30 | let strLength: number = (someValue as string).length 31 | 两种形式是等价的。 至于使用哪个大多数情况下是凭个人喜好;然而,当你在TypeScript里使用JSX时,只有 as语法断言是被允许的。 32 | 33 | ## 联合类型 34 | 联合类型(Union Types)表示取值可以为多种类型中的一种。 35 | ### 简单的例子 36 | ``` 37 | let lunarDay: string | number 38 | lunarDay = '初一' 39 | lunarDay = 1 40 | ``` 41 | 联合类型使用 | 分隔每个类型。 42 | 43 | 这里的` let lunarDay: string | number `的含义是,允许 lunarDay 的类型是 string 或者 number,但是不能是其他类型。 44 | 45 | ### 访问联合类型的属性或方法 46 | 当 TypeScript 不确定一个联合类型的变量到底是哪个类型的时候,我们只能访问此联合类型的所有类型里共有的属性或方法: 47 | ``` 48 | function getLength(something: string | number): number { 49 | return something.length 50 | } 51 | // Property 'length' does not exist on type 'string | number'. 52 | // Property 'length' does not exist on type 'number'. 53 | ``` 54 | 55 | 上例中,length 不是 string 和 number 的共有属性,所以会报错。 56 | 访问 string 和 number 的共有属性是没问题的: 57 | ``` 58 | function getString(something: string | number): string { 59 | return something.toString() 60 | } 61 | ``` 62 | ### 联合类型赋值的类型推断 63 | 联合类型的变量在被赋值的时候,会根据类型推论的规则推断出一个类型: 64 | ``` 65 | let lunarDay: string | number 66 | lunarDay = '初一' 67 | console.log(lunarDay.length) // 2 68 | lunarDay = 1 69 | console.log(lunarDay.length) // 编译时报错 70 | ``` 71 | 上例中,第二行的 lunarDay 被推断成了 string,访问它的 length 属性不会报错。 72 | 而第四行的 lunarDay 被推断成了 number,访问它的 length 属性时就报错了。 73 | 74 | ## Null 和 Undefined 75 | `null` 是一个只有一个值的特殊类型。表示一个空对象引用。用 typeof 检测 null 返回是 `object`。 76 | typeof 一个没有值的变量会返回 `undefined`。 77 | 78 | null 和 Undefined 是其他任何类型(包括 void)的子类型,可以赋值给其它类型,如数字类型,此时,赋值后的类型会变成 null 或 undefined。 79 | 80 | 在TypeScript中启用严格的空校验(--strictNullChecks)特性,使得 `null` 和 `undefined` 只能被赋值给 `void` 或本身对应的类型 81 | 82 | 在 tsconfig.json 中启用 --strictNullChecks 83 | ``` 84 | let x: number 85 | x = 1 // 运行正确 86 | x = undefined // 运行错误 87 | x = null // 运行错误 88 | ``` 89 | 90 | 在 tsconfig.json 中启用 --strictNullChecks,需要将x赋值为联合类型 91 | ``` 92 | let x: number | null | undefined //本身对应的类型 93 | x = 1 // 运行正确 94 | x = undefined // 运行正确 95 | x = null // 运行正确 96 | ``` 97 | 98 | ## Never 99 | never类型表示的是那些永不存在的值的类型。 例如, never类型是那些总是会抛出异常或根本就不会有返回值的函数表达式或箭头函数表达式的返回值类型; 变量也可能是 never类型,当它们被永不为真的类型保护所约束时。 100 | 101 | never类型是任何类型的子类型,也可以赋值给任何类型;然而,没有类型是never的子类型或可以赋值给never类型(除了never本身之外)。 即使 any也不可以赋值给never。 102 | 103 | 下面是一些返回never类型的函数: 104 | ``` 105 | // 返回never的函数必须存在无法达到的终点 106 | function error(message: string): never { 107 | throw new Error(message) 108 | } 109 | 110 | // 推断的返回值类型为never 111 | function fail() { 112 | return error("Something failed") 113 | } 114 | 115 | // 返回never的函数必须存在无法达到的终点 116 | function infiniteLoop(): never { 117 | while (true) { 118 | } 119 | } 120 | ``` -------------------------------------------------------------------------------- /advanced/declaration-files.md: -------------------------------------------------------------------------------- 1 | # 声明文件 2 | 3 | 当使用第三方库时,我们需要引用它的声明文件,才能获得对应的代码补全、接口提示等功能。 4 | 5 | ## 新语法索引 6 | 7 | 由于本章涉及大量新语法,故在本章开头列出新语法的索引,方便大家在使用这些新语法时能快速查找到对应的讲解: 8 | 9 | - [`declare var`](#declare-var) 声明全局变量 10 | - [`declare function`](#declare-function) 声明全局方法 11 | - [`declare class`](#declare-class) 声明全局类 12 | - [`declare enum`](#declare-enum) 声明全局枚举类型 13 | - [`declare namespace`](#declare-namespace) 声明(含有子属性的)全局对象 14 | - [`interface` 和 `type`](#interface-he-type) 声明全局类型 15 | - [`export`](#export) 导出变量 16 | - [`export namespace`](#export-namespace) 导出(含有子属性的)对象 17 | - [`export default`](#export-default) ES6 默认导出 18 | - [`export =`](#export-1) commonjs 导出模块 19 | - [`export as namespace`](#export-as-namespace) UMD 库声明全局变量 20 | - [`declare global`](#declare-global) 扩展全局变量 21 | - [`declare module`](#declare-module) 扩展模块 22 | - [`/// `](#san-xie-xian-zhi-ling) 三斜线指令 23 | 24 | ## 什么是声明语句 25 | 26 | 假如我们想使用第三方库 jQuery,一种常见的方式是在 html 中通过 `