diff --git a/chapter01/.flowconfig b/chapter01/.flowconfig new file mode 100644 index 0000000..1fed445 --- /dev/null +++ b/chapter01/.flowconfig @@ -0,0 +1,11 @@ +[ignore] + +[include] + +[libs] + +[lints] + +[options] + +[strict] diff --git a/chapter02/.flowconfig b/chapter02/.flowconfig new file mode 100644 index 0000000..1fed445 --- /dev/null +++ b/chapter02/.flowconfig @@ -0,0 +1,11 @@ +[ignore] + +[include] + +[libs] + +[lints] + +[options] + +[strict] diff --git a/chapter02/package.json b/chapter02/package.json index 4c4bbc2..082b897 100644 --- a/chapter02/package.json +++ b/chapter02/package.json @@ -26,7 +26,7 @@ "node": true }, "extends": ["eslint:recommended", "plugin:flowtype/recommended"], - "plugins": ["flowtype"], + "plugins": ["babel", "flowtype"], "rules": { "no-console": "off" } diff --git a/chapter02/src/extdate.js b/chapter02/src/extdate.js index 8c6a0e0..efa69a6 100644 --- a/chapter02/src/extdate.js +++ b/chapter02/src/extdate.js @@ -1,3 +1,5 @@ +// @flow + class ExtDate extends Date { fullDate() { const months = [ @@ -16,7 +18,7 @@ class ExtDate extends Date { ]; return ( - months[this.getMonth()] + + months[this.getMonth()] + " " + String(this.getDate()).padStart(2, "0") + " " + @@ -41,7 +43,7 @@ class ExtDate extends Date { ]; return months[m]; } - + fullDate2() { return ( ExtDate.getMonthName(this.getMonth()) + @@ -55,5 +57,5 @@ class ExtDate extends Date { console.log(new ExtDate().fullDate()); // "MAY 01 2018" -console.log(ExtDate.getMonthName(8)); // "SEP" +console.log(ExtDate.getMonthName(8)); // "SEP" console.log(new ExtDate().fullDate2()); // "MAY 01 2018" diff --git a/chapter02/src/persons.js b/chapter02/src/persons.js index f2b551d..ced1e26 100644 --- a/chapter02/src/persons.js +++ b/chapter02/src/persons.js @@ -24,7 +24,7 @@ class Person { } } -pp = new Person("Erika", "Mustermann"); +let pp = new Person("Erika", "Mustermann"); console.log(pp); // Person {first: "Erika", last: "Mustermann"} console.log(pp.initials()); // "EM" console.log(pp.fullName()); // "Erika Mustermann" @@ -44,7 +44,7 @@ class Developer extends Person { } } -window.dd = new Developer("John", "Doe", "JS"); +let dd = new Developer("John", "Doe", "JS"); console.log(dd); // Developer {first: "John", last: "Doe", language: "JS"} console.log(dd.initials()); // "JD" console.log(dd.fullName()); // "John Doe, JS dev" @@ -63,68 +63,64 @@ const countKeysMixin = base => } }; - class PersonWithTwoMixins extends toJsonMixin(countKeysMixin(Person)) { - toJson() { - // redefine the method, just for the sake of it - return "NEW TOJSON " + super.toJson(); - } +class PersonWithTwoMixins extends toJsonMixin(countKeysMixin(Person)) { + toJson() { + // redefine the method, just for the sake of it + return "NEW TOJSON " + super.toJson(); + } +} + +let p2m = new PersonWithTwoMixins("Jane", "Roe"); +console.log(p2m); +console.log(p2m.toJson()); // NEW TOJSON {"first":"Jane","last":"Roe"} +console.log(p2m.countKeys()); // 2 + +class ExtDate extends Date { + fullDate() { + const months = [ + "JAN", + "FEB", + "MAR", + "APR", + "MAY", + "JUN", + "JUL", + "AUG", + "SEP", + "OCT", + "NOV", + "DEC" + ]; + + return ( + months[this.getMonth()] + + " " + + String(this.getDate()).padStart(2, "0") + + " " + + this.getFullYear() + ); } - - p2m = new PersonWithTwoMixins("Jane", "Roe"); - console.log(p2m); - console.log(p2m.toJson()); // NEW TOJSON {"first":"Jane","last":"Roe"} - console.log(p2m.countKeys()); // 2 - - - - class ExtDate extends Date { - fullDate() { - const months = [ - "JAN", - "FEB", - "MAR", - "APR", - "MAY", - "JUN", - "JUL", - "AUG", - "SEP", - "OCT", - "NOV", - "DEC" - ]; - - return ( - months[this.getMonth()] + - " " + - String(this.getDate()).padStart(2, "0") + - " " + - this.getFullYear() - ); - } - - - static getMonthName(m) { - const months = [ - "JAN", - "FEB", - // etc... - "NOV", - "DEC" - ]; - return months[m]; - } - - fullDate2() { - return ( - ExtDate.getMonthName(this.getMonth()) + - " " + - String(this.getDate()).padStart(2, "0") + - " " + - this.getFullYear() - ); - } + static getMonthName(m) { + const months = [ + "JAN", + "FEB", + // etc... + "NOV", + "DEC" + ]; + return months[m]; } - - console.log(new ExtDate().fullDate()); // "MAY 01 2018" \ No newline at end of file + + fullDate2() { + return ( + ExtDate.getMonthName(this.getMonth()) + + " " + + String(this.getDate()).padStart(2, "0") + + " " + + this.getFullYear() + ); + } +} + +console.log(new ExtDate().fullDate()); // "MAY 01 2018" diff --git a/chapter02/src/types_advanced.js b/chapter02/src/types_advanced.js new file mode 100644 index 0000000..c760184 --- /dev/null +++ b/chapter02/src/types_advanced.js @@ -0,0 +1,19 @@ +/* @flow */ +/* eslint-disable no-unused-vars */ + +let flag: number | boolean; +flag = true; // OK +flag = 1; // also OK +flag = "1"; // error: wrong type + +let traffic: "red" | "amber" | "green"; // traffic is implicitly string +traffic = "yellow"; // error: not allowed + +type numberOrString = number | string; + +function addTwo(x: numberOrString, y: numberOrString) { + return x + y; +} + +const fs: any = require("fs"); +fs.readFile("/somefilename.ext", processData); diff --git a/chapter02/src/types_basic.js b/chapter02/src/types_basic.js new file mode 100644 index 0000000..8cc533c --- /dev/null +++ b/chapter02/src/types_basic.js @@ -0,0 +1,33 @@ +/* @flow */ +/* eslint-disable no-unused-vars */ + +let someFlag: boolean; +let greatTotal: number; +let firstName: string; + +function toString(x: number): string { + return String(x); +} + +function addTwo(x: number | string, y: number | string) { + return x + y; +} + +function showValue(z: mixed): void { + // not returning anything + console.log("Showing... ", z); +} + +let numbersList: Array; +numbersList = [22, 9, 60]; // OK +numbersList[1] = "SEP"; // error; cannot assign a string to a number + +let anotherList: number[]; + +let sealedObject: { name: string, age?: number }; +sealedObject.name = "Ivan Horvat"; // OK +sealedObject.id = 229; // error: key isn't defined in the data type +sealedObject = { age: 57 }; // error: mandatory field is missing + +let unsealedObject = {}; +unsealedObject.id = 229; // OK