Book Image

TypeScript 3.0 Quick Start Guide

By : Patrick Desjardins
Book Image

TypeScript 3.0 Quick Start Guide

By: Patrick Desjardins

Overview of this book

<p>TypeScript is designed for the development of large applications and can be used to develop JavaScript applications for both client-side and server-side execution. This book is the ideal introduction to TypeScript, covering both the basics and the techniques you need to build your own applications.</p> <p>We start by setting up the environment and learning about the build tools that support TypeScript. Then we look at scoping of a variable, and the difference between a undefined variable and a null variable. You will then see the difference between an object, an Object, an object literal, and an object built with a constructor, crucial concepts in understanding TypeScript.</p> <p>You will learn how to make your code more generic to increase the reusability of your classes, functions, and structures, and to reduce the burden of duplicating code. We look at creating definition files to transform the actual JavaScript code to be compatible with TypeScript.</p> <p>By the end of the book, you will have worked with everything you need to develop stunning applications using TypeScript.</p>
Table of Contents (14 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
Free Chapter
1
Getting Started with TypeScript
Index

Using an object from a class versus an object literal


An object literal is quick to build and does not need to have the data passed down a constructor or public members to fill an object. An object literal is an efficient way to move data around. You can read a JSON file, receive the data from an Ajax call or from many sources, and cast to the desired type:

ajax.then((response:any)=>{
   return response as DataTyped;
};

However, if you have many functions or more complex logic that needs to be encapsulated, a class is more pragmatic. The reason is that using an object literal would require assigning the function on each instance manually. Also, a class can contain a private function that you may not want to expose, provide encapsulation with private/protected and interfaces. An object literal's fields are public and accessible. In the following example, we see data returned from an Ajax call. The expected type is ObjectDefinition, which has a function. The function doesn't come for free...