接口类型
对象接口
interface IUser {
name: string;
age: number;
sex: "男" | "女";
}
let user: IUser;
user = {
name: "zhangsan",
age: 14,
sex: "男",
};
数组接口
interface IArr {
// 下标类型为 number,值类型为 number | string
[ids: number]: number | string;
}
let ids: IArr = [1, 2, "3", "4"];
函数接口
interface IFun {
(str: string, num: number): void;
}
let fn: IFun = (name: string, age: number) => {
console.log(name + "今年" + age + "岁");
};
fn("张三", 18);