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

TypeScriptで幸せになろう

puku0x
February 26, 2018

 TypeScriptで幸せになろう

FukuokaJS #2 LT

puku0x

February 26, 2018
Tweet

More Decks by puku0x

Other Decks in Technology

Transcript

  1. ੩తܕ෇͚ • ม਺ • ؔ਺ text: string function value(): number

    let a: number; a = 100; // OK a = ‘100’; // Error !! ίϯύΠϧ࣌
 ʹ൑ఆ
  2. Ϋϥε • ES2017൛ class Person { constructor(name) { this.name =

    name; } } Ҿ਺૿͑Δͱ Կ౓΋ॻ͘ͷ໘౗…
  3. Ϋϥε • TypeScript൛ class Person { constructor(public name?: string) {}

    } ͋Γ͕ͱ͏ΞΫηεम০ࢠʂ
 ʢͱΦϓγϣφϧʣ
  4. ΦϒδΣΫτͷ୅ೖ • ES2017൛ let person = new Person(); person =

    { name: ‘ςετଠ࿠’, gender: ‘MALE’, // উखʹ௥Ճ }; มͳΦϒδΣΫτ ೖͬͨΓ͠ͳ͍͔ͳ…
  5. ΦϒδΣΫτͷ୅ೖ • TypeScript൛ ͪΌΜͱ஄͍ͯ͘ΕΔʂ let person = new Person(); person

    = {ɹ name: ‘ςετଠ࿠’, gender: ‘MALE’, // Error };
  6. ΦϒδΣΫτͷ୅ೖ • ΠϯλʔϑΣʔεΛ࢖ͬͨ৔߹ interface IPerson { name: string; } const

    person: IPerson = {ɹ name: ‘ςετଠ࿠’, gender: ‘MALE’, // Error }; ͋Γ͕ͱ͏ΠϯλʔϑΣʔεʂ
  7. ஋ͷ୅ೖ • ES2017൛ class Person { gender; // ‘MALE’ or

    ‘FEMALE’ } มͳ஋͕ ೖͬͨΓ͠ͳ͍͔ͳ…
  8. Union Typeʢڞ༻ܕʣ • ਺஋Ϧςϥϧ • จࣈྻϦςϥϧ type Grade = 1

    | 2 | 3 | 4 | 5; type Gender = ‘MALE’ | ‘FEMALE’;
  9. ؀ڥͷ༻ҙ $ npm install -g typescript $ tsc -—init $

    tsc —-watch • ίϯύΠϥͷΠϯετʔϧ