ts接口03
// 接口 对对象的形状进行描述 可以理解为一种约束 // ?表示为可选属性,表示可有可无 // [prop:string] 代表任意属性,当不确定属性名的时候,属性类型,可以使用 ,但是要注意的是,一旦确定了不是any类型,而是string,number,Boolean之类的,其他的类型也会变成他的子集 // [prop:string] 中如果有多个属性,可以使用联合类型,如: [prop:string]:string|number|Boolean // readonly 只读属性 // 定义接口 注意:接口是首字母要大写 interface Person{ readonly id:number; name:string, age:number, sex?:string, [prop:string]:any } let p:Person={ id:1, name:'张三', age:132, // sex:'111' www:111 }
数组类型
// 该方法不常用 interface INewArray{ [index:number]:number|string //index表示数组中的下标 } let arr01:INewArray=[1,4,5,6,""]
函数类型
// 函数类型 interface ISearchFunc{ (a:string,b:string):boolean //(参数:类型):返回值 } const fun1:ISearchFunc = function(a:string,b:string):boolean{ return a.search(b) !==-1 } fun1('222','111')
代码改变了我们,也改变了世界