1.2.2-TS-TS

115 阅读1分钟

★ TS快速上手

  • 下载安装 typescript 模块npm包 yarn add typescript
  • 编写代码
  • 运行 yarn tsc xxx.ts // tsc命令 typescriptcompile

类型断言

而类型转换是运行时候的概念

断言是编译过程中的概念,编译过后这个断言就不存在

const num1 = res as number

const num2 = <number>res // JSX 下不能使用

接口

接口用来约束一个对象的结构,一个对象要实现一个接口,就必须拥友接口当中的所有成员,

只是在编译中做类型约束的,运行中没有实际意义,

interface Post {
  title: string
  subtitle?: string  // 可有可无的:  ?
  readonly summary: string // readonly 只读,修改时候会报错
}

interface Cache {
  [prop: string]: string  //动态成员: 
}