js 的原生类型
- 基本数据类型
- string number undefined null boolean
 
- 复杂【引用】数据类型
- object
 
- 缺点: 弱类型 数据类型可以更改 安全性低
ts 类型系统 强类型 数据类型是定死的 安全性更高
编译代码时即可以发现错误
- 基本数据类型
- string number undefined null boolean
 
- 复杂【引用】数据类型
- object
 
- 其他数据类型
- 内置对象类型
- 元组
- 枚举
- 接口
- 。。。
 
为什么要提供类型系统? 为什么现在要用 ts
- ts 是强类型,安全度更高
- ts 编译既可以发现错误,可以减少 debug 时间
- 提供更多的代码提示
ts 缺点
代码量增加了
第一个 ts 案例
- ts 不能直接在浏览器使用,需要编译成 js
- tsc 在编译时,还会将高版本的 es 语法转成 es5
基础
let/const 变量名:数据类型 = 值
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 | let m: number = 1000;
 
 
 let s: string = "Hello ts";
 
 
 let b: boolean = false;
 
 
 let und: undefined = undefined;
 
 
 let nul: null = null;
 
 | 
ts 给了简写:类型推论(根据值来根据值来定义变量的数据类型)
| 12
 
 | let n = 1000; n = "string";
 
 | 
对象类型
在 TypeScript 中,我们使用接口(Interfaces)来定义对象的类型。
| 12
 3
 4
 5
 6
 7
 
 | const obj = {
 id: 1,
 name: "zhangsan",
 age: 18,
 sex: "nan",
 };
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 
 | const obj: ObjType = {
 
 id: 1,
 name: "zhangsan",
 age: 18,
 sex: "nan",
 color: "yellow",
 };
 
 
 
 
 
 
 
 
 
 
 interface ObjType {
 readonly id: number;
 name: string;
 age: number;
 sex: string;
 color?: string;
 [propName: string]: any;
 }
 
 
 
 
 type ObjType = {
 readonly id: number;
 name: string;
 age: number;
 sex: string;
 color?: string;
 [propName: string]: any;
 };
 
 | 
总结:
- 赋值的时候,变量的形状必须和接口的形状保持一致。
- 可选属性的含义是该属性可以不存在。 ?:
- 未定义的属性用任意属性 [propName: string]: any
- 只读属性 readonly
函数
ts 定类型:参数和返回值
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 
 | function fn(a: number, b: number): number {
 return a + b;
 }
 
 const fn2 = (n: string, m: string): string => {
 return n + m;
 };
 
 const fn3: (n: string, m: string) => string = (n, m) => {
 return n + m;
 };
 
 function fn4(a: string, b: boolean): void {
 
 
 }
 
 
 
 const fn5: FnType = (a, b) => a + b;
 
 interface FnType {
 (a: number, b: number): number;
 }
 
 | 
数组
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 
 | 
 const arr: number[] = [1, 2, 3, 4];
 
 
 const arr2: string[] = ["a", "b", "v"];
 
 
 const arr3: (number | string | boolean)[] = [1, 2, 3, "a", "b", true];
 
 
 const arr4: ArrType[] = [
 {
 id: 1,
 name: "zhangsan",
 age: 18,
 sex: "nan",
 color: "yellow",
 },
 ];
 
 
 
 | 
元组类型
元组: 是一种规定了数组长度以及每一个元素的数据类型的特殊数组
| 12
 3
 4
 
 | 
 let x: [number, string];
 x = [10, "hello"];
 
 | 
枚举类型
枚举类型:自动添加计数
| 12
 3
 4
 5
 6
 7
 8
 9
 
 | enum Color {Red = 1,
 Green,
 Yellow,
 }
 
 console.log(Color.Green);
 console.log(Color.Yellow);
 console.log(Color[1]);
 
 | 
泛型
泛型:给未知数据定义类型
| 12
 3
 4
 5
 6
 7
 
 | function Handler<T>(a: T): number {
 return 1000;
 }
 
 Handler(10);
 Handler("hello");
 
 | 
class (类)
问题: 类中可以写什么?
- 实例成员: 实例化类之后得到一个实例,这个实例身上的东西
- 实例属性 [public] count = 1
- 实例方法 [public] fn () {}
 
- 静态成员 类身上的东西
- 静态属性 static color = ‘red’
- 静态方法 static handler () {}
 
- 私有成员 类内部使用的属性和方法
- 私有属性 private msg = ‘hello’
- 私有方法 private func () {}
 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 
 | class App implements AppType, HelloType {
 count = 1;
 color = "yello";
 fn = () => {};
 username = "lakers";
 static color: string = "red";
 static handler(n: string, m: string): string {
 return n + m;
 }
 
 private msg: string = "hello";
 private func(n: string, m: string): string {
 return n + m;
 }
 }
 
 interface AppType {
 
 count: number;
 color: string;
 }
 
 interface HelloType {
 username: string;
 }
 
 | 
类型声明
问题: ts 中引入其他 js 插件,会发生什么
$. js 提示代码一个都没有了
类型声明: xxx.d.ts 类型声明文件
类型声明文件 @types 社区
输入以下命令
| 1
 | $ cnpm i @types/jquery -S
 | 
xxx.d.ts 类型声明文件中
declare 可以为 js 来声明类型
/// 三斜杠语法 声明文件的引入