Permalink
Browse files

Improves shit

  • Loading branch information...
luciavelasco committed Dec 5, 2017
1 parent 2d1f38e commit c2d5950cd68204a04bfd33deb7fe794b4c53b981
Showing with 193 additions and 64 deletions.
  1. +21 −7 README.md
  2. +10 −35 index.ts
  3. +7 −2 src/animal.ts
  4. +14 −0 src/bird.ts
  5. +0 −19 src/chicken.ts
  6. +25 −0 src/staticTypeChecking.ts
  7. +31 −0 src/typingArrays.ts
  8. +84 −0 src/typingFunctions.ts
  9. +1 −1 tsconfig.json
View
@@ -1,10 +1,24 @@
`npm i typescript --save-dev`
`npm install @types/node --save-dev`
`node ./node_modules/typescript/lib/tsc --init`
`npm install ts-node --save-dev`
`npm install nodemon --save-dev`
1. `npm i typescript --save-dev`
2. `npm install @types/node --save-dev`
`npm run build`
`node ./index.js`
3. `node ./node_modules/typescript/lib/tsc --init` or manually create a `tsconfig.json` file that looks like this:
```
{
"compilerOptions": {
"target": "es2015",
"module": "commonjs",
"strict": true
}
}
```
4. `npm install ts-node --save-dev`
5. `npm install nodemon --save-dev`
6. `npm run build`
7. `node ./index.js`
View
@@ -1,46 +1,21 @@
// Types of imports
// import * as moment from 'moment';
// import moment = require('moment');
// const moment = require('moment');
import { Chicken } from './src/chicken'
// import Animal, { IAnimal } from './src/animal';
// import './src/typingFunctions';
// import './src/staticTypeChecking';
// console.log('Hello World!');
console.log('Hello World!');
/**
* 1. Setup
* 2. Different types and how to use them
* - Bonus, why they're so good for documentation
* 3. Interfaces
* 4. Generics
* 5. JS vs ES6 vs TS
* 5. .d.ts files and why your IDE loves them
* 6. JS vs ES6 vs TS (abstract classes)
*/
// Typings
// any, array, typles, void, enum, type, null, undefined, never
// Typings and naming as good documentation
// types helping you write by telling you what's next and what's available
// Generics
// Compare how-to in JS/ES5 vs ES6/2015 vs TS
// Example of static type checking
// let foo = 123;
// foo = '456';
const chicken = new Chicken();
console.log(`${chicken.speak()}`);
// console.log(`${chicken.speak()} at ${moment().format('h:mm:ssa')}`);
console.log(chicken.getInfo());
function grabOne<T>(options: T[]): T {
return options[Math.floor(Math.random() * options.length)];
}
type TFruit = 'banana' | 'orange' | 'apple'
const fruitBowl: TFruit[] = ['banana', 'banana', 'orange', 'apple', 'apple', 'apple']
console.log(grabOne<TFruit>(fruitBowl))
View
@@ -5,10 +5,15 @@ export interface IAnimal {
name: string,
sound: string,
age: number,
type: TAnimal
type: TAnimal,
body?: IBody
}
// Using Generics in Abstract Classes
export interface IBody {
numberOfLegs: number
}
// Using Generics and Abstract Classes
export default abstract class Animal<T extends IAnimal> {
constructor(private animal: T){}
View
@@ -0,0 +1,14 @@
import Animal from './animal' // importing a default
import { IAnimal } from "./animal"; // importing a named declaration
export interface IBird extends IAnimal {
type: 'bird'
}
// Passes in the Chicken interface, which extends the Animal interface
export class Bird extends Animal<IBird> {
constructor(bird: IBird) {
// You can force types
super(bird)
}
}
View

This file was deleted.

Oops, something went wrong.
View
@@ -0,0 +1,25 @@
// // How your IDE helps you (static type checking)
// function makePrincess(character: 'wise' | 'sweet' | 'cunning', hair: string, name: string) {
// return `${name} is a ${character} princess with ${hair} hair`
// }
// // console.log(makePrincess())
// // console.log(makePrincess('caring', 'long', 'Aurora'))
// console.log(makePrincess('wise', 'long', 'Aurora'))
// function makeChicken() {
// return new Bird({
// name: 'chicken',
// sound: 'cluck',
// // age: 2,
// // body: {
// // numberOfLegs: 'cat'
// // },
// type: 'bird'
// });
// // } as IBird)
// }
// Example of static type checking
// let foo = 123;
// foo = '456';
View
@@ -0,0 +1,31 @@
// Array vs []
type TArray<T> = T[];
// type TArray<T> = Array<T>;
type TStringOrNumber = number | string
let array: TArray<'cat'> = ['cat'];
// array = ['dog']; // invalid
type TArrayOfStringsOrNumbers = Array<string | number>
let arrayOfStringsOrNumbers: TArrayOfStringsOrNumbers = [1, 'cat', 3]
type TStringOrArrayOfNumbers = string | number[]
let stringOrArrayOfNumbers: TStringOrArrayOfNumbers = 'cat'
stringOrArrayOfNumbers = [1, 2, 3]
// stringOrArrayOfNumbers = [1, 'cat', 3] // invalid
// stringOrArrayOfNumbers = 5 // invalid
// Tuples and Arrays
type TArrayOfStringThenNumber = [string, number]
let arrayOfStringThenNumber: TArrayOfStringThenNumber = ['cat', 1, 3]
// arrayOfStringThenNumber = ['cat', 'cat', 3] // invalid
type TArrayOfStringOrNumberThenNumber = [string | number, number]
let arrayOfStringOrNumberThenNumber: TArrayOfStringOrNumberThenNumber = ['cat', 1, 3]
arrayOfStringOrNumberThenNumber = [1, 1, 3]
// arrayOfStringOrNumberThenNumber = [1, 'cat', 3] // invalid
// arrayOfStringOrNumberThenNumber = [1, {}, 3] // invalid
View
@@ -0,0 +1,84 @@
type TFruit = 'banana' | 'orange' | 'apple'
const fruitBowl: TFruit[] = ['banana', 'banana', 'orange', 'apple', 'apple', 'apple']
const fruitAndVegetables = ['banana', 'banana', 'banana', 'leek', 'leek', 'orange']
// // Typehint parameters and return
// function countBananas(fruit: TFruit[]): number {
// return fruit.filter(v => {
// return v === 'banana'
// }).length
// }
//
// // Typecast arguments
// console.log(`There are ${countBananas(fruitBowl)} bananas`)
// console.log(`There are ${countBananas(fruitAndVegetables as TFruit[])} bananas`)
// // Type any
// function countApples(fruit: any): number | void {
// if (!fruit || !Array.isArray(fruit)) return;
//
// return fruit.filter(v => {
// return v === 'apple'
// }).length
// }
// console.log(`There are ${countApples(fruitBowl)} apples`)
// console.log(`There are ${countApples({})} apples`)
// // Promise and using void
// async function countOranges(fruit: any): Promise<number | void> {
// if (!fruit || !Array.isArray(fruit)) return;
// return fruit.filter(v => {
// return v === 'orange'
// }).length
// }
// const numberOfOranges = countOranges(fruitBowl)
// console.log(`There are ${numberOfOranges} oranges`)
//
// countOranges(fruitBowl).then(numberOfOranges => {
// if (numberOfOranges)
// console.log(`There are ${numberOfOranges} oranges`)
// })
// // never
// function juggleOranges(): never {
// throw new Error('Don\'t play with your food!')
// }
// juggleOranges()
// // undefined and enums (and null)
// enum Brushable {
// Teeth,
// Hair
// }
// function brush(brushSize: 'big' | 'small' | undefined, teethOrHair: Brushable) {
// const brushable = teethOrHair === Brushable.Hair ? 'hair' : 'teeth';
// const brushType = brushSize || (teethOrHair === Brushable.Teeth ? 'small' : 'big');
// return `Today I will brush my ${brushable} with a ${brushType} brush`
// }
// console.log(brush(undefined, Brushable.Teeth))
// // Generics in functions
// function grabOne<T>(options: T[]): T {
// return options[Math.floor(Math.random() * options.length)];
// }
// console.log(grabOne<TFruit>(fruitBowl))
// import * as moment from 'moment';
// import { IAnimal } from './animal';
// import Animal from './animal';
// import { Chicken, IBird } from './chicken';
//
// const chicken = new Chicken();
// console.log(chicken.speak());
// console.log(chicken.getInfo());
//
// const animalSpeakNow = <T extends IAnimal>(animal: Animal<T>) => {
// console.log(`${animal.speak()} at ${moment().format('h:mm:ssa')}`);
// }
// animalSpeakNow(chicken);
//
// const chickenSpeakNow = <T extends IBird & {name: 'chicken'}>(animal: Animal<T>) => {
// console.log(`${animal.speak()} at ${moment().format('h:mm:ssa')}`);
// }
// chickenSpeakNow(chicken);
View
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2015",
"module": "commonjs",
"strict": true
}

0 comments on commit c2d5950

Please sign in to comment.