1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

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

        共 4794字,需瀏覽 10分鐘

         ·

        2021-08-17 10:54

        ?

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

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

        ?


        ???? 1. Objects / Functions

        • 接口和類型別名都可以用來描述對象的形狀或函數(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ò)展, 但語法有所不同. 此外, 接口和類型別名不是互斥的.
        • ? 接口可以擴(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ò)展的時候使用 extends
        • 類型別名擴(kuò)展的時候使用 &

        ???? 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

        ?

        接口可以定義多次, 自動合并到單個接口中

        ?
        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)注公眾號添加鬼哥微信,和鬼哥一起學(xué)習(xí)

        ?? 看完三件事

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

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

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

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



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

        手機(jī)掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點(diǎn)贊
        評論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            图书室里娇喘的短裙白丝校花视频 | 中文字幕视频一区 | 东京热AV无码国产东京热AⅤ | 国产超碰在线 | 一级a一级a爰片免费免软件下载 | 国产欧美一区二区三区在线看蜜臀 | 91.xxxxx | 淫香淫色综合网天天插 | 麻豆av免费 | 人妻FrXXeeXXee护士 |