typescript 学习笔记

2022-09-03 00:00    #typescript  

资料

安装

1yarn global add typescript ts-node

数据类型

1 let num:number = null
 1const xiaoJieJie : {
 2    name:string,
 3    age:number
 4}   = {
 5    name:'hel',
 6    age:19
 7} //1 普通
 8
 9const xiaoJjiejieS :string [] = ['1','dajiao','liuyaing']; // 2  .数组
10
11class Person{}
12const dajiao: Person = new Person() //3 类类型
13
14const jianXiaoJjieJie: () => string  = () => {return "dajiao"}; //函数对象类型

脚手架

换了一个脚手架 GitHub - xddqnodejs-typescript-modern-starter Minimal and modern starter template to develop a Node.js project leveraging Typescript

类型注解 与 类型推断

工作使用的原则

05 函数参数和返回类型的注解

demo5

06数组类型的定义

demo/demo6.ts

07 元组的使用

demo/demo7.ts

元组现在用的少了,因为可以用对象的形式来代替

08 09 interface 接口

demo/demo8.ts

demo/demo9.ts

10 类的概念和使用

demo/demo10.ts

类的访问类型

类的内部与外部

demo/demo11.ts

12 类的构造函数

demo/demo12.ts

13 类的getter setter static 只读

demo/demo13.ts

14 抽象类

demo/demo14.ts

15 16 17 tsconfig.json 配置文件

compileOptions

18 类型保护与联合类型

demo/demo18.ts

19 enum 类型 typescript 独有

demo/demo19.ts

20 泛型

demo/demo20.ts

21 类中使用泛型

为了约束传入的参数

demo/demo21.ts

22 23 命名空间

在浏览器中运行与看效果

1namespace fater {
2    namespace child {
3    }
4}

tsconfig.json

24 import

需要引用 require.js 来使用 amd 的模块

1require(["page"],function(){
2})

25 parcel

1yarn add --dev parcel

26 ts 使用jquery

1yarn add @types/jquery

or

1declare var $:any;