Advanced built-in types / #kansaits 1

Advanced built-in types / #kansaits 1

kansai.ts #1 ( https://kansaits.connpass.com/event/130120/ ) でTypeScriptの便利なbuilt-in typesについて話をしました。
質問はTwitter( https://twitter.com/shisama_ )までお願いいたします。

5cf7e9533a457726cd51232e06c1da9a?s=128

Masashi Hirano

June 10, 2019
Tweet

Transcript

  1. 8.

    type Partial<T> = { [P in keyof T]?: T[P]; };

    LFZPG͸5 ΦϒδΣΫτ ͷϓϩύςΟ໊Λऔ͍ͬͯΔ ϓϩύςΟʹ Λ෇༩ͯ͠PQUJPOBMʹ͍ͯ͠Δ JO͸5ͷϓϩύςΟ໊Λ1ʹ֨ೲ͍ͯ͠Δɻ 5<1>͸ࢦఆͨ͠ϓϩύςΟͷܕ
  2. 10.
  3. 13.

    type Required<T> = { [P in keyof T]-?: T[P]; };

     ͸PQUJPOBMͷϓϩύςΟΛOPOPQUJPOBMʹ͢Δ 1BSUJBMͷٯͰ ΛऔΓআ͍͍ͯΔ
  4. 15.
  5. 18.

    type Readonly<T> = { readonly [P in keyof T]: T[P];

    }; ϓϩύςΟͷલʹSFBEPOMZΛॻ͘ͱͦͷϓϩύςΟ͸ಡ ΈऔΓઐ༻ʹͳΔ
  6. 20.
  7. 23.

    type Pick<T, K extends keyof T> = { [P in

    K]: T[P]; }; ,ʹࢦఆͰ͖Δͷ͸5ͷϓϩύςΟ໊ͷΈɻ5ʹແ͍ϓϩ ύςΟ໊Λࢦఆ͢ΔͱΤϥʔʹͳΔ
  8. 25.
  9. 28.

    type Record<K extends keyof any, T> = { [P in

    K]: T; }; lLFZPGBOZzlTUSJOHcOVNCFScTZNCPMz
  10. 30.
  11. 32.
  12. 35.

    type Exclude<T, U> = T extends U ? never :

    T; ࡾ߲ԋࢉࢠͷΑ͏ʹ৚݅ʹΑͬͯܕ͕ܾ·Δ 5͕6Λܧঝ͍ͯ͠Δ৔߹͸OFWFSɺͦ͏Ͱͳ͍৔߹͸5 OFWFS͸஋Λฦ͞ͳ͍ܕɻ
  13. 37.
  14. 40.

    type Extract<T, U> = T extends U ? T :

    never; 5͕6Λܧঝ͍ͯ͠Δ৔߹͸5ɺ ͦ͏Ͱͳ͍৔߹͸OFWFSΛฦ͍ͯ͠Δɻ ฦ͢஋͕&YDMVEFͷٯʹͳ͍ͬͯΔ
  15. 42.
  16. 45.

    type NonNullable<T> = T extends null | undefined ? never

    : T; OVMM·ͨ͸VOEFpOFEΛܧঝ͍ͯ͠ΔܕͰ͋ Ε͹OFWFSɺͦ͏Ͱͳ͍৔߹͸5Λฦ͍ͯ͠Δ
  17. 49.

    type Parameters<T extends (...args: any) => any> = T extends

    (...args: infer P) => any ? P : never;
  18. 50.

    type Parameters<T extends (...args: any) => any> = T extends

    (...args: infer P) => any ? P : never; JOGFS͸ܕਪ࿦ͨ݁͠ՌΛऔΔ͜ͱ͕Ͱ͖Δ ͜ͷ৔߹ؔ਺ͷҾ਺ͷܕΛਪ࿦͍ͯ͠Δɻ 5͕ؔ਺ܕ ʜBSHTJOGFS1 BOZͷܧঝͩͬͨ৔߹ɺ ਪ࿦ͨ͠Ҿ਺ͷܕΛฦ͍ͯ͠Δ 5͸ؔ਺ܕͷΈࢦఆͰ͖Δ