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. Advanced built-in types you may not know Masashi Hirano @shisama

    kansai.ts #1
  2. ฏ໺ ণ࢜ / Masashi Hirano @shisama_ @shisama Node.js Core Collaborator

    ؔ੢NodeֶԂOrganizer
  3. TypeScriptʹbuilt-in͞Ε͍ͯΔศརͳܕʹ ͍ͭͯ঺հ͠·͢ʂ

  4. https://www.typescriptlang.org/docs/handbook/advanced-types.html ެࣜͷAdvanced TypesಡΜͩ͜ͱ͋Δ?

  5. Advanced Types • Conditional Types΍Mapped TypesͳͲෳࡶͳܕ΍Union Types ͳͲศརͳه๏ͳͲΛ঺հ͍ͯ͠Δެࣜͷϖʔδ • ࠓճ͸ͦͷத͔Βbuilt-in͞Ε͍ͯΔศརͳܕΛ঺հ͠·͢


    ※ܕఆٛΛಡΉʹ͸Conditional Types΍Mapped Types͕ಡ ΊΔඞཁ͕͋Γ·͢ɻ
  6. https://github.com/microsoft/TypeScript/blob/master/src/lib/es5.d.ts 5ZQF4DSJQUͷ(JU)VCϦϙδτϦͷ TSDMJCFTEUTʹఆ͕ٛ͋Δ

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

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

    LFZPG͸5 ΦϒδΣΫτ ͷϓϩύςΟ໊Λऔ͍ͬͯΔ ϓϩύςΟʹ Λ෇༩ͯ͠PQUJPOBMʹ͍ͯ͠Δ JO͸5ͷϓϩύςΟ໊Λ1ʹ֨ೲ͍ͯ͠Δɻ 5<1>͸ࢦఆͨ͠ϓϩύςΟͷܕ
  9. Partial ͢΂ͯͷϓϩύςΟΛ0QUJPOBMʹ͢Δ

  10. Partial

  11. Partial 6TFS͸JE͕ඞਢͳͷͰۭΦϒδΣΫτ͸ίϯ ύΠϧΤϥʔʹͳΔɻ
 1BSUJBM6TFS͸͢΂ͯͷϓϩύςΟ͕0QUJPOBMʹͳΔͨ ΊۭΦϒδΣΫτΛ୅ೖͰ͖Δ

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

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

     ͸PQUJPOBMͷϓϩύςΟΛOPOPQUJPOBMʹ͢Δ 1BSUJBMͷٯͰ ΛऔΓআ͍͍ͯΔ
  14. Required ͢΂ͯͷϓϩύςΟΛOPOPQUJPOBMʹ͢Δ

  15. Required

  16. Required ͢΂ͯͷϓϩύςΟ͕OPOPQUJPOBMͳͷͰ JE͸লུͰ͖ͳ͍

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

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

    }; ϓϩύςΟͷલʹSFBEPOMZΛॻ͘ͱͦͷϓϩύςΟ͸ಡ ΈऔΓઐ༻ʹͳΔ
  19. Readonly ͢΂ͯͷϓϩύςΟΛSFBEPOMZʹ͢Δ

  20. Readonly

  21. Readonly ͢΂ͯͷϓϩύςΟΛSFBEPOMZͷͨΊ ୅ೖෆՄೳ

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

    K]: T[P]; };
  23. type Pick<T, K extends keyof T> = { [P in

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

  25. Pick

  26. Pick ࢦఆͨ͠ϓϩύςΟҎ֎͸ΤϥʔʹͳΔ

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

    K]: T; };
  28. type Record<K extends keyof any, T> = { [P in

    K]: T; }; lLFZPGBOZzlTUSJOHcOVNCFScTZNCPMz
  29. Record LFZ໊ͱWBMVFͷܕΛࢦఆͨ͠ΦϒδΣΫτܕ Λ࡞Δ

  30. Record

  31. Record -BTUTBUPcUBOBLBͨΊɺ TV[VLJ͸ΤϥʔʹͳΔ

  32. Record

  33. Record -BTUTBUPcTV[VLJcUBOBLBͨΊɺ UBOBLB͕ແ͍ͱΤϥʔʹͳΔ

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

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

    T; ࡾ߲ԋࢉࢠͷΑ͏ʹ৚݅ʹΑͬͯܕ͕ܾ·Δ 5͕6Λܧঝ͍ͯ͠Δ৔߹͸OFWFSɺͦ͏Ͱͳ͍৔߹͸5 OFWFS͸஋Λฦ͞ͳ͍ܕɻ
  36. Exclude ྻڍ͔Βࢦఆͨ͠஋ΛऔΓআ͘

  37. Exclude

  38. Exclude औΓআ͍ͨ஋Λ୅ೖ͢ΔͱΤϥʔ

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

    never;
  40. type Extract<T, U> = T extends U ? T :

    never; 5͕6Λܧঝ͍ͯ͠Δ৔߹͸5ɺ ͦ͏Ͱͳ͍৔߹͸OFWFSΛฦ͍ͯ͠Δɻ ฦ͢஋͕&YDMVEFͷٯʹͳ͍ͬͯΔ
  41. Extract ྻڍͷத͔Βࢦఆͨ͠஋Λநग़

  42. Extract

  43. Extract நग़ͨ͠஋Ҏ֎Λ୅ೖ͢ΔͱΤϥʔ

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

    : T;
  45. type NonNullable<T> = T extends null | undefined ? never

    : T; OVMM·ͨ͸VOEFpOFEΛܧঝ͍ͯ͠ΔܕͰ͋ Ε͹OFWFSɺͦ͏Ͱͳ͍৔߹͸5Λฦ͍ͯ͠Δ
  46. NonNullable OVMMͱVOEFpOFEΛऔΓআ͘

  47. NonNullable

  48. NonNullable OVMM΍VOEFpOFE͸୅ೖͰ͖ͳ͍

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

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

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

  52. Parameters

  53. Parameters ؔ਺ͷҾ਺ͷܕʹ߹Θͳ͍ͱΤϥʔ

  54. ·ͱΊ • ศརͳܕ͕TypeScriptຊମʹ૊Έࠐ·Ε͍ͯΔ • Conditional TypesͱMapped TypesΛ࢖ͬͯఆٛ͞Ε͍ͯΔ • ࠓ೔঺հ͖͠Εͳ͔ͬͨܕͷఆٛ͸src/lib/es5.d.tsʹ͋ΔͷͰڵ ຯ͕͋Ε͹ݟͯΈ͍ͯͩ͘͞

  55. Thanks. @shisama_ @shisama