class Menu { // Our properties: // By default they are public, but can also be private or protected. private items: Array<string>; // The items in the menu, an array of strings. protected pages: number; // How many pages will the menu be, a number. readonly other: string;
get numOfPages(): number { return pages.length; }
// A straightforward constructor. constructor(item_list: Array<string>, total_pages: number) { // The this keyword is mandatory. this.items = item_list; this.pages = total_pages; }
// Methods public list(): void { console.log("Our menu for today:"); for(var i=0; i<this.items.length; i++) { console.log(this.items[i]); } }
}
// Create a new instance of the Menu class. var sundayMenu = new Menu(["pancakes","waffles","orange juice"], 1);
class HappyMeal extends Menu { // Properties are inherited
// A new constructor has to be defined. constructor(item_list: Array<string>, total_pages: number) { // In this case we want the exact same constructor as the parent class (Menu), // To automatically copy it we can call super() - a reference to the parent's constructor. super(item_list, total_pages); }
// Just like the properties, methods are inherited from the parent. // However, we want to override the list() function so we redefine it. list(): void{ console.log("Our special menu for children:"); for(var i=0; i<this.items.length; i++) { console.log(this.items[i]); }
} }
// Create a new instance of the HappyMeal class. var menu_for_children = new HappyMeal(["candy","drink","toy"], 1);
// This time the log message will begin with the special introduction. menu_for_children.list();
Generics
Generics are templates that allow the same function to accept arguments of various different types. Creating reusable components using generics is better than using the any data type, as generics preserve the types of the variables that go in and out of them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// The <T> after the function name symbolizes that it's a generic function. // When we call the function, every instance of T will be replaced with the actual provided type.
// Receives one argument of type T, // Returns an array of type T.
functiongenericFunc<T>(argument: T): T[] { var arrayOfT: T[] = []; // Create empty array of type T. arrayOfT.push(argument); // Push, now arrayOfT = [argument]. return arrayOfT; }
var arrayFromNumber = genericFunc(42); console.log(arrayFromNumber[0]); // 42 console.log(typeof arrayFromNumber[0]) // number
Modules
External modules as comparing to Namespaces.
Third-party Declaration Files
When using a library that was originally designed for regular JavaScript, we need to apply a declaration file to make that library compatible with TypeScript. A declaration file has the extension .d.ts and contains various information about the library and its API.
TypeScript declaration files are usually written by hand, but there’s a high chance that the library you need already has a .d.ts. file created by somebody else. DefinitelyTyped is the biggest public repository, containing files for over a thousand libraries. There is also a popular Node.js module for managing TypeScript definitions called Typings.