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

TypeScript導入で得られる「変えていく勇気」 / The courage to cha...

OKUNOKENTARO
September 01, 2018

TypeScript導入で得られる「変えていく勇気」 / The courage to change by TypeScript

2018/9/1、GDG DevFest Tokyo 2018にて発表した資料です。

OKUNOKENTARO

September 01, 2018
Tweet

More Decks by OKUNOKENTARO

Other Decks in Technology

Transcript

  1. "MU+4ˠ#BCFM w ͪΐͬͱ ͨ͠ྺ࢙ͷ࿩ w ೥$PGGFF4DSJQU w ೥5ZQF4DSJQU  

    w ೥#BCFM wʮKT͡Όͳ͍ଞͷϑΝΠϧʯ Λ+BWB4DSJQUϑΝΠϧʹม׵ w ม׵ޙͷϑΝΠϧ͕ಈ࡞͢Ε͹Α͍ͱ͍͏ߟ͑ํ
  2. XFCQBDL w OQN w /PEF1BDLBHF.BOBHFS w ΋ͱ͸/PEFKT༻ͷϞδϡʔϧґଘؔ܎؅ཧ༻ͷπʔϧ w ݱ୅Ͱ͸ϑϩϯ τΤϯ

    υ։ൃͷσϑΝΫ τ ɾ ελϯμʔ υʹ w όοΫΤϯ υɺ ϑϩϯ τΤϯ υؔ܎ͳ͘OQNͰґଘΛ؅ཧ w ϑϩϯ τΤϯ υͳΒ͹XFCQBDLͱ͍͏όϯ υϥʔ࢖ͬͯόϯ υϧϑΝΠϧΛ࡞੒
  3. ॲཧΛ଍͢ͳΒɺ Α Γ҆શଆʹ w ݱ୅ͷ+BWB4DSJQUͰ͸࣮ߦલʹԿ͔ͱॲཧ͕ͪ͠ w #BCFMͳͲͷ τϥϯεύΠϧ w XFCQBDLͰͷόϯ

    υϧ w Ͳ͏࣮ͤߦલʹԿ͔͠Βॲཧ͢ΔͳΒόάΛݮΒͤΔΑ Γ҆શଆʹ޲͚ͨॲཧ΋ w 5ZQF4DSJQUͷίϯύΠϧ ʢ੩తܕݕࠪʣ  w ϓϩάϥϜͷ࣮ߦલʹݕ஌Ͱ͖ΔΤϥʔ͸͢΂ͯ੩తʹݕ஌͓ͯ͘͠
  4. ޡղܕఆٛϑΝΠϧͷ؅ཧ͕໘౗ w 5ZQF4DSJQUͰɺ +BWB4DSJQUϥΠ ϒϥ ϦΛ࢖͍͍ͨͱ͖
 ͦͷϥΠ ϒϥ ϦͷܕఆٛϑΝΠϧΛඞཁͱ͢Δ w

    ͦͷܕఆٛϑΝΠϧ .d.ts ͸
 ܕఆٛ؅ཧπʔϧΛ࢖ͬͯผ్Πϯε τʔϧ͠ͳ͍ͱ͍͚ͳ͍ w ੲ͸ͦ͏ͩͬͨ w ࠓ͸ҧ͏
  5. ࠞͥͯӡ༻Ͱ͖·͢ w ίϯύΠ ϥΦϓγϣϯͷ--allowJsΛ༗ޮʹ͢Δ
 ·ͨ͸ɺ tsconfig.jsonͷallowJsΛtrueʹ͢Δ w +BWB4DSJQUଆͷੈք͸͢΂ͯanyͰ͋Δͱ ͯ͠
 ஍ಓʹ5ZQF4DSJQUԽΛਐΊΔ

    w ίϯύΠ ϥઃఆϑΝΠϧtsconfig.jsonͰ͸ॳظ஋͕strict: true w .jsΛ.tsʹϦωʔϜͨ͠Βɺ Ͳ͜Λ௚ͤ͹͍͍͔ίϯύΠ ϥ͕ڭ͑ͯ͘ΕΔ
  6. VOLOPXOܕ w 5ZQF4DSJQUΑ Γunknownܕ͕ఆٛ͞Εͨ w ͍··Ͱ͸ ʮΑ ͘Θ͔Βͳ͍΋ͷʯ ͸anyܕͱ͍ͯͨ͠ w

    ࠓޙɺ ྫ͑͹JSON.parse()ͷ໭Γܕ͸
 anyΑ Γunknownͱ ͯ͠ѻ͏ ͱ҆શ ʢඪ४ͷܕఆٛͰ͸anyͱͳ͍ͬͯΔʣ
  7. ԿΛม͍͑ͯ͘ͷ͔ w ϓϩάϥϜͷιʔείʔ υ w ίʔ υ͸෗Δ w ෗ͬͨ෦෼͸আڈ͢Δ w

    ߴ඼࣭ͳϓϩάϥϜͰ͋ΔͨΊʹɺ 
 ৗʹίʔ υશମͷ઱౓Λߴ͘อͨͳ͚Ε͹ͳΒͳ͍
  8. ͳͥ෗Δͷ͔ w ϓϩάϥϛϯά͸ϏϧͷݐஙͰ͸ͳ͍ w ઃܭਤ௨Γʹ׬੒͠ͳ͍ w ίʔ υ֎ͰͷมԽ w ࢓༷ͷมߋɺ

    νʔϜϝϯόʔͷೖΕସ͑ w ։ൃऀͷεΩϧ޲্ ɾ ཁ݅΁ͷख़஌ w ෗Δͱ͸ɺ ίʔ υΛॻ͍͔ͯΒͷ࣌ؒܦաΛࢦ͢ͷͰ͸ͳ͘
 ίʔ υΛॻ͍ͨ࣌఺͔Βݱ࣌఺·Ͱͷɺ ίʔ υ֎ͰͷมԽͷେ͖͞Ͱ͋Δ
  9. Ϧ ϑΝΫλ Ϧ ϯά w ιʔείʔ υΛม͍͑ͯͨ͘Ίʹ͸Ϧ ϑΝΫλ Ϧ ϯά͕ඞཁ

    w ݱ୅ͷϓϩάϥϛϯάʹ͓͍ͯ͸
 ʮҰ౓ಈ࡞ͨ͠ϓϩάϥϜʹ͸ೋ౓ͱखΛՃ͑ͳ͍ʯ ͳΜͯෆՄೳ w ͦ΋ͦ΋ ʮҰ౓ಈ࡞ͨ͠ʯ ΛͲͷ࣌఺ͷಈ࡞ͱ͢Δͷ͔
 ఆٛͰ͖ͳ͍΄Ͳཁ͕݅ڊେʹͳ͍͍ͬͯͬͯΔ w ͢ͳΘͪৗʹϦ ϑΝΫλ Ϧ ϯά͠ଓ͚ͳ͚Ε͹ͳΒͳ͍ wʮม͍͑ͯ͘༐ؾʯ ͕ඞཁ
  10. ܕݕࠪʹΑΔ҆৺ײ w Ϧ ϑΝΫλ Ϧ ϯάதʹ৽ͨͳόάΛ࣋ͪࠐΜͰ͸ͳΒͳ͍ w ςε τ͕͋Ε͹ม͍͚͑ͯΔ w

    ςε τ͕ͳ͚Ε͹ʜʜ w Ұ౓ʹେ෯ͳมߋΛ͠ͳ͍ͳΒɺ ίϯύΠ ϥΛ৴༻͢Δख΋͋Δ wʮςε τ͸ॻ͍ͯͳ͍͚Ͳɺ ίϯύΠϧ ɾ Τϥʔ͕ग़͍ͯͳ͍ʯ  w ˠ ʮগͳ͘ ͱ΋ɺ ίϯύΠϧ ɾ ΤϥʔͱͳΔΑ ͏ͳϛε͸ؚ·Ε͍ͯͳ͍ ʂ ʯ  w ΋ͪΖΜςε τΛॻ͘ʹӽͨ͜͠ͱ͸ͳ͍
  11. ܕݕ͕ࠪ৴༻ͳΒͳ͍ྫ addToFavorites( entryId: number, userId: number ) { // do

    something } toFormattedString( date: Date, format: string, timeZone: string ): string { // do something }
  12. Ҿ਺ʹ஋ΦϒδΣΫ τΛಋೖ addToFavorites( entryId: EntryId, userId: UserId ) { //

    do something } toFormattedString( date: Date, format: DateFormat, timeZone: TimeZone ): string { // do something }
  13. ஋ΦϒδΣΫ τ export abstract class EntryId extends NumberValue { constructor(protected

    value: number) { super(value); } } export abstract class UserId extends NumberValue { constructor(protected value: number) { super(value); } }
  14. ͜Ε͚ͩ͡Ό଍Γͳ͍ w 5ZQF4DSJQU͸4USVDUVSBM5ZQJOH ʢߏ଄ܕʣ ͱ͍͏ܕࣝผ๏Λ༻͍͍ͯΔ w લทͷྫ w EntryIdܕ΋UserIdܕ΋value: numberϓϩύςΟ

    Λ࣋ͭ w ಉ͡ϓϩύςΟ ͔͍࣋ͬͯ͠ͳ͍ͷͰɺ ޓ׵ੑͷ͋ΔܕͱΈͳ͞Εͯ͠· ͏ w /PNJOBM5ZQJOH ʢެশܕʣ Λ࠾༻͢Δݴޠ w ϓϩύςΟʹؔΘΒͣDMBTT͕ҧ͑͹ผͷܕͱͳΔ
  15. ࠞಉ๷ࢭϓϩύςΟ Λ଍͢ type PreventEquation = void; export abstract class EntryId

    extends NumberValue { EntryId: PreventEquation; constructor(protected value: number) { super(value); } } export abstract class UserId extends NumberValue { UserId: PreventEquation; constructor(protected value: number) { super(value); } }
  16. ஋ΦϒδΣΫ τͷ਌Ϋϥε export abstract class NumberValue extends Value<number> { constructor(protected

    value: number) { super(value); } lt(other: NumberValue): boolean { return this.value < other.value; } lte(other: NumberValue): boolean { return this.value <= other.value; } }
  17. جఈΫϥε export abstract class Value<T> { constructor(protected value: T) {

    // } toString(): string { return this.value.toString(); } valueOf(): T { return this.value; } eq(other: Value<T>): boolean { return this.value === other.value; } }
  18. ܧঝ͸ڞ௨ԽͰ͸ͳ͍ w DMBTTͷܧঝ FYUFOET Λෳ਺ͷΫϥεؒͷॲཧͷڞ௨Խखஈͱ ͯ͠༻͍ͯ͸ͳΒͳ͍ w εύήςΟͷݪҼɺ ͍͍ͩͨޙչ͢Δ w

    FYUFOET͸ڞ௨ԽͷखஈͰ͸ͳ͘ɺ ந৅తੑ࣭ͷදݱͱ ͯ͠࢖͏
 ͜ͷྫͩͱ wʮจࣈྻܥͷΦϒδΣΫ τʯ ͳͷ͔ wʮ਺஋ܥͷΦϒδΣΫ τʯ ͳͷ͔ w Λදݱ͍ͯ͠Δ
  19. ίϯύΠϧ ɾ ΤϥʔΛຯํʹ͚ͭΖ w ίϯύΠϧ ɾ Τϥʔ͸ѱ͍΋ͷͰ͸ͳ͍ w Ή͠Ζ։ൃऀʹৗʹدΓఴ͏ɺ ৺ڧ͍ຯํͰ͋Δ

    w Τϥʔʹײँ ʂ  w QSFUUJFS ʢίʔ υ ɾ ϑΥʔϚολʣ ͱ5ZQF4DSJQUίϯύΠ ϥʹै͓͚ͬͯ͹޾ͤ
  20. ෆ҆ΛऔΓআ͘ w ίʔ υΛม͍͑ͯ͘ ͜ͱ͸ৗʹෆ҆ͱͷઓ͍ w ෆ҆ΛऔΓআͨ͘ΊʹԿ͕Ͱ͖Δ͔ w ςε τΛॻ͘

    w ܕΛॻ͘ wυΩϡϝϯ τΛॻ͘ ʢܕΞϊςʔγϣϯ͸ྑ࣭ͳ υΩϡϝϯ τͰ͋Δʣ  w 5ZQF4DSJQUΛ࠾༻͢Ε͹ɺ ෆ͕҆औΓআ͚ͯ ʮม͍͑ͯ͘༐ؾʯ ΛಘΒΕΔ