Book Image

TypeScript 2.x for Angular Developers

By : Christian Nwamba
Book Image

TypeScript 2.x for Angular Developers

By: Christian Nwamba

Overview of this book

<p>TypeScript, a superset of JavaScript, is the default language for building Angular applications from Google. TypeScript 2.0 adds optional static types, classes, and modules to JavaScript, to enable great tooling and better structuring of large JavaScript applications.</p> <p>This book will teach you how to leverage the exciting features of TypeScript while working on Angular projects to build scalable, data-intensive web applications. You will begin by setting up and installing TypeScript and then compile it with JavaScript. You will then learn to write simple JavaScript expressions with TypeScript. You will then go through some built in TypeScript types and accessors. Further you will build web components using TypeScript Interfaces and Decorators. You will go through the Angular form module, which will enable you to write predictable typed forms with TypeScript and learn to use typed DOM events. You will also build a single-page application using the Router module. Lastly, you will consume API data with Angular's HTTP Module and handle responses with RxJS observables. At the end of the book, you will go through different testing and debugging techniques.</p>
Table of Contents (20 chapters)
Title Page
Credits
About the Author
Acknowledgments
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
9
Writing Modules, Directives, and Pipes with TypeScript

Chapter 1. From Loose Types to Strict Types

JavaScript is loosely typed. It's worth repeating, JavaScript is loosely typed. Notice how the sentence is passive--we cannot categorically hold someone responsible for the loose-type nature of JavaScript just as we can't do so for other famous glitches of JavaScript.

A detailed discussion on what loose-types and loosely typed languages are will help aid your understanding of the problem that we plan to solve with this book.

When a programming language is loosely typed, it means that the data passed around using variables, functions, or whatever member applicable to the language does not have a defined type. A variable x could be declared, but the kind of data it holds is never certain. Loosely typed languages are contrary to strongly typed languages, which enforce that every declared member must strictly define what sort of data it can hold.

These types are categorized into:

  • Strings
  • Numbers (int, float, and so on.)
  • Data structures (arrays, lists, objects, maps, and so on.)
  • Boolean (true and false)

JavaScript, PHP, Perl, Ruby, and so on, are all examples of loosely typed languages. Java, C, C#, are examples of strongly typed languages.

In loosely typed languages, a member may be initially defined as a string. Down the line, this member could end up storing a number, a boolean, or even a data structure. This instability leads us to the implications of loosely typed languages.