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>

        來玩TS嗎,機都給你開好了

        共 3807字,需瀏覽 8分鐘

         ·

        2021-09-18 17:29


        ?

        本題目旨在讓你更好的了解 TS 的類型系統(tǒng)(類型編程),編寫你自己的類型工具,或者只是單純的享受挑戰(zhàn)的樂趣!

        ?

        實現(xiàn) Pick

        實現(xiàn) TS 內(nèi)置的 Pick<T, K>,但不可以使用它。

        從類型 T 中選擇出屬性 K,構(gòu)造成一個新的類型。

        例如:

        interface Todo {
          title: string
          description: string
          completed: boolean
        }

        type TodoPreview = MyPick<Todo, 'title' | 'completed'>

        const todo: TodoPreview = {
            title: 'Clean room',
            completed: false,
        }

        答案:

        實現(xiàn) Readonly

        不要使用內(nèi)置的Readonly,自己實現(xiàn)一個。

        該 Readonly 會接收一個 泛型參數(shù),并返回一個完全一樣的類型,只是所有屬性都會被 readonly 所修飾。

        也就是不可以再對該對象的屬性賦值。

        例如:

        interface Todo {
          title: string
          description: string
        }

        const todo: MyReadonly<Todo> = {
          title: "Hey",
          description: "foobar"
        }

        todo.title = "Hello" // Error: cannot reassign a readonly property
        todo.description = "barFoo" // Error: cannot reassign a readonly property

        答案:

        元組轉(zhuǎn)換為對象

        傳入一個元組類型,將這個元組類型轉(zhuǎn)換為對象類型,這個對象類型的鍵/值都是從元組中遍歷出來。

        例如:

        const tuple = ['tesla''model 3''model X''model Y'as const

        const result: TupleToObject<typeof tuple> // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}

        答案:

        第一個元素

        實現(xiàn)一個通用First,它接受一個數(shù)組T并返回它的第一個元素的類型。

        例如:

        type arr1 = ['a''b''c']
        type arr2 = [321]

        type head1 = First<arr1> // expected to be 'a'
        type head2 = First<arr2> // expected to be 3

        答案:

        獲取元組長度

        創(chuàng)建一個通用的Length,接受一個readonly的數(shù)組,返回這個數(shù)組的長度。

        例如:

        type tesla = ['tesla''model 3''model X''model Y']
        type spaceX = ['FALCON 9''FALCON HEAVY''DRAGON''STARSHIP''HUMAN SPACEFLIGHT']

        type teslaLength = Length<tesla> // expected 4
        type spaceXLength = Length<spaceX> // expected 5

        答案:

        實現(xiàn)Exclude

        實現(xiàn)內(nèi)置的Exclude <T,U>

        從T中排除可分配給U的那些類型

        答案:

        Awaited

        假如我們有一個 Promise 對象,這個 Promise 對象會返回一個類型。在 TS 中,我們用 Promise 中的 T 來描述這個 Promise 返回的類型。請你實現(xiàn)一個類型,可以獲取這個類型。

        比如:Promise,請你返回 ExampleType 類型。

        答案:

        If

        實現(xiàn)一個If util,它接受一個C泛型參數(shù),如果為true就返回類型T,如果為false,則返回類型F

        例如:

          type A = If<true'a''b'>  // expected to be 'a'
          type B = If<false'a''b'// expected to be 'b'

        Concat

        實現(xiàn)數(shù)組的Concat類型版本

        例如:

         type Result = Concat<[1], [2]> // expected to be [1, 2]

        答案:

        Includes

        ?

        在類型系統(tǒng)中實現(xiàn)JavaScript Array.includes函數(shù)。一個類型需要兩個參數(shù)。輸出應該是一個布爾值的true或false。

        ?

        例如:

        type isPillarMen = Includes<['Kars''Esidisi''Wamuu''Santana'], 'Dio'// expected to be `false`

        這題有難度,反正我是copy的!?。?/p>

        Push

        實現(xiàn)Array.push類型版本

        例如:

          type Result = Push<[12], '3'// [1, 2, '3']

        答案:

        Unshift

        實現(xiàn)Array.unshift的類型版本

        例如:

        type Result = Unshift<[12], 0// [0, 1, 2,]

        答案:

        最后

        以上這些題我都做過,歡迎評論區(qū)留言討論

        參考文獻

        瀏覽 37
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        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>
            特级婬片AAAAAAA级附近的 | 中国老女人曰逼 | 天天日天天干天天草 | 天天综合久久综合网 | 午夜福利成人视频 | 亚洲性生活免费视频 | 一级一级大片一级大黄片操逼 | 一级黄色网址 | 亲吻呻吟打开双腿做受视频 | 国产91精品在线 |