# 类型系统

# 类型别名

type 定义类型别名

类型别名和接口的区别 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#interfaces-vs-type-aliases

# 接口

# 元祖

# enum

  1. 枚举的类型推导
  2. 枚举成员作为类型限制符号 https://www.typescriptlang.org/v2/docs/handbook/enums.html#union-enums-and-enum-member-types
  3. Ambient enums https://www.typescriptlang.org/v2/docs/handbook/enums.html#ambient-enums

# 类

  1. 类
  2. 可以直接在构造器上申明 public 且不用赋值,详见 public scale (opens new window)
  1. 原始类型
    1. 基本类型 es6 的基础类型
    2. 特殊基础类型 void,never,any
  2. 对象类型 object 所有 js 引用类型

    --strictNullChecks 默认关闭,此时基础类型值 null,undefined 可以赋值给其他类型,因为 null,undefined 是所有其他类型的子类型

  3. 接口类型
    1. 采用 ?: 声明可选参数
    2. 采用 readonly 修饰只读参数
    3. ReadonlyArray<T> 修饰只读数组
      1. 使用 const 和 readonly 的选择 https://www.typescriptlang.org/v2/docs/handbook/interfaces.html#readonly-vs-const (opens new window)
        1. 变量 const
        2. 属性 readonly
        3. 索引类型实现扩展 [index:string]:any
        4. 索引类型约束字段,详见 https://www.typescriptlang.org/v2/docs/handbook/interfaces.html#excess-property-checks (opens new window)
        5. 综合 readonly 和索引类型定义
  4. 函数
  5. 参数和返回值的注解
  6. 内联函数注解
> 采用有意义的参数注解函数便于使用,参看 https://www.typescriptlang.org/v2/docs/handbook/functions.html#writing-the-function-type
  1. 可选参数
  2. 剩余参数
  3. this 限制 https://www.typescriptlang.org/v2/docs/handbook/functions.html#this-parameters
  4. 重载
  5. 解构标注
  6. 组合类型
  7. 联合类型 1.4 加入 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-4.html#union-types
  8. 交叉类型
  9. 泛型
  10. 泛型基本使用
  11. 泛型变量
  12. 泛型类
  13. 泛型类型操作
    1. extends 实现泛型约束
    2. new 泛型构造函数
    3. keyof
  14. 高阶类型
  15. 字面量类型
    1. 数值字面量
    2. 字符串字面量 https://github.com/Microsoft/TypeScript/pull/5185
  16. Discriminated Unions https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#discriminated-unions
  17. this 的多态 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#polymorphic-this-types
  18. 索引类型
    1. keyof 后去泛型的字面量联合类型
  19. 映射类型
    1. in 迭代联合类型
  20. 条件类型 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#conditional-types
    1. 预定义的条件类型 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#type-inference-in-conditional-types
  21. 分配置条件类型 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#distributive-conditional-types
  22. 工具类型 https://www.typescriptlang.org/v2/docs/handbook/utility-types.html
  23. 模块
  24. 模块解析
  25. 命名空间
  26. JSX 的类型系统

# 函数

# 类型断言

  1. <>
  2. as 语法

当运行时需要根据不同入参进行处理使采用断言避免报错

  1. 对象使用类型断言详见 https://www.typescriptlang.org/v2/docs/handbook/interfaces.html#excess-property-checks (opens new window)

# 类型守卫

用于条件判断 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html 解决运行时类型判断

类型守卫 https://devblogs.microsoft.com/typescript/typescript-1-4-sneak-peek-union-types-type-guards-and-more/

  1. is
  2. in
  3. typeof
  4. strictNullChecks 限制赋值

类型守卫和类型断言的区别 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#type-guards-and-type-assertions

类型守卫,类型判断语法会被 TypeScript 理解,并影响类型推导。

# 类型注释

  1. : 进行注释
  2. 结构对象的类型注解
  3. ?: 表示可选字段
  4. js 文件采用 jsdoc 注释 https://www.typescriptlang.org/v2/docs/handbook/type-checking-javascript-files.html

# 类型推导

https://www.typescriptlang.org/v2/docs/handbook/type-inference.html

  1. 上下文推导 https://www.typescriptlang.org/v2/docs/handbook/functions.html#inferring-the-types

  2. 耗尽检查 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#exhaustiveness-checking

  3. 条件类型推导 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#type-inference-in-conditional-types

  4. 类型兼容 https://www.typescriptlang.org/v2/docs/handbook/type-compatibility.html#subtype-vs-assignment

    1. 子类型兼容
    2. 赋值兼容
  5. 类型申明,用于创建类型

  6. 重点理解创建值的申明会产生运行时代码, 表 https://www.typescriptlang.org/v2/docs/handbook/declaration-merging.html#basic-concepts

  7. 申明合并

    1. 后申明顺序在上
    2. 含有字面量则优先
    3. 不同类型的合并
    4. 模块增加类型
  8. 工具特性

  9. 三元指令 Triple-Slash Directives (opens new window)

    1. /// <reference path="..." /> 索引额外的文件编译
  10. 类型系统 如何定义类型

  11. 静态类型检测 如何识别类型错误

  12. 类型推导

  13. 类型注释

  14. 运行时类型检测

  15. 类型导出

  16. 申明

  17. 工具特性

vue 使用 ts https://github.com/DanielRosenwasser/typescript-vue-todomvc

# 配置