typescript——接口

本文详细介绍了 TypeScript 中的接口概念,包括属性接口用于约束 JSON 对象、函数接口约束方法参数和返回值、可索引接口对数组和对象的定义,以及类类型接口如何约束类。此外,还提及了接口的扩展与继承。
摘要由CSDN通过智能技术生成

属性接口:对 json 的约束

function printLabel (json: { label: string }): void {
  console.log(json)
}

printLabel({ label: '' })

上面的方式传 json 太繁琐

假如对批量方法传入参数进行约束

interface FullName {
  firstName: string
  secondName: string
}

function printName (name: FullName) {
  console.log(name.firstName + '---' + name.secondName)
}

let obj = { // firstName 和 secondName 必须有,否则编译错误
  firstName: '肖',
  secondName: '泽',
}
printName(obj) // 肖---泽

可选属性接口

interface FullName {
  firstName: string
  secondName?: string
}

function getName (name: FullName) {
  console.log(name)
}

getName({ firstName: 'xiao' })

实例:ajax 接口封装

interface Config {
  type: string
  url: string
  data?: string
  dataType: string
}

function ajax (config: Config) {
  let xhr = new XMLHttpRequest()
  xhr.open(config.type, config.url, true)
  xhr.send(config.data)
  xhr.onreadystatechange = function () {
    if (xhr.readyState === 4 && xhr.status === 200) {
      console.log('success')
      if (config.dataType === 'json') {
        console.log(JSON.parse(xhr.responseText))
      }
    }
  }
}

ajax({
  type: 'get',
  data: 'name=xiaoze',
  url: 'http://www.baidu.com',
  dataType: 'json',
})

函数接口:对方法传入的参数,以及返回值进行约束

加密函数类型接口

interface encrypt {
  // 传入参数要求 string , 返回值类型要求 string ,否则会编译错误
  (key: string, value: string): string
}

let md5: encrypt = function (key: string, value: string): string {
  // 加密算法忽略
  return key + value
}

md5('name', ' bob') // name bob

可索引接口:数组、对象的接口(不常用)

数组

interface UserArr {
  [index: number]: any
}

let arr: UserArr = ['123', 234]
console.log(arr) // [ '123', 234 ]

对象

interface UserObj {
  [index: string]: any
}

let obj: UserObj = { name: 'bob', age: 21 }
console.log(obj) // { name: 'bob', age: 21 }

类类型接口:对类的约束,与抽象类有点类似

interface Animal {
  name: string
  eat (str: string): void
}

class Dog implements Animal { // 类必须实现接口定义属性和方法,否则编译错误
  name: string

  constructor (name: string) {
    this.name = name
  }

  eat (something: string) {
    console.log(`${this.name} eat ${something}`)
  }
}

let dog = new Dog('tom')
dog.eat('foods') // tom eat foods

接口的扩展:继承接口

interface Animal {
  eat (): void
}

interface Person extends Animal {
  work (): void
}

class Programmer {
  name: string

  constructor (name: string) {
    this.name = name
  }

  coding (code: string) {
    console.log(`${this.name} is ${code}`)
  }
}

// 必须包含 类类接口的符和子的继承
class Man extends Programmer implements Person {
  name: string

  constructor (name: string) {
    super(name)
    this.name = name
  }

  eat (): void {
    console.log(`${this.name} is eating`)
  }

  work (): void {
    console.log(`${this.name} is working`)
  }
}

let bob = new Man('bob')
bob.eat() // bob is eating
bob.work() // bob is working
bob.coding('coding') // bob is coding
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值