<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有什么冷門但是很好用的特性?

          共 1652字,需瀏覽 4分鐘

           ·

          2021-09-24 23:55

          獲取一個(gè)值的類型

          let defaultState = {    foo: 7,    bar: 'hello'};
          type State = typeof defaultState;
          let nextState: State = { foo: 'seven', bar: 'world'};// 不能將類型“{ foo: string; bar: string; }”分配給類型“{ foo: number; bar: string; }”。// 屬性“foo”的類型不兼容。// 不能將類型“string”分配給類型“number”。


          獲取一個(gè)函數(shù)的返回值的類型

          function getState() {    return {        foo: 7,        bar: 'hello'    };}
          type State = ReturnType<typeof getState>;
          let nextState: State = { foo: 'seven', bar: 'world'};// 不能將類型“{ foo: string; bar: string; }”分配給類型“{ foo: number; bar: string; }”。// 屬性“foo”的類型不兼容。// 不能將類型“string”分配給類型“number”。


          將一個(gè)類型中的所有屬性都變成可選屬性

          let defaultState = {    foo: 7,    bar: 'hello'};
          type PartialState = Partial<typeof defaultState>;
          let partialState: PartialState = { foo: 8};


          取出一個(gè)類型中的部分屬性,生成另一個(gè)類型

          let defaultState = {    foo: 7,    bar: 'hello'};
          type PickedState = Pick<typeof defaultState, 'foo'>;
          let partialState: PickedState = { foo: 8, bar: 'world'};// 不能將類型“{ foo: number; bar: string; }”分配給類型“Pick<{ foo: number; bar: string; }, "foo">”。// 對(duì)象文字可以只指定已知屬性,并且“bar”不在類型“Pick<{ foo: number; bar: string; }, "foo">”中。


          還有一些其他內(nèi)置工具類型,可以在把鼠標(biāo)放到 ReturnType 上,按 F12 查看

          type Readonly<T> = {    readonly [P in keyof T]: T[P];}
          type Partial<T> = { [P in keyof T]?: T[P];}
          type Nullable<T> = { [P in keyof T]: T[P] | null;}
          type Proxy<T> = { get(): T; set(value: T): void;}
          type Pick<T, K extends keyof T> = { [P in K]: T[P];}
          type Record<K extends string, T> = { [P in K]: T;}


          瀏覽 59
          點(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>
                  人人爽,人人妻,人人操 | 欧美一区二区三区的 | 国产亚洲日韩欧美蝌蚪窝91视频 | 天天日天天干天天舔 | 亚洲三级先锋影音 |