什么是Interfaces 接口类 (形象办法汇合)
https://www.runoob.com/typesc...
“接口” Interfaces 是一系列形象办法的申明,是一些办法特色的汇合,这些办法都应该是形象的,须要由具体的类去实现,而后第三方就能够通过这组形象办法调用,让具体的类执行具体的办法。
ts 中的 Interfaces(接口)是一个非常灵活的概念,除了可用于对类的局部行为进行形象,也能够对 对象的形态(shape) 进行形容。
简略的了解 就能够了解为一种定制化的 数据结构 规定规范
简略例子:
interface Test{ name: string; age: number; //可有可无 sex?:boolean, hobby:()=>string, }//属性数量是Test 中定义好的let demo: Test= { name: '张三', age: 18, //hobby:():string=>{ return '写代码'}, sayHi(){ return '写代码' },};
增加任意新属性
如果在应用中,Interfaces中 还存在一些任意增加的新属性
interface Test{ name: string; age: number; //可有可无 sex?:boolean, hobby:()=>string, //自定义key 任意值 [propname:string]:any, }//属性数量是Test 中定义好的let demo: Test= { name: '张三', age: 18, hobby:():string=>{ return '写代码'}, //任意新value demo1:'233', demo2:0, demo3:true, ...};