logo头像

前端从未如此简单有趣

一文知TypeScript的核心

TypeSript【官网

【READBOOK】https://www.bookstack.cn/read/wangdoc-typescript-tutorial/docs-d.ts.md

ts是js的一个超集,解决js类型支持
js: 动态类型
ts:静态类型 代码执行的顺序是先编译在执行 发现错误更早

1.为什么要添加类型支持?

image-20240721120523287

2.优势有什么?

image-20240721120537617

3.全局安装

npm i -g typescript
tsc -v
image-20240721120743399

4.简化运行ts的步骤

image-20240721120750227

5.typescript常用类型

image-20240721120830252
类型检查机制
image-20240721120838004

5.1 类型注解

image-20240721120849885

5.2 常用基础类型

image-20240721120912074

5.3 原始类型

image-20240721120918575

6.数组类型

image-20240721120928792

7.类型别名 type关键字

image-20240721120940043

1
2
3
4
5
6
// 类型别名
type CustomArray = (number|string)[]
let arr1:CustomArray = [1,2,'a',3]
let arr2:CustomArray = [1,4,'a',3]
console.log(arr1);
console.log(arr2);

8.函数类型

image-20240721121001207
image-20240721121015535 image.png

9.可选参数

10.对象类型

image-20240721121049309
对象的类型其实就是在描述对象的结构

11.对象中的可选属性

image-20240721121102273

12.接口【复用】

image-20240721121115763

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//   接口
interface IPerson{
name:string
age:number
sayhi():void
}


let person_3:IPerson ={
name:'JACL',
age:11,
sayhi(){}
}

let person_4:IPerson ={
name:'JAC33L',
age:33,
sayhi(){}
}
console.log(person_3);

console.log(person_4);

13.接口和类型别名

image-20240721121126704

1
2
3
4
5
type num = number | string
let a:num = 1
let b:num = 'asf'
console.log(a);
console.log(b);

14.接口继承复用

image-20240721121135431

15.元组

image-20240721121142483

16.类型推论

image-20240721121150350

17.类型断言

image-20240721121208236
image-20240721121217184

18.字面量类型

image-20240721121236561

image-20240721121252766

19.枚举类型

image-20240721121309266
image-20240721121340668
image-20240721121347906

20.字符串枚举

image-20240721121359120

21.枚举特点

image-20240721121408775

22.any类型

image-20240721121416386

23.ypeof

image-20240721121424894

24.TS高级类型

24.1 class

image-20240721121441081
image-20240721121452792

25.类的实例方法

image-20240721121504102

26.类的继承

image-20240721121517304

image-20240721121524791

27.类成员可见性

image-20240721121544322
image-20240721121559852

image.png
image-20240721121608530

28.类型兼容性

image-20240721121619356

29.对象兼容性

image-20240721121633606

30.接口兼容性

image-20240721121642523

31.函数兼容性

image-20240721121711255

32.类型兼容性

image-20240721121753257

33.交叉类型

image-20240721121806081

34.交叉类型(&)和接口继承(extends)的区别

image-20240721121817034

35.泛型

image-20240721121849720
image-20240721121902412

36.简化调用泛型函数

image-20240721121923218

37.泛型循环

image-20240721121937488

38.添加约束

image-20240721121947098

39.泛型的类型变量

image-20240721121958758

40.泛型接口

image-2024072112200836841.JS中的数组就是TS中的一个泛型接口

image-20240721122019636

42.class配合泛型来使用

image-20240721122028955

43.泛型工具类型

43.1 partial

image-20240721122043712

43.2 readonly

image-20240721122101158

43.3 pick

image-20240721122115258

43.4 record

image-20240721122125778

44.索引签名类型

image-20240721122135895
image-20240721122142423

45.映射类型

45.1 基于旧类型进行创建

image-20240721122156811

45.2 基于对象创建

image-20240721122216988
image.png

45.3 引查询类型

image-20240721122236902

45.4 同时查询多个索引的类型

image-20240721122253777

46.d.ts 类型声明文件

image-20240721122304827

47.TS中的两种文件类型

image-20240721122315380

48.类型声明文件的使用说明

image-20240721122324510

49.使用已有的类型声明文件

49.1 内置api

image-20240721122343148

49.2 第三方库的类型声明

image-20240721122354991

49.3 definitelyTyped

image.png

49.4 创建自己的声明文件

image-20240721122444503

49.5 项目内共享类型

image-20240721122504904

49.6 为已有js文件提供类型声明

image-20240721122514015

50.【webpack】搭建一个基本的webpack4.x项目

51.React支持TS

image-20240721122539813