From 988aea6c1d3e6d4592cc11dde50c7e5eb81777f0 Mon Sep 17 00:00:00 2001 From: Ken Date: Fri, 15 Feb 2019 20:13:29 -0800 Subject: [PATCH] await async --- server/index.js | 4 +++ step2-01/src/async/index.ts | 8 +++++ step2-01/src/generics/index.ts | 20 ++++++++++++ step2-01/src/index.tsx | 21 ++++++++----- step2-01/src/interfaces/index.ts | 19 ++++++++++++ step2-01/src/spread/index.ts | 12 ++++++++ step2-01/src/types/index.ts | 52 ++++++++++++++++++++++++++++++++ 7 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 step2-01/src/spread/index.ts diff --git a/server/index.js b/server/index.js index dae2115..0a277ca 100644 --- a/server/index.js +++ b/server/index.js @@ -33,6 +33,10 @@ app.post('/todos', req => { store.todos = req.body; }); +app.get('/hello', (req, res) => { + res.send('world'); +}); + app.listen(process.env.NODE_ENV === 'production' ? undefined : 3000, () => { console.log('Listening at http://localhost:3000'); }); diff --git a/step2-01/src/async/index.ts b/step2-01/src/async/index.ts index e69de29..efcf1d5 100644 --- a/step2-01/src/async/index.ts +++ b/step2-01/src/async/index.ts @@ -0,0 +1,8 @@ +async function fetchSomething() { + const response = await fetch('http://localhost:3000/hello'); + return await response.text(); +} + +fetchSomething().then(text => { + console.log('hello ' + text); +}); diff --git a/step2-01/src/generics/index.ts b/step2-01/src/generics/index.ts index e69de29..eed6a06 100644 --- a/step2-01/src/generics/index.ts +++ b/step2-01/src/generics/index.ts @@ -0,0 +1,20 @@ +// Generics for classes +class Stack { + private data: T[] = []; + + push(item: T) { + this.data.push(item); + } + pop(): T { + return this.data.pop(); + } +} + +const numberStack = new Stack(); +const stringStack = new Stack(); + +// Generics for functions +function reverse(arg: T[]): T[] { + // TODO: implement the logic to reverse the array + return arg; +} diff --git a/step2-01/src/index.tsx b/step2-01/src/index.tsx index 395ba15..41d949e 100644 --- a/step2-01/src/index.tsx +++ b/step2-01/src/index.tsx @@ -1,14 +1,19 @@ -// - modularity -import './modules'; +// Interesting Typescript Topics -// - interface -import './interfaces'; - -// - function typing +// types import './types'; -// - generics +// interface +import './interfaces'; + +// modularity +import './modules'; + +// generics import './generics'; -// - await / async +// await / async import './async'; + +// spread syntax +import './spread'; diff --git a/step2-01/src/interfaces/index.ts b/step2-01/src/interfaces/index.ts index e69de29..9037376 100644 --- a/step2-01/src/interfaces/index.ts +++ b/step2-01/src/interfaces/index.ts @@ -0,0 +1,19 @@ +interface Car { + make: string; + model: string; +} + +class MyCar implements Car { + make: 'Honda'; + model: 'Accord'; +} + +const myCar: Car = { + make: 'Honda', + model: 'Accord' +}; + +// Interface as Functions +interface InterestingFn { + (someArgs: string): number; +} diff --git a/step2-01/src/spread/index.ts b/step2-01/src/spread/index.ts new file mode 100644 index 0000000..dccb3d7 --- /dev/null +++ b/step2-01/src/spread/index.ts @@ -0,0 +1,12 @@ +// Destructuring +var [x, y, ...rest] = [1, 2, 3, 4]; +console.log(x, y, rest); // 1,2,[3,4] + +// Array assignment +var list = [1, 2]; +list = [...list, 3, 4]; +console.log(list); // [1,2,3,4] + +// Object assignment +const point2D = { x: 1, y: 2 }; +const point3D = { ...point2D, z: 3 }; diff --git a/step2-01/src/types/index.ts b/step2-01/src/types/index.ts index e69de29..d25ab32 100644 --- a/step2-01/src/types/index.ts +++ b/step2-01/src/types/index.ts @@ -0,0 +1,52 @@ +// Basic Types +let isDone: boolean = false; +let decimal: number = 6; +let color: string = 'blue'; +let sky: string = `the sky is ${color}`; + +// Function Types +type FibFn = (n: number) => number; + +// Object Types +type Obj = { + [key: string]: string; +}; + +// Object with Specified Keys +type Specific1 = { + foo: string; + bar: number; + common: string; +}; + +type Specific2 = { + alice: string; + bob: number; + common: number; +}; + +// composition +type typeofObj = { + foo: string; + bar: number; + obj: Specific1; +}; + +// union, intersection +type Union = Specific1 | Specific2; +type Intersection = Specific1 & Specific2; + +// casting +let choose1 = { common: '5' }; + +// string literal union +type CatStatus = 'alive' | 'dead' | 'both'; + +// Classes +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 {}