Created
December 17, 2020 01:25
-
-
Save jose-almir/6f8b706ebc37eadb26744e4a130a586f to your computer and use it in GitHub Desktop.
TypeScript Fast Crash Course Annotations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TypeScript Fast Crash Course Annotations | |
// Type inference | |
const car = { | |
brand: 'BMW', | |
engine: '3.4', | |
run: () => { | |
console.log(`Running ${car.brand}`); | |
} | |
}; | |
// Types I (Similar to Kt declaring variables) | |
let firstName: string = "John"; | |
let age: number = 18; | |
let isStudent: boolean = true; | |
let height: number = 1.83; | |
let notes: number[] = [7.6, 9.5, 7.1]; | |
// Types II (any type) | |
let anyValue; | |
anyValue = 1; | |
anyValue = "John"; | |
let anyValues: any[] = [1, "John", 1.83, 18, [7.6, 9.5, 7.1], true]; | |
// Types III (Kt-like function parameters) | |
// Obs: without specify parameters type, Ts will infer that is any type. | |
function sum(a: number, b: number): number { | |
return a + b; | |
} | |
// Functions as types | |
function mod(a: number, b: number): number { | |
return a % b; | |
} | |
const func = mod; | |
let f: () => string; | |
f = () => "hello"; | |
function fold(onLeft: (a: number) => number) { // Function as parameter | |
return onLeft(1); | |
} | |
function a(): () => number { // Returning function | |
return () => 1; | |
} | |
// Objects as types | |
const dog: { name: string, bark: () => any } = { | |
name: "Rex", | |
bark: () => console.log('Au Au') | |
} | |
let cat: { name: string, meow: () => any }; | |
// Union types | |
let eitherNumberOrString: number | string; | |
eitherNumberOrString = 1; | |
eitherNumberOrString = "I'm string"; | |
// Tuple typle | |
const point: [number, number, string] = [20, 45, "#ffffff"]; | |
// Defining and using Interfaces | |
interface Person { | |
name: string, | |
age: number, | |
talk: () => void | |
} | |
const student: Person = { // Object implementing Person interface | |
name: "John", | |
age: 15, | |
talk: () => console.log('Hi!') | |
} | |
interface Animal { | |
name: string, | |
eat(): void, | |
sleep(): void | |
} | |
const dog2: Animal = { // another way to implement methods (more spaced) | |
name: "Rex", | |
eat() { | |
console.log('dsdpadsad'); | |
}, | |
sleep() { | |
console.log('zzzzzz'); | |
} | |
} | |
// Interfaces to functions | |
function bath(animal: Animal) { | |
console.log('You are a good boy, take a biscuit, ' + animal.name); | |
animal.eat() | |
} | |
// Interface to classes | |
class Bird implements Animal { | |
name: string; | |
// name = "Blu"; or | |
constructor(name: string) { | |
this.name = name; | |
} | |
eat = () => { console.log('Biadsada') } // this | |
sleep() { // or this | |
console.log('zzzzzzzzB'); | |
} | |
} // bath(new Bird("Blue")); // test this | |
// bath(dog2); // and this | |
// Interface inheritance | |
interface Employee extends Person { | |
salary: number, | |
hasBenefit: boolean | |
} // A employee is a person | |
class Recepcionist implements Employee { | |
hasBenefit: boolean; | |
constructor(public name: string, public age: number, public salary: number) { | |
this.hasBenefit = salary < 500; | |
} | |
talk() { | |
console.log('Hello, welcome!') | |
} | |
} | |
// Class definition (basic) | |
class Tree { | |
name: string; | |
constructor(name: string) { | |
this.name = name; | |
} | |
} | |
class Tree2 { | |
constructor(public name: string) { } | |
} //console.log(new Tree2("sad").name) // try this | |
// Access modifiers | |
class BankAccount { | |
constructor(private total: number = 100.0) { } | |
private getSomeMoney(value: number) { | |
this.total -= value; | |
} | |
} | |
// Abstract class | |
abstract class Device { | |
abstract emitSignal(): void; // abstract method = without impl | |
} | |
class Phone extends Device { | |
emitSignal() { | |
console.log('[run signal]'); | |
} | |
} // new Phone().emitSignal(); // test this | |
// Generics | |
function identity<T>(value: T): T { | |
return value; | |
}// identity(1); // try this returns 1 | |
// Class Generics | |
// My exercise was create a Either type from FP :) | |
// This applies generics, inheritance and polymorphism | |
interface Either<L, R> { | |
fold<B>(onLeft: (left: L) => B, onRight: (right: R) => B): B; | |
} | |
class Left<L, R> implements Either<L, R>{ | |
constructor(private value: L) {} | |
fold<B>(onLeft: (left: L) => B, onRight: (right: R) => B): B { | |
return onLeft(this.value); | |
} | |
} | |
class Right<L, R> implements Either<L, R> { | |
constructor(private value: R) {} | |
fold<B>(onLeft: (left: L) => B, onRight: (right: R) => B): B { | |
return onRight(this.value); | |
} | |
} | |
// Generics constraints | |
function conversationBetweenPeople<T extends Person>(people: T[]) { | |
for(let person of people){ | |
person.talk(); | |
} | |
} | |
// Namespaces | |
namespace arithmetic { | |
export function sum(a: number, b: number){ | |
return a + b; | |
} | |
export function times(a: number, b: number){ | |
return a * b; | |
} | |
export function div(a: number, b: number){ | |
return a / b; | |
} | |
export function minus(a: number, b: number){ | |
return a - b; | |
} | |
export function mod(a: number, b: number){ | |
return a % b; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment