一.定义函数接口
interface Fn {(name: string): void;
}
var fn: Fn = function (name): void {console.log(name);
};
fn("逍遥的码农");
二.定义类的接口
定义类需要用到关键字implements跟上定义的接口
interface Me {name: string;age: number;say(): void;
}
class My implements Me { //定义类需要用到关键字implementsname: string;age: number;constructor(name: string, age: number) {this.name = name;this.age = age;}say(): void {console.log(this.name + this.age);}
}
三.可索引接口
1.数组(数组下标为number类型 index可以自定义)
interface Indexable {[index: number]: string;
}
var arr: Indexable = ["a", "b", "c"];
console.log(arr[0]);
2.对象(对象的key为string类型 key可以自定义)
interface Obj {[key: string]: string;
}
var obj: Obj = {name: "逍遥的码农",
};
console.log(obj.name);