用于取消 readonly 的只读属性。来源 readonly mapped type modifiers and readonly arrays (opens new window)
操作符
# 类型系统
# 类型别名
type 定义类型别名
类型别名和接口的区别 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#interfaces-vs-type-aliases
# 接口
# 元祖
# enum
- const enum (opens new window) 运行时编译为内联内容
- 枚举的类型推导
- 枚举成员作为类型限制符号 https://www.typescriptlang.org/v2/docs/handbook/enums.html#union-enums-and-enum-member-types
- Ambient enums https://www.typescriptlang.org/v2/docs/handbook/enums.html#ambient-enums
# 类
- 类
- 可以直接在构造器上申明 public 且不用赋值,详见 public scale (opens new window)
- 访问控制
- protected (opens new window) 类和子类才可访问
- private 类中才可访问,实例无法访问
- ,实例无法访问
- 构造函数也可限制,表示只有扩展类才可实例化
- public 类实例均可以访问
- 结合 readonly 限制属性
- 构造函数名字限制为只读,没看懂 https://www.typescriptlang.org/v2/docs/handbook/classes.html#parameter-properties
- 访问器
- 只有 get 自动推到为 readonly
- 静态成员
- 构造器约束和实例约束 https://www.typescriptlang.org/v2/docs/handbook/interfaces.html#difference-between-the-static-and-instance-sides-of-classes (opens new window)
- 继承
implements实现接口extends shape,..实现d多继承,详见 https://www.typescriptlang.org/v2/docs/handbook/interfaces.html#extending-interfaces- 组合
implements extend使用 - 接口继承类类型 https://www.typescriptlang.org/v2/docs/handbook/interfaces.html#interfaces-extending-classes (opens new window) 没看懂此特性
- 抽象类
- 抽象方法
- 构造器类型和实例类型
- 接口继承类类型 https://www.typescriptlang.org/v2/docs/handbook/classes.html#using-a-class-as-an-interface
- 装饰器 https://www.typescriptlang.org/v2/docs/handbook/decorators.html
- 混入 https://www.typescriptlang.org/v2/docs/handbook/mixins.html
- protected (opens new window) 类和子类才可访问
- 原始类型
- 基本类型 es6 的基础类型
- 特殊基础类型 void,never,any
- 对象类型 object 所有 js 引用类型
--strictNullChecks默认关闭,此时基础类型值 null,undefined 可以赋值给其他类型,因为 null,undefined 是所有其他类型的子类型 - 接口类型
- 采用
?:声明可选参数 - 采用
readonly修饰只读参数 ReadonlyArray<T>修饰只读数组- 使用 const 和 readonly 的选择 https://www.typescriptlang.org/v2/docs/handbook/interfaces.html#readonly-vs-const (opens new window)
- 变量 const
- 属性 readonly
- 索引类型实现扩展
[index:string]:any - 索引类型约束字段,详见 https://www.typescriptlang.org/v2/docs/handbook/interfaces.html#excess-property-checks (opens new window)
- 综合 readonly 和索引类型定义
- 使用 const 和 readonly 的选择 https://www.typescriptlang.org/v2/docs/handbook/interfaces.html#readonly-vs-const (opens new window)
- 采用
- 函数
- 参数和返回值的注解
- 内联函数注解
> 采用有意义的参数注解函数便于使用,参看 https://www.typescriptlang.org/v2/docs/handbook/functions.html#writing-the-function-type
- 可选参数
- 剩余参数
- this 限制 https://www.typescriptlang.org/v2/docs/handbook/functions.html#this-parameters
- 重载
- 解构标注
- 组合类型
- 联合类型 1.4 加入 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-4.html#union-types
- 交叉类型
- 泛型
- 泛型基本使用
- 泛型变量
- 泛型类
- 泛型类型操作
extends实现泛型约束new泛型构造函数keyof
- 高阶类型
- 字面量类型
- 数值字面量
- 字符串字面量 https://github.com/Microsoft/TypeScript/pull/5185
Discriminated Unionshttps://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#discriminated-unions- this 的多态 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#polymorphic-this-types
- 索引类型
keyof后去泛型的字面量联合类型
- 映射类型
in迭代联合类型
- 条件类型 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#conditional-types
- 预定义的条件类型 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#type-inference-in-conditional-types
- 分配置条件类型 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#distributive-conditional-types
- 工具类型 https://www.typescriptlang.org/v2/docs/handbook/utility-types.html
- 模块
- 模块解析
- 命名空间
- JSX 的类型系统
# 函数
# 类型断言
<>- as 语法
当运行时需要根据不同入参进行处理使采用断言避免报错
# 类型守卫
用于条件判断 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/
isintypeofstrictNullChecks限制赋值
类型守卫和类型断言的区别 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#type-guards-and-type-assertions
类型守卫,类型判断语法会被 TypeScript 理解,并影响类型推导。
# 类型注释
:进行注释- 结构对象的类型注解
?:表示可选字段- js 文件采用 jsdoc 注释 https://www.typescriptlang.org/v2/docs/handbook/type-checking-javascript-files.html
# 类型推导
https://www.typescriptlang.org/v2/docs/handbook/type-inference.html
上下文推导 https://www.typescriptlang.org/v2/docs/handbook/functions.html#inferring-the-types
耗尽检查 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#exhaustiveness-checking
条件类型推导 https://www.typescriptlang.org/v2/docs/handbook/advanced-types.html#type-inference-in-conditional-types
类型兼容 https://www.typescriptlang.org/v2/docs/handbook/type-compatibility.html#subtype-vs-assignment
- 子类型兼容
- 赋值兼容
类型申明,用于创建类型
重点理解创建值的申明会产生运行时代码, 表 https://www.typescriptlang.org/v2/docs/handbook/declaration-merging.html#basic-concepts
申明合并
- 后申明顺序在上
- 含有字面量则优先
- 不同类型的合并
- 模块增加类型
工具特性
三元指令 Triple-Slash Directives (opens new window)
/// <reference path="..." />索引额外的文件编译
类型系统 如何定义类型
静态类型检测 如何识别类型错误
类型推导
类型注释
运行时类型检测
类型导出
申明
工具特性
vue 使用 ts https://github.com/DanielRosenwasser/typescript-vue-todomvc
# 配置
- noEmitOnError (opens new window) 类型检查出错阻止编译 --strictNull 操作符