TypeScript笔记 -- 接口约束函数和数组

54 阅读1分钟

1.约束数组

interface IMyArray {
    [index: number]: number
}
//正确
let arr:IMyArray = [1,2,3]
//错误
let arr:IMyArray = [1,2,'']

2.约束函数

interface ISearchFunc {
    //(参数:类型,.....):返回值类型
   (a: string, b:string): boolean
}
const fun1:ISearchFunc = function(a:string, b:string):boolean {}