文章目录
基础类型


一、数组(Array)
let list1: number[] = [1,2,3,4]
let list2: Array<number> = [1,2,3,4](泛型)
let list3 = [1,2,3,4]
let list4 = [1,"666"] //(string|number)[]
let list5: any[] = [1,"888",true] //any[]
二、元组(Tuple)
let person1: [number,string] = [1,"7878"]
(固定长度,固定类型的数组)
bug:使用push可以突破元组
三、联合类型(Union)
let union: string | number
let union2: string | number | boolean | string[]
四、字面量类型(Literal)
let union2: 0 | 1 | 2
五、枚举类型(Enum)
enum Color{
red,green,blue
}
let color = Color.blue
console.log(color)
如果blue未赋值,输出内容应该是11
enum Color{
red = 5,
green = 10,
blue = 12
}
let color = Color.blue
console.log(color) //12
enum Color{
red = 5,
green = 10,
blue = 'BBLLUUEE'
}
let color = Color.blue
console.log(color) //BBLLUUEE
六、Any
适合代码快速成型,快速上线,有安全隐患
let randomValue: any = 666
七、unknow
不保证类型,但是能保证类型安全
let randomValue: unknow = 666
八、void(原生没有void)
没有返回值(变量本身就不存在)
function hello() : void{
console.log("hello")
}
九、undefined
不存在(变量未赋值或初始化)
function hello() : undefined{
console.log("hello")
return
}
十、never(常用于处理异常)
一个函数永远执行不完
function throwError(message: string,errorCode: number): never{
throw{
message,errorCode
}
}
throwError('not Found',404)
function whileLoop(true): never{
while(true){
console.log("haha")
}
}
十一、类型适配(类型断言)Type Assertions
let message : any
message="abc"
let aaa = (<string>message).endWith("c")
let bbb = (message as string).endWidth("c")
十二、函数类型
let log = function(message){
console.log(message)
}
let log1 = (message : string) => console.log(message)
log1("hello")
let log2 = (message: string , code : number) => console.log(message,code)
log2("hello",2)
加上问号,可省略此参数:code(加上问号或者默认值的数据需放在后面,否则会报错)
let log3 = (message: string , code?: number) => console.log(message,code)
log3("hello",2) //hello 2
log3("hello") //hello undefined
可设置默认值:code(加上问号或者默认值的数据需放在后面,否则会报错)
let log4 = (message: string , code: number=123) =>{ console.log(message,code)
}
log4("hello",99) //hello 99
log4("hello") //hello 123
十三、Object对象类型
完全可以省略object
const person: object= {
firstName:"张三",
lastName:"李四",
age:18
}
console.log(person.age)
十四、interface接口
let draw = (x,y) => {
console.log({x,y}) //{ x: 1, y: 2 }
}
draw(1,2)
let draw = (point: Point) => {
console.log({ x:point.x , y:point.y })
}
draw({ x:100 , y:200 }) //{ x:100 , y:200 }
draw({ x:'张三' , y:'李四' }) //不符合要求
interface Point {
x:number;
y:number;
}
高内聚,低耦合
高内聚:功能相关的内容应该放在同一个模块里边
低耦合:
十五、class类
interface IPoint{
x:number;
y:number;
drawPoint:()=>void
getDistances: (p: IPoint) => number
}
class Point implements IPoint{
//x:number;
//y:number;
//使用public时不可以再用?(问号,可缺少)
constructor( public x:number,public y:number=2){
//this.x = x;
//this.y = y;
}
drawPoint = () => {
console.log("x:",this.x,",y:",this.y)
}
getDistances = (p: IPoint) => {
return Math.pow(p.x-this.x,2) + Math.pow(p.y-this.y,2)
}
}
const point = new Point(288,499)
point.drawPoint()
十六、Access Modifier 访问修饰符(public、private、protected)
1)public(公共的):被 public所修饰的属性和方法可以被有类访问。 所修饰的属性和方法可以被有类访问。
2)private(私有的):被 private所修饰的属性和方法只能在该类内部使用
3)protected (受保护的):被protected所修饰的属性和方法可以在类内部、相同包以及该类的子所访问。
4)默认的(不加任何访问修饰符):在类内部以及相同包下面的所使用。
十七、Module模块(export)
export
十八、Generics泛型
let lastInArray=<T>(arr: Arry<T>)=>{
return arr[arr.length-1];
}
等同于
let lastInArray=<T>(arr: T[])=>{
return arr[arr.length-1];
}
const l1 = lastInArray([1,2,3,4]) //4
const l2 = lastInArray<string>(["a","b","c","d"]) //d
const l3 = lastInArray<string | number>(["a",2,"c",8]) //8
console.log(l1,l2,l3)
多个泛型类型,用逗号隔开
let make=<T,Y>(x:T,y:Y)=>[x,y]
const v1 = make(1,"one")
const v2 = make<boolean,number>(true,2)
console.log(v1,v2)