TS中接口的使用

作者: 深度剖析JavaScript | 来源:发表于2020-11-05 08:09 被阅读0次

接口是一种规范,跟抽象类有点类似

通过 interface关键字定义接口,格式如下:

interface xxx{
  属性: 类型 ;
  ...
  (函数参数) : 返回值类型
  ...
}

interface一般用于规范三个东西:

  1. 函数
  2. 构造器
一、函数interface

函数interface可规范函数的参数及返回值

interface xxx{
    (参数,...):返回值类型
}

例如

interface SearchFn {
    (key: string, page?: number): number[]
}
const search: SearchFn = function (key: string, page: number) {
    return [1, 2, 3]
}

我们可以看到,interface规范函数时,interface相当于type
当一个函数类型是接口时,要求:

  1. 参数名可不一致,但参数的值类型必须与接口定义的对应,且参数可以少不可多;
  2. 返回值必须有,且与接口定义的一致;
一、类interface

我们知道,继承的好处是复用代码,并且层级关系清晰。但JavaScript中继承是单继承,一个类只能有一个父类。而在TypeScriptinterface会显得更加灵活,因为interface可以多实现
例如:

interface serialization {
    toJSON(): object;
    formJSON(json: object): void;
}
class Box implements serialization {
    width: number;
    height: number;
    constructor(width:number,height:number){
        this.width = width;
        this.height = height;
    }
    toJSON(): object {
        return { width: this.width, height: this.height }
    }
    formJSON(json: object): void {
        if (isSize(json)) {
            this.width = json.width;
            this.height = json.height;
        }
    }
}
function isSize(json: any): json is { width: number, height: number } {
    if (typeof json != 'object') {
        console.log('必须是object类型');
    } else {
        if (typeof json.width != 'number' || typeof json.height != 'number') {
            console.log('width 和 height 必须是number类型!!')
        }
    }
    return true;
}
let box = new Box(50,50)

如上,通过implements关键字可以让一个类实现一个接口,要求必须实现时间接口定义的方法,否则会出错

三、构造函数interface

构造函数interface比较特殊,是通过赋值的形式来实现,并且得跟普通interface区分开,普通interface还是使用implements。另外在接口中使用new指代构造器

interface BoxConstructorInterface{
    new (a:string)
}
interface BoxInterface{
    show(a:number):void;
}
const Box:BoxConstructorInterface = class implements BoxInterface {
    private a:string;
    constructor(a:string){
        this.a = a;
    }
    show(a:number){
        console.log(this.a)
    }
}

另外,跟类一样,interface也能继承另外一个interface
例如:

interface A { }
interface B extends A { }
class C implements B { }

所以,我们知道了,接口本身只是一种规范,里头定义了一些必须有的属性或者方法,接口可以用于规范functionclass或者constructor,只是规则有点区别

以上就是TS中interface的基本使用!

相关文章

  • 8、TypeScript 接口继承接口,类实现多接口

    1、ts类中只能继承一个父类2、ts类中可以实现多少接口,使用(,)号分隔3、ts接口中可以继承多个接口,使用(,...

  • typeScript语法

    ts类型 ts联合类型使用或 ts定义任意类型any ts定义函数返回值的类型 ts中的类定义 interface接口

  • TS中接口的使用

    接口是一种规范,跟抽象类有点类似 通过 interface关键字定义接口,格式如下: interface一般用于规...

  • TypeScript 学习笔记 之 接口与类

    接口 TS 中判断是否实现接口的核心原则是基于结构而不是基于名称的。即鸭子类型判断。 TS 中接口可以声明可选的接...

  • TS中的接口

    本文目录: 1.接口的概念 2.可选属性和只读属性 3.任意属性 4.函数类型 5.可索引属性 6.类接口 8.接...

  • 【umiJS】在非组件中调用 dispatch

    需求:每次请求任意接口,都调用A接口获取未读消息数量,然后给页面公共tags使用。 这需要在request.ts中...

  • ng-alain中的st表格

    后端控制分页的st表格使用 数据列初始化 ts 后端接口获取data ts 表格分页设置 ts change回调 ...

  • TypeScript爬虫案例

    使用TS中的一些语法特性,比如接口等语法,实现一个简单的爬虫示例代码。

  • TS基础(五)接口

    TS中接口是用来定义对象的类型。TS中接口和泛型属于十分重要的类型。在面向对象语言中,接口它是对行为的抽象,行动由...

  • typescript

    typescript ts暂时不被支持,所以需要编译使用;ts补充了js没有的语法特性,例如类型,接口,抽象等;由...

网友评论

    本文标题:TS中接口的使用

    本文链接:https://www.haomeiwen.com/subject/ibfovktx.html