Skip to content

Instantly share code, notes, and snippets.

@JaykeOps
Last active September 2, 2017 08:11
Show Gist options
  • Save JaykeOps/551f85696fa76146ec4763e542f9a381 to your computer and use it in GitHub Desktop.
Save JaykeOps/551f85696fa76146ec4763e542f9a381 to your computer and use it in GitHub Desktop.
Just some notes I took while going through the TypeScript documentation
//Destructuring Examples
//1.0 Array destructing
let travelClasses = [200, 500, 1000];
let [economy, business, first] = travelClasses;
console.log("Business class price:" + business); //500
//1.1 Swap
[first, business, economy] = [economy, business, first];
console.log("Someone got a cheap firstclass flight!\n" + first); //first = 200
//1.2 Function
function bookRoundTrip([dDate, rDate]: [Date, Date]) {
console.log("Destination flight date: " + dDate);
console.log("Return flight date: " + rDate);
}
let dDate = new Date();
let rDate = new Date();
dDate.setDate(dDate.getDate() + 1);
rDate.setMonth(rDate.getMonth() + 1);
bookRoundTrip([dDate, rDate]);
//2.0 Object Destructing
let flight = {
No: "CR9023453",
NumberOfSeats: 120,
NumberOfSeatsOccupied: 107
};
let { NumberOfSeats, NumberOfSeatsOccupied } = flight;
//2.1 Renaming properties
let { No: FlightNo, NumberOfSeats: Seats, NumberOfSeatsOccupied: SeatsOccupied } = flight;
console.log(Seats);
//3.0 Default values for undefined properties on an object passed into a function
function printFlight(trip: { departure?: string, destination?: string}) {
let { departure = "Hong Kong International Airport",
destination = "Hartsfield–Jackson Atlanta International Airport" } = trip;
console.log("Departure: " + departure + "\nDestination: " + destination);
}
printFlight({ departure: undefined, destination: undefined});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment