美文网首页
typescript接口声明

typescript接口声明

作者: 会飞得鼠 | 来源:发表于2019-03-04 23:00 被阅读0次

typescript 检查变量类型的时候其中采用的一个方法是“鸭子类型判断法”

"所谓鸭子类型判断法也就是指看起来像,叫声是鸭子,那么他就是鸭子"

interface声明方式

interface 只能声明对象类型并且只做声明不实现

interface与class声明方式差异

1.interface在编译后就会被删除class会被保留

//数组

interface ArrayInterface  {

    [key:number]:string

}

const ary:ArrayInterface = [

    "A","B","C"

];

//方法

interface funInterface {

    (x:string,y:string):string

}

let fun:funInterface;

fun =(x:string):string =>{

    return ""

}

//对象

interface ObjInterface {

    name:string,

    age:number

}

const obj:ObjInterface = {

    name:"test",

    age:18

}

//类型转换 转换后的类型自动继承了接口的其他方法

const json = {name:"json"};

const newJson = <ObjInterface>json;

newJson.age = 18;

interface在线格式化地址

https://app.quicktype.io/

相关文章

网友评论

      本文标题:typescript接口声明

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