三、typescript 接口定义/继承/类

三,接口

interface 定义接口

   function printObj(labelObj:{label:string}){
     console.log(labelObj.label)
    }
    let newObj = {size:10,label:'label obj'}
    printObj(newObj)
必须给label属性类型为string
   interface labelType{
        label:string
    }
    function printObj(labelObj:labelType){
        console.log(labelObj.label)
    }
    let myObj = {size:100,label:'label'}
    printObj(myObj)
可选属性  ?: 
 interface myConfig{
        color?:string,
        width?:number
    }
   function createConfig(config:myConfig):{color:string;width:number}{
        let newConfig = {color:'pink',width:100}
        if(config.color){
            newConfig.color = config.color 
        }
        if(config.width){
            newConfig.width = config.width
        }
        return newConfig
    }
    let configV = createConfig({color:'black'})
    console.log('config',configV)

只读属性 readonly

  interface Point {
            readonly x: number;
            readonly y: number
        }
    let p1: Point = { x: 10, y: 20 }
    p1.x = 5 //error

额外的属性检查

  interface Config{
        color?:string;
        width?:number;
        [propName:string]:any;   //额外属性
    }
    

函数类型

   interface SearchFun {
        (value: string, sub: string): boolean
    }
    let mySearch: SearchFun;
    mySearch = function (value: string, sub: string): boolean {
        let res = value.search(sub)
        return res > -1
    }
    console.log('mySearch', mySearch)

可索引的类型 签名和索引赋值类型需要相同

   interface StringArr {
        [index: number]: string
    }
    let myArr: StringArr = ['aaa', 'bbb']
    let myStr: string = myArr[0]    //number   string
    console.log('mystr', myStr)
类类型
interface ClockFace{
    curTime:Date;
    setTime(d:Date)
}
class Clock implements ClockFace{
    curTime:Date;
    setTime(d:Date){
        this.curTime = d
    }
    constructor(h:number){
    }
}
console.log('Clock',Clock)

静态部分和实例

  interface ClockConstructor {
        new(hour: number, minute: number): ClockInterface;
    }
    interface ClockInterface {
        tick();
    }
    function createClock(ctor: ClockConstructor, hour: number, minute: number): ClockInterface {
        return new ctor(hour, minute)
    }
    class AClock implements ClockInterface {
        constructor(hour: number, minute: number) {
            console.log(hour, '---', minute)
        }
        tick() {
            console.log('aClock')
        }
    }
    class BClock implements ClockInterface {
        constructor(h: number, m: number) {
            console.log(h, '====', m)
        }
        tick() {
            console.log('bclock')
        }
    }
    let aCreate = createClock(AClock, 10, 11)
    let bCreate = createClock(BClock, 22, 11)
    console.log('aCreate:', aCreate)
    console.log('bCreate', bCreate)
继承接口
interface Shape1{
    color:string
}
interface Shape2{
    width:number
}
//可一次继承多个接口
interface ChildShape extends Shape1,Shape2{
    height:number
}
let myShape = <ChildShape>{}
myShape.color='pink'
myShape.width=100
myShape.height=100
console.log('myShape',myShape)
混合类型
interface Counter{
    (start:number):string;
    interval:number;
    reset():void;
    (propName:String):any
}
function getCounter():Counter{
    let counter = <Counter>function(start:number){}
    counter.interval=123
    counter.reset=function(){}
    return counter
}
let cc = getCounter()
cc(10)
cc.reset()
cc.interval=2
console.log('cc:',cc)
接口继承类
class Control{
    private state:any
}
interface SelectControl extends Control{
    select():void
}
class Button extends Control implements SelectControl{
    select(){}
}
class TextBox extends Control{
    select(){}
}
//error 缺少state属性
class Image implements SelectControl{
    select(){};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

葫芦娃y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值