typescript 接口

青旅半醒 2021-11-27 05:28 587阅读 0赞
  1. /**
  2. 类型检查器会查看printLabel的调用。
  3. printLabel有一个参数,并要求这个对象参数有一个名为label类型为string的属性。
  4. 需要注意的是,我们传入的对象参数实际上会包含很多属性,但是编译器只会检查那些必需的属性是否存在,并且其类型是否匹配
  5. */
  6. // function printLabel(labelledObj: { label: string }) {
  7. // console.log(labelledObj.label)
  8. // }
  9. //
  10. // let myObj = {size: 10, label: '这个属性必须是个字符串类型'}
  11. // printLabel(myObj)
  12. /**
  13. * 使用接口描述
  14. */
  15. // interface LabelValue {
  16. // label: string
  17. // }
  18. //
  19. // function printLabel(labelledObj: LabelValue) {
  20. // console.log(labelledObj.label)
  21. // }
  22. //
  23. // let myObj = {size: 10, label: '使用接口来描述'}
  24. // printLabel(myObj)
  25. /**
  26. * LabelledValue接口就好比一个名字,用来描述上面例子里的要求。
  27. * 它代表了有一个 label属性且类型为string的对象。
  28. * 需要注意的是,我们在这里并不能像在其它语言里一样,说传给 printLabel的对象实现了这个接口。我们只会去关注值的外形。
  29. * 只要传入的对象满足上面提到的必要条件,那么它就是被允许的。
  30. * 还有一点值得提的是,类型检查器不会去检查属性的顺序,只要相应的属性存在并且类型也是对的就可以。
  31. */
  32. /**
  33. * 可选属性
  34. 接口里的属性不全都是必需的。 有些是只在某些条件下存在,或者根本不存在。
  35. */
  36. interface SquareConfig {
  37. color?: string,
  38. width?: number
  39. }
  40. /**
  41. * 带有可选属性的接口与普通的接口定义差不多,只是在可选属性名字定义的后面加一个 ? 符号。
  42. */
  43. function createSquare(config: SquareConfig): { color: string, area: number } {
  44. let newSquare = {color: 'blue', area: 100}
  45. if (config.color) {
  46. newSquare.color = config.color
  47. }
  48. if (config.width) {
  49. newSquare.area = config.width * config.width
  50. }
  51. return newSquare
  52. }
  53. let mySquare = createSquare({color: 'pink'})
  54. console.log(mySquare)
  55. /**
  56. * 只读属性
  57. 一些对象属性只能在对象刚刚创建的时候修改其值。 你可以在属性名前用 readonly来指定只读属性:
  58. */
  59. interface Point {
  60. readonly x: number,
  61. readonly y: number
  62. }
  63. let p1: Point = {x: 10, y: 5}
  64. // p1.x = 5 //Cannot assign to 'x' because it is a read-only property.
  65. console.log(p1)
  66. /**
  67. * ReadonlyArray<T> 它与Array<T>相似,只是把所有可变方法去掉了,因此可以确保数组创建后再也不能被修改:
  68. */
  69. let a: number[] = [1, 2, 3]
  70. let ro: ReadonlyArray<number> = a
  71. // ro[0] = 5 //err
  72. /** ro 的内容不能再更改了 就算把整个ReadonlyArray赋值到一个普通数组也是不可以的*/
  73. /** 但是可以使用类型断言 */
  74. a = ro as number[]
  75. console.log(ro, a)
  76. /** readonly vs const
  77. 最简单判断该用readonly还是const的方法是看要把它做为变量使用还是做为一个属性。 做为变量使用的话用 const,若做为属性则使用readonly。*/
  78. /**
  79. * 函数类型
  80. * 为了使用接口表示函数类型,我们需要给接口定义一个调用签名。 它就像是一个只有参数列表和返回值类型的函数定义。参数列表里的每个参数都需要名字和类型。
  81. */
  82. interface InterFn {
  83. (age: number, fullName: string): string
  84. }
  85. let myFn: InterFn
  86. /** 函数的参数名不需要与接口里定义的名字相匹配 但要求对应位置上的参数类型是兼容的 因为函数直接赋值给了 SearchFunc类型变量*/
  87. /** 可以不指定类型 但是对应位置不能指定错误的类型 */
  88. // myFn = (age: number, fullName: string) => {
  89. myFn = (age, fullName) => {
  90. let explain = `年龄:${age} 姓名:${fullName}`
  91. return explain
  92. }
  93. let user_str = myFn(23, 'LWT')
  94. console.log(user_str)
  95. /**
  96. * 可索引的类型
  97. * 可索引类型具有一个 索引签名,它描述了对象索引的类型,还有相应的索引返回值类型。
  98. * TypeScript支持两种索引签名:字符串和数字。 可以同时使用两种类型的索引,但是数字索引的返回值必须是字符串索引返回值类型的子类型。
  99. */
  100. interface StrArr {
  101. [index: number]: string
  102. }
  103. let arrIndex: StrArr = ['LWT', 'WLP']
  104. let myName: string = arrIndex[0]
  105. console.log(myName)
  106. /**
  107. * 继承接口
  108. */
  109. interface Shape {
  110. color: string
  111. }
  112. interface Super extends Shape {
  113. age: number
  114. }
  115. let star = <Super>{}
  116. star.color = 'red'
  117. star.age = 20
  118. console.log(star.color)
  119. console.log(star.age)
  120. /** 一个接口可以继承多个接口,创建出多个接口的合成接口。 */
  121. interface Car {
  122. name: string
  123. width: number
  124. height: number
  125. }
  126. interface Price {
  127. price: number
  128. }
  129. interface Introduce extends Car, Price {
  130. }
  131. let car = <Introduce>{}
  132. car.name = '汽车'
  133. car.width = 1500
  134. car.height = 300
  135. car.price = 20000
  136. console.log(car)
  137. // car.color = 'red' 报错 Property 'color' does not exist on type 'Introduce'.
  138. /**
  139. * 混合类型
  140. * 一个对象可以同时做为函数和对象使用,并带有额外的属性。
  141. */
  142. interface ToDay {
  143. weather: string
  144. (mood: string): string
  145. funny(): void
  146. }
  147. function initToDay(): ToDay {
  148. let day = <ToDay>function (wearther: string) { }
  149. day.weather = 'sunny day'
  150. day.funny = function () { }
  151. return day
  152. }
  153. let day = initToDay()
  154. console.log(day.weather)
  155. day('sunny day')
  156. day.funny()

发表评论

表情:
评论列表 (有 0 条评论,587人围观)

还没有评论,来说两句吧...

相关阅读

    相关 TypeScript接口

    > interface接口,TypeScript使用接口定义数据类型 > 类可以实现接口,接口之间可以相互继承 > 接口只是TypeScript在开发过程中帮助做语法提

    相关 TypeScript接口

    TypeScript 的核心原则之一是对值所具有的结构进行类型检查。我们使用接口(Interfaces)来定义对象的类型。`接口是对象的状态(属性)和行为(方法)的抽象(描述)

    相关 TypeScript 接口

    接口在使用中用来规范的作用,定义一个方法需要什么参数,什么返回类型,或者属性什么类型等,实现了接口的类就要遵循接口的规矩 interface ren{ shengao

    相关 TypeScript 接口介绍

    一、介绍 TypeScript的核心原则之一是对值所具有的结构进行类型检查。接口的作用就是这些类型名称和为你的代码或第三方代码定义契约。 和C\中一样,接口定义规则,不定义

    相关 TypeScript -- ts接口

    上一节,我们简单的介绍ts的基础数据类型,那么接下来我们来介绍ts的接口。 TS的接口 > 接口是一系列抽象方法的声明,是一些方法特征的集合,这些方法都应该是抽象的,需