Book Image

TypeScript 2.x By Example

By : Sachin Ohri
Book Image

TypeScript 2.x By Example

By: Sachin Ohri

Overview of this book

The TypeScript language, compiler, and open source development toolset brings JavaScript development up to the enterprise level. It allows you to use ES5, ES6, and ES7 JavaScript language features today, including classes, interfaces, generics, modules, and more. Its simple typing syntax enables building large, robust applications using object-oriented techniques and industry-standard design principles. This book aims at teaching you how to get up and running with TypeScript development in the most practical way possible. Taking you through two exciting projects built from scratch, you will learn the basics of TypeScript, before progressing to functions, generics, promises, and callbacks. Then, you’ll get to implement object-oriented programming as well as optimize your applications with effective memory management. You’ll also learn to test and secure your applications, before deploying them. Starting with a basic SPA built using Angular, you will progress on to building, maybe, a Chat application or a cool application. You’ll also learn how to use NativeScript to build a cool mobile application. Each of these applications with be explained in detail, allowing you to grasp the concepts fast. By the end of this book, you will have not only built two amazing projects but you will also have the skills necessary to take your development to the next level.
Table of Contents (11 chapters)

TypeScript Generics

Traditional programming languages had generics for a long time. Microsoft introduced generics in C# in 2005; Java also has generics. JavaScript itself does not have any explicit concept of generics but TypeScript does.

Generics, in layman's terms, is a concept that allows developers to define functions or classes without explicitly defining the types that the said function or class expects but rather allowing a generic definition, which would then be consumed by the calling functions as they see fit.

We have been using generics in our last application and we will be using it again, extensively, in our current application.

Generics allow us to create reusable and consistent component definitions. For example, we can have a function that accepts a string or a number and adds it to a collection.

So, we can either define two functions, each with their parameter...