Skip to content

Instantly share code, notes, and snippets.

@vivektikar25
Last active November 29, 2019 03:58
Show Gist options
  • Save vivektikar25/30f58a6ab5e17da1b089d44424c0fd9b to your computer and use it in GitHub Desktop.
Save vivektikar25/30f58a6ab5e17da1b089d44424c0fd9b to your computer and use it in GitHub Desktop.
// ====================================== *********** ==============================
// Function Currying
// ====================================== *********** ==============================
function multiply(a, b, c) {
return a * b * c;
}
function multiply(a) {
return (b) => {
return (c) => {
return a * b * c
}
}
}
console.log(multiply(1)(2)(3)) // 6
let finalMultipleFn = multiply(1)(2);
calculateSomething(finalMultipleFn)
async function calculateSomething(fn) {
let x = await ajaxCall();
return fn(x)
};
// Discounted price example
// let getDiscountedPrice = (percentDiscount) => {
// return (price) => (price) - (price * percentDiscount) / 100;
// }
// let getPriceWith10PercentDiscount = getDiscountedPrice(10);
// let getPriceWith20PercentDiscount = getDiscountedPrice(20);
// let price1 = getPriceWith10PercentDiscount(100)
// let price2 = getPriceWith20PercentDiscount(100)
// console.log(price1);
// console.log(price2);
// ====================================== *********** ==============================
// Basic types in TS
// ====================================== *********** ==============================
const color: string = "blue";
const isDone: boolean = false;
const list1: number[] = [1, 2, 3];
const list2: Array<number> = [1, 2, 3];
const notSure: any = 4;
const multiType: string | number | boolean = true;
// ******** Functions ************ //
function foo(message) {
return message === "Hello";
}
function bar(message: string): boolean {
return message === "Hello";
}
bar(25);
let baz = (message: number | string): string => "xyz"
// ====================================== *********** ==============================
// Classes in TS
// ====================================== *********** ==============================
class Animal {
private name: string;
protected color: string; // Accessible in child class
constructor(theName: string, theColor: string) {
this.name = theName;
this.color = theColor;
}
private sayHi() {
console.log("Hi");
}
}
const animal = new Animal("Cat", "White");
console.log(animal.name); // Error: 'name' is private;
console.log(animal.color); // Error: 'color' is protected; with nice information on hover
animal.sayHi();
// ====================================== *********** ==============================
// Interfaces in TS
// ====================================== *********** ==============================
// Interface example ********************
interface IsroInterface {
vehicleName?: string;
readonly launchDate: Date;
isLauncherReady: () => boolean; // no body just defination
}
class Isro implements IsroInterface {
vehicleName: string;
readonly launchDate: Date;
constructor() {
this.launchDate = new Date();
this.vehicleName = "chandrayan";
}
changeLaunchDate() {
this.launchDate = new Date("2019-11-23");
}
// isLauncherReady() {
// return true;
// }
}
// Interface extending and combining ********************
interface Circle {
radius: number;
}
interface Shape extends Circle {
area: number;
calc: (length: number) => number
}
let obj: Shape = {
radius: 10,
area: 100,
calc: length => length * length
};
// Merging interface
interface Person {
name: string;
}
interface Person {
age: number;
}
interface Person {
zipCode: number;
}
let obj: Person = {
name: "Vivek",
zipCode: 411058,
age: 26,
};
// ====================================== *********** ==============================
// Generics in TS
// ====================================== *********** ==============================
function identity(arg: string): string {
return arg;
}
function identity<T>(arg: T): T { // T is placeholder
return arg;
}
// Multiple generic variables *****************
let output1 = identity<number>(10); // If not provided 'number' as value of T here
// T will be 10 then
let output2 = identity("myString"); // In this case T will be "myString" not string
// On hover it shows T's value.
// Suggest properties on return type of it.
function identityNew<T, U>(arg1: T, arg2: U): [T, U] {
const list: [T, U] = [arg1, arg2];
return list;
}
identityNew("xyz", 123);
// Generic Classes *****************
class GenericNumber<T, U> {
numbValue: T;
identity(x: U): U {
return x;
}
}
let myGenericNumber = new GenericNumber<number, string>();
// We can also have generic interfaces
// Generic Interfaces *****************
// interface Identities<V, W> {
// id1: V;
// id2: W;
// }
// class ABC implements Identities<string, number> {
// id1: string = "abc";
// id2: number = 20;
// }
// ====================================== *********** ==============================
// Decorators in TS
// ====================================== *********** ==============================
// ******************************** //
// Can change class or function body, constructor code or methods on classes;
// ******************************** //
// *********** Replace Class **************
const replaceClass = (originalConstructor) => {
debugger;
return class newPet {
constructor() { console.log("In new PET class") }
printMe() {
console.log("New printMe method")
}
}
}
@replaceClass
class Pet {
constructor(name: string, age: number) { }
printMe() {
console.log("Orignal printMe method")
}
}
console.log(Pet)
// *********** Change Class **************
const replaceClassMethod = (originalConstructor) => {
originalConstructor.prototype.printMe = function printMe() {
console.log("New printMe method")
}
return originalConstructor;
}
@replaceClassMethod
class Pet {
constructor(name: string, age: number) { }
printMe() {
console.log("Orignal printMe method")
}
}
const pet = new Pet("Azor", 12)
pet.printMe()
// *********** Method decorator **************
class Greeter {
@greetWithNewMessage
greet() {
return "Hello";
}
}
function greetWithNewMessage(target: any, propertyKey: string, descriptor: PropertyDescriptor) {
debugger;
descriptor.value = function () {
return "How are you";
};
}
const greeter = new Greeter();
console.log(greeter.greet())
// Use of decorators.
class CRUD {
    get() { }
    post() { }
 
    @admin
    delete() { }
 
    @owner
    put() { }
}
// One can decorate class properties as well as method parameters.
// ====================================== *********** ==============================
// Why TypeScript - IntelliSense, Error detection
// ====================================== *********** ==============================
interface IsroInterface {
vehicleName: string;
launchDate: Date;
launch: () => void;
isLauncherReady: () => boolean;
}
class Isro implements IsroInterface {
vehicleName: string;
launchDate: Date;
constructor() {
this.launchDate = new Date();
this.vehicleName = "chandrayan";
}
launch() {
if (this.launchDate === "2019-11-23") {
console.log("Launch rocket");
}
}
isLauncherReady() {
return "yes";
}
}
const isro = new Isro()
isro.
// Shows intelliscence, Function details (In real case it might be in different file), Errors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment