TS接口
对象接口
interface a {
name:string,
age:number
}
let b:a = {
name:'xiaoming',
age:18
}
console.log(b) //{name: "xiaoming", age: 18}
//设置可选属性
interface a {
name?:string,
age:number
}
let c:a = {
age:18,
}
console.log(c) //{age: 18}
//设置冗余选项
interface a {
name?:string,
age:number,
[prop:string]:any
}
let d:a = {
name:'xiaoming',
age:18,
asd:133,
asdas:5151
}
console.log(d) //{name: "xiaoming", age: 18, asd: 133, asdas: 5151}
函数接口
interface dd {
//()代表是个函数,num1:number,num2:number 指定两个参数必须都是数组 : number 表示返回值是number
(num1:number,num2:number):number
}
let tempdd : dd = (num1,num2) =>{
return num1 + num2
}
console.log(tempdd(1,3)) //4
混合类型接口
interface bibao {
count:number,
():void
}
const getNum = () : bibao => {
const ca = () => { ca.count++ }
ca.count = 0
return ca
}
const countGet : bibao = getNum()
countGet()
console.log(countGet.count) //1
countGet()
console.log(countGet.count)//2
countGet()
console.log(countGet.count)//3
//首次调用 countGet()的时候 先调用getNum() 此时返回的是ca函数再进行执行 ca 函数 此时ca.count++ ca.count = 1
//再进行调用 countGet的时候 就是直接调用ca函数了 因为getNum返回的就是 ca这个函数 不再执行getNum 也就不再重置 count = 0 了