Skip to content

Instantly share code, notes, and snippets.

View PranavBhatia's full-sized avatar

Pranav Bhatia PranavBhatia

  • Montreal, Quebec
View GitHub Profile
@PranavBhatia
PranavBhatia / employeeGenerics.ts
Last active March 23, 2020 15:34
A very basic example of generics using typescript.
// Generics are types which can hold/use several types
let employeeNamesArray: Array<string>; // This array will only accept strings
// employeeNamesArray = [123]; // => Error
employeeNamesArray = ['Bob', 'Sam'];
console.log(employeeNamesArray); // Result => [ 'Bob', 'Sam' ]
// Interface can also be called a contract.
// We can use them to define custom types without creating classes.
// Interfaces ARE NOT compiled to JavaScript! It's just for checking/validation done by our TypeScript compiler.
// Employee interface
interface Employee {
employeeName: string;
salary: number;
mobileNumber?: string; // Optional property => Does not have to be implemented
}
@PranavBhatia
PranavBhatia / employee.ts
Last active March 22, 2020 22:50
We might have some extra code for defining types but in the end, it's only for the better.
// Employee class is bascially a blueprint of any employee object that can be instantiated with this piece of code.
class Employee {
public employeeName: string; // A public variable of type STRING
private salary: number; // A private variable of type NUMBER
constructor(employeeName: string, salary: number) {
this.employeeName = employeeName;
this.salary = salary;
} // A contructor to create an employee object with a predefined salary and name.