作用:在面向对象的编程中,接口是一种规范的定义,它定义了行为和动作的规范,在程序设计里面,接口起到一种限制和规范的作用。 接口定义了某一批类所需要遵守的规范,接口不关心这些类的内部状态数据,也不关心这些类里方法的实现细节,它只规定这批类里必须提供某些方法,提供这些方法的类就可以满足实际需要。 它类似于
java,同时还增加了更加灵活的接口类型,包括属性、函数、可索引和类等。
TypeScript 通过 interface关键字,实现自定义接口
1. 属性接口
// 方式一:直接定义变量,进行约束function printLabel(labelInfo: { label: string }): void {console.log()}printLabel('张三'); // ×printLabel({ name: '张三' }); // ×printLabel({ label: '张三' }); // √// 方式二:使用接口 interfaceinterface LabelInfo {label: string}function printLabel(labelInfo: LabelInfo): void {console.log()}
接口可以对属性、方法进行批量约束
// 传入对象的约束,也成为属性接口interface FullName {firstName: string; // 以分号结束secondName: string;age?: number; // 可选属性}function printName(name: FullName) {// 必须传入对象包含 firstName secondNmae}
2. 函数类型接口
对方法传入的参数以及返回值进行约束
// 方式一:使用普通方式,定义函数的入参出参类型var md5 = function(key: string, value: string): string {return key + value;}// 方式二:使用接口,定义函数的入参出参类型interface encrypt {(key: string, value: string): string;}var md5: encrypt = function (key, value) {return key + value;}
3. 可索引接口
可索引类型具有一个索引签名,它描述了对象索引的类型,还有相应的索引返回值类型。
通过 number 类型索引,得到 string 类型的返回值
interface StringArray {[index: number]: string;}let myArray: StringArray;myArray = ["Bob", "Fred"];let myStr: string = myArray[0];
4. 类类型接口
对类进行约束,与抽象类有点相似。
interface Animal {name: string;eat(str: string): void;}class Dog implements Animal {name: string;constructor(name: string) {this.name = name;}eat(str: string): void {console.log(this.name + '吃粮食', str)}}
5. 接口扩展
- extends 继承
- 类和接口都可以使用它
 InterFaceA exntends InterFaceBInterFaceA extends ClassXClassA extends ClassB
 - implements 实现
- 类可以使用
 ClassA implements InterFaceX。ClassA implements ClassB。实现(implements)是面向对象中的一个重要概念。 一般来讲,一个类只能继承自另一个类,有时候不同类之间可以有一些共有的特性,这时候就可以把特性提取成接口(interfaces),用 implements 关键字来实现。这个特性大大提高了面向对象的灵活性
 
extends 类/接口的继承
interface GenericInterface<U> {value: U;getIdentity: () => U}/*** 相当于* interface Good<T> {* value: T;* getIdentity: () => T* name: string;* }*/interface Good<T> extends GenericInterface<T> {name: string;}
class Father {name: string;constructor() {this.name = 'father';}run(): void {console.log('Running')}}/*** FatherProps 继承了 Father 的 name 属性 和 run 方法*/interface FatherProps extends Father {add(a: number, b: number): number}
class Father {name: string;constructor() {this.name = 'father';}run(): void {console.log('Running')}}class Son extends Father {say() {console.log('Saying')}}
implement 类的实现
interface IProps {setState(state: object): void}class Son implements IProps {state: object;constructor() {this.state = {}}setState(state: object) {this.state = state}}
class Father {name: string;constructor() {this.name = 'father';}run(): void {console.log('Running')}}class Son implements Father {name: stringconstructor() {this.name = 'son';}run(): void {console.log('Running son')}}
综合使用
interface IProps {setState(state: object): void}interface IFatherProps extends IProps {name: string;run(): void;}interface ISonProps extends IFatherProps {say(message: string): void}class Father implements IFatherProps {state: object;name: string;constructor(name: string) {this.name = name;this.state = {}}setState(state: object) {this.state = state;}run(): void {console.log('Running')}}class Son extends Father implements ISonProps {constructor(name: string) {super(name);this.name = name;}run(): void {console.log('Running son')}say(message: string) {console.log(this.name)}}
