Skip to content

Instantly share code, notes, and snippets.

@kshirish
Created July 10, 2020 10:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kshirish/cb85ddaac038045558c1a5d524a6a3c6 to your computer and use it in GitHub Desktop.
Save kshirish/cb85ddaac038045558c1a5d524a6a3c6 to your computer and use it in GitHub Desktop.
Learning typescript
// Typescript Documentation:
// https://www.typescriptlang.org/docs/
// Basic Types
const message: string = "Hello world";
const messageArr: Array<string> = [message];
console.log(message);
console.log(messageArr);
const isValid: boolean = false;
const isValidArr: Array<boolean> = [isValid];
console.log(isValid);
console.log(isValidArr);
const noOfSocks: number = 3;
const noOfSocksArr: Array<number> = [noOfSocks];
console.log(noOfSocks);
console.log(noOfSocksArr);
const x: [string, number] = ["Foo", 100];
console.log(x);
const [y, z]: [string, number] = ["Foo", 100];
console.log(y, z);
const { a, b }: { a: string; b: number } = { a: "Wind", b: 800 };
console.log(a, b);
const obj: Object = { name: "Foo Bar", age: 33 };
console.log(obj);
const arr: Array<any> = [1, 3, "100"];
console.log(arr);
const test = (): number => {
console.log("This is test function!");
return 0;
};
test();
const partial = (): Function => {
return (): Array<number> => {
return [1, 100];
};
};
console.log(partial()());
// Interfaces
interface Label {
label: string;
}
const printLabel = (labelObj: Label) => console.log(labelObj.label);
printLabel({ label: "This is a label!" });
interface User {
readonly id: string;
email: string;
phone?: string;
}
const u: User = {
id: "ERTYU234",
email: "support@example.com",
phone: "9876543210"
};
u.email = "care@example.com";
console.log(u);
interface SumType {
(a: number, b: number): number;
}
const add: SumType = (x, y) => x + y;
console.log(add(2, 10));
interface ClockInterface {
currentTime: Date;
setTime(d: Date): void;
}
class Clock implements ClockInterface {
currentTime: Date = new Date();
setTime(d: Date) {
this.currentTime = d;
}
constructor(public hours: number, private mins: number) {}
}
const c = new Clock(10, 20);
console.log(c);
// Classes
class Animal {
name: string;
constructor(theName: string) {
this.name = theName;
console.log("Hey this is Animal constructor.");
}
move(distanceInMeters: number = 0) {
console.log(`${this.name} moved ${distanceInMeters}m.`);
}
}
class Snake extends Animal {
move(distanceInMeters = 5) {
console.log("Slithering...");
super.move(distanceInMeters);
}
}
class Horse extends Animal {
move(distanceInMeters = 45) {
console.log("Galloping...");
super.move(distanceInMeters);
}
}
const sam = new Snake("Sammy the Python");
const tom: Animal = new Horse("Tommy the Palomino");
sam.move();
tom.move(34);
class Bottle {
public company: string;
protected price: string;
private manufacturedAt: string;
constructor(company: string, price: string, manufacturedAt: string) {
this.company = company;
this.price = price;
this.manufacturedAt = manufacturedAt;
}
info() {
console.log("The bottle is manufactured at " + this.manufacturedAt);
}
}
const k = new Bottle("Kinley", "20", "Kanpur");
console.log(k);
k.info();
class Octopus {
readonly name: string;
readonly numberOfLegs: number = 8;
constructor(theName: string) {
this.name = theName;
}
}
const oswald = new Octopus("Man with the 8 strong legs");
console.log(oswald);
class Employee {
static MAX_FULL_LENGTH: number = 10;
private x: string = "";
get fullName(): string {
return this.x;
}
set fullName(newName: string) {
if (newName && newName.length > Employee.MAX_FULL_LENGTH) {
throw new Error(
"fullName has a max length of " + Employee.MAX_FULL_LENGTH
);
}
this.x = newName;
}
}
const employee = new Employee();
employee.fullName = "Bob Smith";
console.log(employee.fullName);
// Functions
const sayHello = ({ name }: { name: string; age?: number }): void =>
console.log("Hello " + name);
sayHello({ name: "Joe" });
const sayHi = (person: { name: string; age?: number }): void =>
console.log("Hi " + person.name);
sayHi({ name: "Joe" });
type Person = { name: string; age?: number };
const greet = (person: Person): void =>
console.log("Good morning " + person.name);
greet({ name: "Joe" });
interface Human {
name: string;
age?: number;
}
const wish = (human: Human): void =>
console.log("Happy Birthday " + human.name);
wish({ name: "Joe" });
const buildName = (firstName: string, ...restOfName: Array<string>) => {
return firstName + " " + restOfName.join(" ");
};
const employeeName = buildName("Joseph", "Samuel", "Lucas", "MacKinzie");
console.log(employeeName);
// Generics
const identity = <R>(a: R): R => a;
console.log(identity<string>("Hello!"));
console.log(identity<Array<number>>([1, 8, 10]));
const getLength = <T>(a: Array<T>): number => {
return a.length;
};
console.log(getLength([[1, 8], ["Hello"], [false, true, false, false]]));
class GenericNumber<T, R> {
myBad: R;
zeroValue: T;
add: (x: T, y: T) => T;
}
const myGenericNumber = new GenericNumber<number, boolean>();
myGenericNumber.zeroValue = 0;
myGenericNumber.myBad = false;
myGenericNumber.add = function(x, y) {
return x + y;
};
const stringNumeric = new GenericNumber<string, string>();
stringNumeric.zeroValue = "";
stringNumeric.myBad = "Something something";
stringNumeric.add = function(x, y) {
return x + y;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment