Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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_ )までお願いいたします。

Masashi Hirano

June 10, 2019
Tweet

More Decks by Masashi Hirano

Other Decks in Programming

Transcript

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

    LFZPG͸5 ΦϒδΣΫτ ͷϓϩύςΟ໊Λऔ͍ͬͯΔ ϓϩύςΟʹ Λ෇༩ͯ͠PQUJPOBMʹ͍ͯ͠Δ JO͸5ͷϓϩύςΟ໊Λ1ʹ֨ೲ͍ͯ͠Δɻ 5<1>͸ࢦఆͨ͠ϓϩύςΟͷܕ
  2. type Required<T> = { [P in keyof T]-?: T[P]; };

     ͸PQUJPOBMͷϓϩύςΟΛOPOPQUJPOBMʹ͢Δ 1BSUJBMͷٯͰ ΛऔΓআ͍͍ͯΔ
  3. type Readonly<T> = { readonly [P in keyof T]: T[P];

    }; ϓϩύςΟͷલʹSFBEPOMZΛॻ͘ͱͦͷϓϩύςΟ͸ಡ ΈऔΓઐ༻ʹͳΔ
  4. type Pick<T, K extends keyof T> = { [P in

    K]: T[P]; }; ,ʹࢦఆͰ͖Δͷ͸5ͷϓϩύςΟ໊ͷΈɻ5ʹແ͍ϓϩ ύςΟ໊Λࢦఆ͢ΔͱΤϥʔʹͳΔ
  5. type Record<K extends keyof any, T> = { [P in

    K]: T; }; lLFZPGBOZzlTUSJOHcOVNCFScTZNCPMz
  6. type Exclude<T, U> = T extends U ? never :

    T; ࡾ߲ԋࢉࢠͷΑ͏ʹ৚݅ʹΑͬͯܕ͕ܾ·Δ 5͕6Λܧঝ͍ͯ͠Δ৔߹͸OFWFSɺͦ͏Ͱͳ͍৔߹͸5 OFWFS͸஋Λฦ͞ͳ͍ܕɻ
  7. type Extract<T, U> = T extends U ? T :

    never; 5͕6Λܧঝ͍ͯ͠Δ৔߹͸5ɺ ͦ͏Ͱͳ͍৔߹͸OFWFSΛฦ͍ͯ͠Δɻ ฦ͢஋͕&YDMVEFͷٯʹͳ͍ͬͯΔ
  8. type NonNullable<T> = T extends null | undefined ? never

    : T; OVMM·ͨ͸VOEFpOFEΛܧঝ͍ͯ͠ΔܕͰ͋ Ε͹OFWFSɺͦ͏Ͱͳ͍৔߹͸5Λฦ͍ͯ͠Δ
  9. type Parameters<T extends (...args: any) => any> = T extends

    (...args: infer P) => any ? P : never;
  10. type Parameters<T extends (...args: any) => any> = T extends

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