定义兼容性对象数组
T 示意传入的类型,能够为 number、string、boolean 等
传入什么 label 就是什么类型
interface labelValue<T> {
label: T;
value: number;
}
// 传入 number 类型
let obj: labelValue<number>;
export function formatYear (): labelValue<number>[] {let date = new Date().getFullYear();
let arr: labelValue<number>[];
for (let i = 0; i < date + 10; i++) {
obj = {
label: i,
value: i
}
arr.push(obj)
}
return arr;
}
// 传入 string 类型
let obj1: labelValue<string>;
export function formatHours (): labelValue<string>[] {let arr: labelValue<string>[];
for (let i = 0; i <= 24 + 10; i++) {
obj1 = {
label: i + 'h',
value: i
}
arr.push(obj1)
}
return arr;
}
定义对象型数组
定义
interface ObjectOf<V>{[_: string]: V
}
class OrderItem {
uuid: string;
amount: number;
secondaryTotal: number;
total: number;
originalTotal: number;
checked: boolean;
}
应用
interface State {
promoList: {
giftCheck: ObjectOf<OrderItem>
freeCheck: Array<OrderItem>
}
}