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

flowでjavascriptに型付けしよう

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

 flowでjavascriptに型付けしよう

Avatar for kaidouji85

kaidouji85

May 26, 2017
Tweet

More Decks by kaidouji85

Other Decks in Technology

Transcript

  1. ੩తɾಈతܕ෇͚ൺֱ ੩తܕ෇͚ ಈతܕ෇͚ ϝϦοτ ‣ ϦϑΝΫλϦϯά͕ൺֱత
 ༰қ ‣ ͋Δఔ౓ͷ੔߹ੑ͕อূ
 ͞Ε͍ͯΔ

    ‣ ͱΓ͋͑ͣಈ͚͹͍͍ίʔυ Λॻ͘෼ʹ͸ָ σϝϦοτ ‣ ܕࢦఆ͢Δͷ͕໘౗͍͘͞ ‣ ܕνΣοΫ͕ਖ਼͘͠ͳ͍ͱ࣮ ߦͰ͖ͳ͍ ‣ ϦϑΝΫλϦϯά͕೉͍͠ ‣ ΦϒδΣΫτͷϓϩύςΟ໊ มߋͰ͢Β݁ߏେม
  2. ϓϦϛςΟϒܕίʔυྫ // @flow let num: number = 12; let name:

    string = 'τϜ'; let isSuccess: boolean = true; let numArray: number[] = [1, 2, 3]; let strArray: Array<string> = ['abc', 'def', 'ghi']; let obj: Object = {name: 'τϜ', age: 20}; let fn: Function = function() { return 0};
  3. Ϣʔβఆٛܕ ʮUZQFܕ໊\ʜ^ʯͰطଘܕΛ૊Έ߹Θͤͨ
 Ϣʔβఆٛܕ͕࡞ΕΔ ಉ͡ܕఆٛΛϓϩδΣΫτதͰ࢖͍·ΘͤΔ // @flow type Product = {

    id: number, name: string, price: number, productType: string, tag: string[], } let item: Product = {id: 1, ...};
  4. ؔ਺ʹܕΛ͚ͭΔ // @flow function welcomeMessage(count: number): string { return `Α͏ͦ͜ɺ͋ͳͨ͸${count}ਓ໨ͷ͓٬༷Ͱ͢ɻ`;

    } ʮؔ਺໊ Ҿ਺ܕʜ ໭Γ஋ͷܕʯͱݴ͏ܗࣜͰ
 Ҿ਺ɺ໭Γ஋ʹܕΛઃఆ͢Δ͜ͱ͕Ͱ͖Δ
  5. ΫϥεΛܕͱͯ͠࢖͏ྫ // @flow class Player { level: number; name: string;

    constructor(name: string, level: number) { this.name = name; this.level = level; }; hello(): string { return `hello myname is ${this.name}. lebel is ${this.level}`; } } let p1: Player = new Player('name', 99);
  6. ίϚϯυϥΠϯ࣮ߦྫ $ flow check flow-basic.js:4 4: let value2: number =

    '10' // NG ^^^^ string. This type is incompatible with 4: let value2: number = '10' // NG ^^^^^^ number static-type-check.js:4 4: return number + 2; ^^^^^^ identifier `number`. Could not resolve name static-type-check.js:8 8: add('2'); // NG ^^^ string. This type is incompatible with the expected param type of 3: function add(value: number): number { ^^^^^^ number