<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          TypeScript: interfaces 與 type 的區(qū)別和相同點(diǎn)

          共 4794字,需瀏覽 10分鐘

           ·

          2021-08-17 10:54

          ?

          鬼哥本周將通過(guò)幾篇優(yōu)秀的Typescript文章,讓大家學(xué)習(xí)到Typescript一些高級(jí)的用法,讓大家對(duì)Typescript更加的深入理解,并且更好實(shí)踐到工作當(dāng)中,【一共七篇文章】,關(guān)注我們一起完成這個(gè)系列的學(xué)習(xí)

          原文:https://github.com/leslie1943

          ?


          ???? 1. Objects / Functions

          • 接口和類型別名都可以用來(lái)描述對(duì)象的形狀或函數(shù)簽名
          • 使用接口定義
          // 使用接口
          interface Point{
              x: number
              y: number
          }

          interface SetPoint {
              (x:number, y:number): void
          }
          • 使用類型別名定義
          type Point = {
              x: number
              y: number
          }

          type SetPoint = (x: number, y: number) => void;

          ???? 2. Other Types

          • 與接口類型不同, 類型別名可以用于一些其他類型, 比如原始類型, 聯(lián)合類型和元組
           // 原始類型
           type Name = string

           // const name: Name = 22
           const name: Name = 'sss'

           // Object
           type PartialPointX = { x: number }
           type PartialPointY = { y: number; z: number }

           // Union Object
           type PartialPoint = PartialPointX | PartialPointY
           const pointx: PartialPointX = { x: 1 }
           const pointy: PartialPointY = { y: 1, z: 88 }
           const pointer: PartialPoint = { x: 100, y: 200, z: 300 }

           // Tuple
           type Data = [numberstring]
           const dataArr: Array<Data> = [[1'ss']]
           const data: Data = [1'ss']

          ???? 3 Extend

          • 接口和類型別名都能夠被擴(kuò)展, 但語(yǔ)法有所不同. 此外, 接口和類型別名不是互斥的.
          • ? 接口可以擴(kuò)展類型別名
          • ? 類型別名不能擴(kuò)展接口

          ??interface extends interface

            interface PartialPointX {
                x: number
            }
            interface Point extends PartialPointX {
                y: number
            }
            const point: Point = { x: 11, y: 22 }

          ??Type Alias extends type alias

            type FirstName = { first: string }

            type FullName = FirstName & { last: string }

            const fullName: FullName = { first: 'su', last: 'zhen

          ??interface extends type alias

           type PartialPointX = {
               x: number
           }

           interface Point extends PartialPointX {
               y: number
           }

           const point: Point = {
               x: 1,
               y: 2,
           }

          ??type alias extends interface

           interface FirstName {
               first: string
           }

           type FullName = FirstName & { last: string }
               const name: FullName = {
               first: 'su',
               last: 'zhen',
           }
          • 接口擴(kuò)展的時(shí)候使用 extends
          • 類型別名擴(kuò)展的時(shí)候使用 &

          ???? 4 Implements

          • 類可以以相同的方式實(shí)現(xiàn)接口或類型別名 ???
          • 但類不能實(shí)現(xiàn)使用類型別名定義的聯(lián)合類型 ???
          interface Point {
            x: number
            y: number
          }

          class SomePoint implements Point {
            x = 0
            y = 0
          }

          type Point2 = {
            x: number
            y: number
          }

          class SomePoint2 implements Point2 {
            x = 0
            y = 0
          }

          type PartialPoint = { x: number } & { y: number }
          // A class can only implement an object type or
          // intersection of object types with statically known members.ts(2422)
          // class SomePartialPoint implements PartialPoint {} // Error ?

          ???? 5 Declaration merge

          ?

          接口可以定義多次, 自動(dòng)合并到單個(gè)接口中

          ?
          interface Point {
            x: number
          }

          interface Point {
            y: number
          }

          interface Point {
            y: number
          }

          interface Point {
            z: number
          }

          const point: Point = {
            x: 1,
            y: 2,
            z: 3,
          }
          • 類型別名只允許定義一次
          type Name = {
            first: string
          }

          type Name = {
            last: string
          }
          Duplicate identifier 'Name'.ts(2300)

          關(guān)注公眾號(hào)添加鬼哥微信,和鬼哥一起學(xué)習(xí)

          ?? 看完三件事

          如果你覺得這篇內(nèi)容對(duì)你挺有啟發(fā),不妨:

          • 點(diǎn)個(gè)【在看】,或者分享轉(zhuǎn)發(fā),讓更多的人也能看到這篇內(nèi)容

          • 點(diǎn)擊↓面關(guān)注我們,一起學(xué)前端

          • 長(zhǎng)按↓面二維碼,添加鬼哥微信,一起學(xué)前端



          瀏覽 67
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲日产AV一二三四区小说 | 日本xxxxxwww18 | 黄色一级免费视屏 | 亚洲一级操逼片 | 国产乱伦小视频 |