typeScript入门
—
typeScript作为js的超集,主要是加了类型限制,对于面向对象的使用多于js,学习ts是后面学习node框架koa,express,nest等的基础
[toc]
1.类型
1 | let a:number |
ts-可以编译为任何js版本 tsc
1 | let c:boolean = false |
如果变量声明时,直接赋值(同时进行),它可以自动判断
1 | function sum(a,b) { |
类型的都为小写 number,boolean,string
- 1.number 任意数字
- 2.string 任意字符串
- 3.boolean 任意布尔值
- 4.字面量类型说明
1 | let a:10 |
5.any
1
2
3
4
5
6
7
8
9//表示可以为任意类型-
let d //--变量只声明不赋值,自动判断为any
//设置any后,相当于关闭类型检测
let d:any
let s:string
s = d -- s:string
s = d -- s将变为any6.unknown – 效果和any一致
1
2
3
4
5
6
7
8
9
10
11
12
13//any 类型的变量可以赋值给任意类型的变量
let s:string
let d:any
s = d -- s变为any
let n:unkonwn
n = 'hello'
s = n //将一个unkonwn赋给string--报错
//解决
if(typeof s==="string"){
s = n
}为一个安全的any,不能赋值给其他变量
7.类型断言
1
2
3//告诉解析器变量的实际类型
s = e as string
s = <string>e;8.void-函数返回值
1
2
3function fn(num):number{}
function fn(nunm):number|boolean{}
function fn(num):void {} //没有返回值9.never–永远不会返回结果
1
2
3
4
5function fn():never{}
//甚至不需要return
function fn():void{
return
}10.object - js一切皆对象
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25let a:object;
a = {}
//限制对象里面的属性
let b:{name:string};
//对象b必须有一个属性name-类型为string
b = {name:''} //结构和上面一致
//加上一个?表示为可选属性
let b:{name:string,age?:number}
b = {name:'',age:18}
b = {name:''}
//一个必须属性,其他根据需要来
let c = {name:string,[propName:string]:any};
//propName可以为任意字段
let c = {name:string,[name:string]:any};
c = {name:'',age:''}
c = {name:'',age:'',email:''}
//限制函数类型
let d: (a:number, b:number)=>number
//两参数一返回值
d = function (n1:string,n2:string):number{}11.array
限定数组里面值的类型
1
2
3
4
5let e:string[];
e = ['a','s']
let f:number[]
let g:array<number>12.tuple 元组
固定长度的数组
1
2
3
4
5let h:[string,string]
h = ['hello',123]
let h:[number,number]
h = [1,2]13.enum 枚举
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19let i :{name:string,gender:string}
i = {
name:'孙悟空',
gender:'男' //固定的male
}
//let i :{name:string,gender:1|0}
=>{
gender:1
}
enum Gender = {
Male = 0,
Female = 1
}
let i:{naem:string,gender:Gender}
i = {
name:'孙悟空',
gender:Gender.Male
}数组可以为-any任意类型
1
let arr:ant = [1,2,true,'a']
函数
1 | //可选参数 |
shift + space 切换全半角
1.ts编译事项
ts是不不可以直接使用的,需要编译为js才能使用
ts文件里面的形参,如果使用了某个类型进行修饰,那么最终在编译后的js文件里面,是没有这个类型的。
ts文件中的变量使用的let修饰,编译的js文件中的修饰符就变成了var
vs-code 自动编译
接口的定义与使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17//接口-一种能力一种约束
(function () {
interface Person {
name: string;
age: number;
}
console.log("这里是2.ts");
function a(person: Person): void {
console.log(person.name + " " + person.age, "-n-a-m-e");
}
const person = {
name: "wangwu",
age: 23,
};
//将接口接口作为函数参数
a(person);
})();ts里面使用一个类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23(function () {
console.log("class-ts");
interface Person {
firstName: string;
lastName: string;
}
//定义一个类
class PersonOne {
//定义公共的字段
public firstname: string;
lastname: string;
fullname: string;
constructor(firstname: string, lastname: string) {
this.firstname = firstname;
this.lastname = lastname;
this.fullname = firstname + lastname;
}
}
//实例化对象
const person = new PersonOne("诸葛", "孔明");
})();
2.webpack 打包ts
初始化npm init -y
初始化tsc
命令
1
2
3
4
5npm i webpack-cli
npm i webpack-dev-server
npm i html-webpack-plugin clean-webpack-plugin
npm i ts-loader
npm i cross-env
**报错1 **
webpack报错 TypeError: Cannot read property ‘tap‘ of undefined
原因:全局和局部版本不一样
webpack 配置
1 | const HtmlWebpackPlugin = require('html-webpack-plugin'); |
clean-webpack-plugin写法
1 |
|
“ –config build/webpack.config.js”配置
配置配置文件的路径
1 | webpack默认使用同目录下的webpack.config.js文件,也可以自定义指定文件位置 |
3.ts接口
/是对对象的状态(属性)和行为(方法)的抽象(描述)/
/是一种类型,是一种规范,是一种规则,是一个能力,是一种约束/
//定义一个接口,该接口作为person对象的限制使用,限制或者约束该对象中的属性数据//
1 | interface Dog { |
4.函数类型
为了使用接口表示函数类型,我们需要给接口定义一个调用签名
它就是一个只有参数列表和返回值类型的函数定义。参数列表里的每个参数都需要名字和类型
1 | //函数类型-通过接口的方式作为函数的类型来使用 |
5.类-class
定义一个类,这个类的类型就是上面定义的接口,实际上也可以理解为,ifly接口约束了当前的这个person类
1 | console.log("04-class-ts"); |
编译打包问题
1 | 新增文件在服务器不会在页面实现 |
因为使用webpack-dev-server
是webpack5以前的方式了,如果使用,就会在npm run start
运行时发生报错,错误信息为: `Error: Cannot find module ‘webpack-cli/bin/config-yargs’
打包ts原理
1 | 配置一个tsconfig.js |
ps:类可以通过当前接口的方式,定义当前这个类的类型
类可以实现一个接口,类可以实现多个接口,要注意,接口中的内容都要真正实现
那么可以定义一个类,继承其他需要的接口,然后再继承这个接口。
接口和接口之间叫做继承
类和接口之间叫实现
//类:可以理解为模板。通过模板可以实例化对象
1 | class Fish{ |
6.类的继承
继承:类与类之间的关系
继承后类与类的叫法
A类继承了B这个类,那么此时A类叫子类,B叫基类
子类— > 派生类
基类 —> 超类(父类
7.存取器
1 | (() => { |
8.静态成员
1 | //通过static修饰的属性或者方法,那么就是静态的属性及静态的方法,也称作未静态成员 |
9.抽象类
1 | //抽象类和抽象方法 |
10.函数
1 | (() => { |
11.可选参数和默认参数
- webpack 入口文件超过大小限制
1 | performance: { |
12.剩余参数
1 | (()=>{ |
13.函数重载
1 | (()=>{ |
14.多个泛型参数的函数
1 | (() => { |