普通接口
1.使用interface定义
2.项的key后加?作为可选项
3.[propName: string]: any;作为剩余参数
interface LabelledValue {
label: string,
name?: string
}
const inter = function (obj: labelObj) {
console.log(obj.label);
};
inter({ label: 'this is label' });
函数类型
interface SearchFunc {
// 函数参数 // 函数返回值
(source: string, subString: string): boolean;
}
可索引的类型(数组和对象)
索引规则 (只能设置number和string)
属性名为number时, 索引返回值和其他属性返回值可不同
属性名为string时,索引返回值和其他返回值类型必须一致
// 数组
interface interArray {
// 数组有index属性 数组的每一项为string类型
// readonly [index: number]: string 前面加readonly设置只读
[index: number]: string,
length: number
}
const myArr: interArray = ['name1', 'name2']