Skip to content

Instantly share code, notes, and snippets.

@jose4125
Created January 27, 2020 17:01
Show Gist options
  • Select an option

  • Save jose4125/53cb4d4e1913d19f3410b5e386e6eccf to your computer and use it in GitHub Desktop.

Select an option

Save jose4125/53cb4d4e1913d19f3410b5e386e6eccf to your computer and use it in GitHub Desktop.
// INTERSECTION TYPES
interface Admin {
name: string;
privileges: string[];
}
interface Employee {
name: string;
startDate: Date;
}
type ElevatedEmployee = Admin & Employee;
const e1: ElevatedEmployee = {
name: 'jose',
privileges: ['create-server'],
startDate: new Date()
}
type Combinable = string | number;
type Numeric = number | boolean
type Universal = Combinable & Numeric;
// TYPES GUARDS
function add(a: Combinable, b: Combinable) {
if (typeof a === 'string' || typeof b === 'string') {
return a.toString() + b.toString();
}
return a + b;
}
type UnknownEmployee = Employee | Admin;
function printEmployeeInformation(emp: UnknownEmployee) {
console.log('name = ', emp.name);
if ('privileges' in emp) {
console.log('privilege = ', emp.privileges);
}
if ('startDate' in emp) {
console.log('start date = ', emp.startDate);
}
}
printEmployeeInformation(e1);
class Car {
drive(){console.log('driving')}
}
class Truck {
drive(){console.log('driving a truck')}
loadCargo(amount: number){console.log(`loading cargo ${amount}`)}
}
type Vehicle = Car | Truck;
const v1 = new Car()
const v2 = new Truck()
function useVehicle(vehicle: Vehicle) {
vehicle.drive();
if (vehicle instanceof Truck) {
vehicle.loadCargo(1000)
}
}
useVehicle(v1);
useVehicle(v2);
// DISCRIMINATED UNOINS
interface Bird {
type: 'bird';
flyingSpeed: number;
}
interface Horse {
type: 'horse';
runningSpeed: number;
}
type Animal = Bird | Horse;
function moveAnimal(animal: Animal) {
let speed;
switch (animal.type) {
case 'bird':
speed = animal.flyingSpeed;
break
case 'horse':
speed = animal.runningSpeed;
}
console.log('moving with speed: ', speed);
}
moveAnimal({type: 'bird', flyingSpeed: 100});
// TYPE CASTING
const paragraph = <HTMLInputElement>document.querySelector('#text')!;
const paragraph2 = document.querySelector('#text')! as HTMLInputElement;
paragraph.value = 'hi there';
// INDEX PROPERTIES
interface ErrorContainer {
[keyName: string]: string;
}
const errorBag: ErrorContainer = {
'email': 'not a valid number',
'user-name': 'user name is required'
}
// FUNCTION OVERLOADS
function addOverload(a: number, b: number): number;
function addOverload(a: string, b:string): string;
function addOverload(a: Combinable, b: Combinable) {
if (typeof a === 'string' || typeof b === 'string') {
return a.toString() + b.toString();
}
return a + b;
}
const add1 = addOverload(1,3);
const add2 = addOverload('jose', 'lombana');
// OPTIONAL CHAINING
const fetchUserData = {
id: 'u1',
name: 'jose david',
job: {
title: 'developer',
description: 'frontend dev - backend'
}
}
console.log(fetchUserData?.job?.title);
// NULLISH COHALESCING
const userInput = null;
const storedData = userInput ?? 'default'; // print default when is null or undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment