This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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. |