diff --git a/step2-01/README.md b/step2-01/README.md index 0db18b4..b21dc3b 100644 --- a/step2-01/README.md +++ b/step2-01/README.md @@ -2,56 +2,83 @@ [Lessons](../) | [Exercise](./exercise/) | [Demo](./demo/) -In this step, we'll cover enough of the TypeScript concepts to be productive with the React & Redux frameworks. +In this step, we'll cover enough TypeScript concepts to be productive with the React & Redux frameworks. Topics in this step will include: -- ES modules -- Basic Types -- Interfaces & Classes -- Basic Generics -- Spread and Destructuring -- Async / Await +- [ES modules](#modules) +- [Types](#typescript-types) +- [Spread](#spread-operator) and [Destructuring](#destructuring) +- [Promise](#promise) and [Async / Await](#async--await) + +> To try out TypeScript concepts and see the corresponding JavaScript, you can use the [TypeScript playground](http://www.typescriptlang.org/play/index.html). We won't be using it in this training, but it's very handy in general! ## Modules -Historically, JS is only executed in browser. The code all had to be loaded from ` diff --git a/step2-01/demo/src/async/index.ts b/step2-01/demo/src/async/index.ts index c4a34fe..54773ca 100644 --- a/step2-01/demo/src/async/index.ts +++ b/step2-01/demo/src/async/index.ts @@ -3,10 +3,10 @@ async function fetchSomething() { return await response.text(); } -// Async functions always returns Promise +// Async functions always return a Promise fetchSomething().then(text => { console.log('hello ' + text); }); -// adding an export to turn this into a "module" +// adding an export turns this into a "module" export default {}; diff --git a/step2-01/demo/src/generics/index.ts b/step2-01/demo/src/generics/index.ts index 95e5093..a85d164 100644 --- a/step2-01/demo/src/generics/index.ts +++ b/step2-01/demo/src/generics/index.ts @@ -19,5 +19,5 @@ function reverse(arg: T[]): T[] { return arg; } -// adding an export to turn this into a "module" +// adding an export turns this into a "module" export default {}; diff --git a/step2-01/demo/src/interfaces/index.ts b/step2-01/demo/src/interfaces/index.ts index 88e2f17..001baf5 100644 --- a/step2-01/demo/src/interfaces/index.ts +++ b/step2-01/demo/src/interfaces/index.ts @@ -1,22 +1,32 @@ +// Interface for an object or class interface Car { make: string; model: string; } class MyCar implements Car { - make: 'Honda'; - model: 'Accord'; -} + make: string; + model: string; -const myCar: Car = { + constructor(make: string, model: string) { + this.make = make; + this.model = model; + } +} +const myCar1: Car = new MyCar('Honda', 'Accord'); + +const myCar2: Car = { make: 'Honda', model: 'Accord' }; -// Interface as Functions +// Interface for a function interface InterestingFn { (someArgs: string): number; } +const interesting: InterestingFn = (someArgs: string): number => { + return Number(someArgs); +}; // adding an export to turn this into a "module" export default {}; diff --git a/step2-01/demo/src/modules/index.ts b/step2-01/demo/src/modules/index.ts index 8113c7f..5f88c96 100644 --- a/step2-01/demo/src/modules/index.ts +++ b/step2-01/demo/src/modules/index.ts @@ -1,10 +1,10 @@ // These are named imports from a file relative to this file import { namedConst, namedFn, namedObj, namedConstBracket } from './named'; -// We can even apply an alias to the named constant +// We can even give an alias to the named constant import { namedConst as c } from './named'; -// These are the same instances of the named imports, but gets imported all at the same time under a single object +// These are the *same instances* of the named imports, but they all get imported inside a single object import * as named from './named'; // Print out the exports @@ -14,12 +14,15 @@ console.log(namedFn()); console.log(namedObj); console.log(namedConstBracket); -// Print out exports through module level import +// Print out exports from module level import console.log(named.namedConst); console.log(named.namedFn()); console.log(named.namedObj); console.log(named.namedConstBracket); +// The named and module-level imports reference the same object instances +console.log(namedObj === named.namedObj); // true + // Default import can be named anything we want as the consumer import DefaultClass from './default'; import Foo from './default'; diff --git a/step2-01/demo/src/spread/index.ts b/step2-01/demo/src/spread/index.ts index be53c49..20c7b09 100644 --- a/step2-01/demo/src/spread/index.ts +++ b/step2-01/demo/src/spread/index.ts @@ -1,6 +1,6 @@ // Destructuring var [a, b, ...rest] = [1, 2, 3, 4]; -console.log(a, b, rest); // 1,2,[3,4] +console.log(a, b, rest); // 1 2 [3,4] // Array assignment var list = [1, 2]; @@ -20,5 +20,5 @@ const obj3 = { ...obj1, ...obj2 }; // Destructuring object const { x } = obj3; -// adding an export to turn this into a "module" +// adding an export turns this into a "module" export default {}; diff --git a/step2-01/demo/src/types/index.ts b/step2-01/demo/src/types/index.ts index 508b67e..247a67e 100644 --- a/step2-01/demo/src/types/index.ts +++ b/step2-01/demo/src/types/index.ts @@ -47,13 +47,18 @@ let choose1 = { common: '5' }; type CatStatus = 'alive' | 'dead' | 'both'; // Classes -class Animal {} +class Animal { } // Illustration purposes only // In real apps, avoid inheritance if possible // noted exception: React.Component with react@<16.8.0 -class Cat extends Animal {} -class Dog extends Animal {} +class Cat extends Animal { } +class Dog extends Animal { } -// adding an export to turn this into a "module" +// Any Type - avoid if possible +let mystery: any = "I don't like the person who will be maintaining this code"; +mystery = 2; +mystery = () => 3; + +// adding an export turns this into a "module" export default {}; diff --git a/step2-01/exercise/index.html b/step2-01/exercise/index.html index b0857bf..52458b8 100644 --- a/step2-01/exercise/index.html +++ b/step2-01/exercise/index.html @@ -6,7 +6,7 @@
- Nothing to show here, just look at your console window for output. Hit F12 to open console window. + Nothing to show here; just look at your console window for output. Hit F12 (cmd+option+I on Mac) to open the console window.
diff --git a/step2-01/exercise/src/index.ts b/step2-01/exercise/src/index.ts index cb6f05e..737602c 100644 --- a/step2-01/exercise/src/index.ts +++ b/step2-01/exercise/src/index.ts @@ -19,12 +19,12 @@ function makePromise() { return Promise.resolve(5); } -// Do the exercises here, output your results with "console.log()" function +// Do the exercises here, outputting results using console.log() // ... console.log('hello world'); async function run() { - // Place your code for the async / await exercise here + // Call the function you added for the async / await exercise here // ... } diff --git a/step2-01/exercise/src/stack.ts b/step2-01/exercise/src/stack.ts index 99d1ad3..15ccc28 100644 --- a/step2-01/exercise/src/stack.ts +++ b/step2-01/exercise/src/stack.ts @@ -1,10 +1,6 @@ // TODO: create a Stack generic class here: -/** - * - * export class Stack { - * push(...) { ... } - * pop(...) { ... } - * } - * - */ +// export class Stack { +// push(...) { ... } +// pop(...) { ... } +// }