TypeScript3(接口和对象类型)

在typeScript里,我们定义对象的方式用interface(接口),约束数据结构的格式。

//不可多属性,不可少属性
//这样写会报错,因Person定义a,b,而对象person缺少b属性
interface Person{
    a:string,
    b:string
}

const person:Person={
    a:'232'
}

//2.接口重名会合并

//interface A{a:string,b:()=>{}}

interface A{

    a:string

}

interface B{

    b:()=>{}

}

//3.继承

interface A{

    name:string

}

interface b extends A{

    age:number

}

//4.可选属性?操作符

//可以允许属性不存在

interface Person{

    b?:string,

    a:string

}



const person:Person = {

    a:'123'

}

//5.任意key(属性)

//定义任意属性后,确定属性和可选属性都必须是它的类型子集

//6.readonly只读属性不许赋值

interface Person1{

    a1?:string,

   readonly b2:string,

    [propName:string]:any

}



const ife:Person1={

    b2: "213",

    c: "4",

}
//ife.b2这样会报错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值