|
| 1 | +import FibConst, { fib } from './fibonacci'; |
| 2 | +import { Stack } from './stack'; |
| 3 | + |
| 4 | +console.log('hello world'); |
| 5 | + |
| 6 | +// ---- Modules ---- |
| 7 | +console.log('fib(FibConst) is:', fib(FibConst)); |
| 8 | + |
| 9 | +// ---- Types and Interfaces ---- |
| 10 | +type TrafficLight = 'red' | 'green' | 'yellow'; |
| 11 | +const annoyingLight: TrafficLight = 'red'; |
| 12 | + |
| 13 | +interface Car { |
| 14 | + wheels: number; |
| 15 | + color: string; |
| 16 | + make: string; |
| 17 | + model: string; |
| 18 | +} |
| 19 | + |
| 20 | +const myCar: Car = { |
| 21 | + wheels: 4, |
| 22 | + color: 'blue', |
| 23 | + make: 'Toyota', |
| 24 | + model: 'Camry' |
| 25 | +}; |
| 26 | +// JSON.stringify makes a nice string representation of an object |
| 27 | +console.log('My car:', JSON.stringify(myCar)); |
| 28 | + |
| 29 | +// ---- Generics ---- |
| 30 | +const myStack = new Stack<number>(); |
| 31 | +myStack.push(1); |
| 32 | +myStack.push(2); |
| 33 | +myStack.push(3); |
| 34 | +console.log('Number on top of the stack:', myStack.pop()); |
| 35 | + |
| 36 | +// ---- Spread and Destructuring ---- |
| 37 | +const obj1 = { |
| 38 | + first: 'who', |
| 39 | + second: 'what', |
| 40 | + third: 'dunno', |
| 41 | + left: 'why' |
| 42 | +}; |
| 43 | + |
| 44 | +const obj2 = { |
| 45 | + center: 'because', |
| 46 | + pitcher: 'tomorrow', |
| 47 | + catcher: 'today' |
| 48 | +}; |
| 49 | + |
| 50 | +const megaObj = { ...obj1, ...obj2 }; |
| 51 | + |
| 52 | +const { first, second, catcher } = megaObj; |
| 53 | +console.log('First:', first); |
| 54 | +console.log('Second:', second); |
| 55 | +console.log('Catcher:', catcher); |
| 56 | + |
| 57 | +// ---- Async / Await ---- |
| 58 | +function makePromise(): Promise<number> { |
| 59 | + return Promise.resolve(5); |
| 60 | +} |
| 61 | + |
| 62 | +async function getGreeting(name: string): Promise<string> { |
| 63 | + return 'hello ' + name; |
| 64 | +} |
| 65 | + |
| 66 | +async function run() { |
| 67 | + const result = await makePromise(); |
| 68 | + console.log('makePromise returned:', result); |
| 69 | + |
| 70 | + const greeting = await getGreeting('Ken'); |
| 71 | + console.log('greeting:', greeting); |
| 72 | +} |
| 73 | + |
| 74 | +run(); |
| 75 | + |
| 76 | +// Make this file a module so its code doesn't go in the global scope |
| 77 | +export {}; |
0 commit comments