TypeScript 2.x for Angular Developers: Harness the capabilities of TypeScript to build cutting-edge web apps with Angular
()
About this ebook
Write better, reusable, and predictable Angular apps with typed logic
About This Book- Leverage the power of TypeScript for developing large-scale Angular apps
- Take advantage of the cutting-edge features of TypeScript 2.x to build high-performing, maintainable applications
- Explore the TypeScript implementation of modules, classes, and annotations with Angular components
This book is for developers who would like to learn to use TypeScript to develop Angular applications. A basic JavaScript knowledge would be beneficial.
What You Will Learn- Move from loose types to predictable strict types
- Write self-documented API methods and data structures
- Understand custom generic types for complex data structures
- Implement Contract with Interfaces
- Enhance Dependency Injection in Angular via class constructors
- Explore TypeScript advanced types
- Build Interactive web apps with Angular and TypeScript
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.
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.
Style and approachA step-by-step, tutorial-based guide
Related to TypeScript 2.x for Angular Developers
Related ebooks
Learning Angular - Second Edition: A no-nonsense guide to building real-world apps with Angular 5 Rating: 0 out of 5 stars0 ratingsAngular 6 by Example: Get up and running with Angular by building modern real-world web apps, 3rd Edition Rating: 0 out of 5 stars0 ratingsSwift Functional Programming - Second Edition Rating: 3 out of 5 stars3/5JavaScript by Example Rating: 0 out of 5 stars0 ratingsLearn React with TypeScript 3: Beginner's guide to modern React web development with TypeScript 3 Rating: 0 out of 5 stars0 ratingsVue.js 2.x by Example: Example-driven guide to build web apps with Vue.js for beginners Rating: 0 out of 5 stars0 ratingsProgressive Web Apps with React: Create lightning fast web apps with native power using React and Firebase Rating: 0 out of 5 stars0 ratingsHands-On Software Engineering with Golang: Move beyond basic programming to design and build reliable software with clean code Rating: 0 out of 5 stars0 ratingsLearning C++ Functional Programming Rating: 0 out of 5 stars0 ratingsTypeScript 2.x By Example: Build engaging applications with TypeScript, Angular, and NativeScript on the Azure platform Rating: 0 out of 5 stars0 ratingsNode.js High Performance Rating: 0 out of 5 stars0 ratingsLearning jQuery 3 - Fifth Edition Rating: 0 out of 5 stars0 ratingsLearn WebAssembly: Build web applications with native performance using Wasm and C/C++ Rating: 0 out of 5 stars0 ratingsWindows Presentation Foundation Development Cookbook: 100 recipes to build rich desktop client applications on Windows Rating: 0 out of 5 stars0 ratingsHands-On Object-Oriented Programming with C#: Build maintainable software with reusable code using C# Rating: 0 out of 5 stars0 ratingsHybrid Mobile Development with Ionic Rating: 0 out of 5 stars0 ratingsCreating Templates with Artisteer Rating: 0 out of 5 stars0 ratingsLearn D3.js: Create interactive data-driven visualizations for the web with the D3.js library Rating: 0 out of 5 stars0 ratingsHands-On JavaScript High Performance: Build faster web apps using Node.js, Svelte.js, and WebAssembly Rating: 0 out of 5 stars0 ratings
Programming For You
Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Gray Hat Hacking the Ethical Hacker's Rating: 5 out of 5 stars5/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5C Programming For Beginners: The Simple Guide to Learning C Programming Language Fast! Rating: 5 out of 5 stars5/5Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5HTML in 30 Pages Rating: 5 out of 5 stars5/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5Python QuickStart Guide: The Simplified Beginner's Guide to Python Programming Using Hands-On Projects and Real-World Applications Rating: 0 out of 5 stars0 ratingsLearn PowerShell in a Month of Lunches, Fourth Edition: Covers Windows, Linux, and macOS Rating: 5 out of 5 stars5/5Excel 101: A Beginner's & Intermediate's Guide for Mastering the Quintessence of Microsoft Excel (2010-2019 & 365) in no time! Rating: 0 out of 5 stars0 ratingsNarrative Design for Indies: Getting Started Rating: 4 out of 5 stars4/5JavaScript All-in-One For Dummies Rating: 5 out of 5 stars5/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5C All-in-One Desk Reference For Dummies Rating: 5 out of 5 stars5/5C++ Learn in 24 Hours Rating: 0 out of 5 stars0 ratingsPYTHON: Practical Python Programming For Beginners & Experts With Hands-on Project Rating: 5 out of 5 stars5/5
Reviews for TypeScript 2.x for Angular Developers
0 ratings0 reviews
Book preview
TypeScript 2.x for Angular Developers - Christian Nwamba
TypeScript 2.x for Angular Developers
Harness the capabilities of TypeScript to build cutting-edge web apps with Angular
Christian Nwamba
BIRMINGHAM - MUMBAI
TypeScript 2.x for Angular Developers
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author(s), nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavoured to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: December 2017
Production reference: 1051217
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN: 978-1-78646-055-4
www.packtpub.com
Credits
About the Author
Christian Nwamba is a Lagos, Nigeria-based full stack engineer, developer, and evangelist. He spends most of his life building user experiences with JavaScript and spreading the word to other developers through conferences, meetups, and technical articles.
He has worked with quite a few interesting SaaS companies, such as Cloudinary, Pusher, and Auth0, and has published articles on popular blogs, such as Sitepoint, Scotch.io, Codementor, and Web Designer Depot. This is his first book and he's very proud of the effort he put in to make it enjoyable for the readers.
He has a lot of interest in community building and efforts, especially in Africa. He does this by organizing and facilitating meetups/conferences (forLoop Africa, Angular Nigiera, GDGs, and more).
Acknowledgments
Before anything, I would like to express my sincere gratitude to my parents, Edwin Nwamba and Helen Nwamba, for nurturing and playing a huge role in my literary and technical upbringing. Their guidance gave me a great head start, which made all this possible.
Besides my parents, I would also like to thank the Scotch, Cloudinary, and Packt teams for the continuous and unrestrained support they showed to me during my time writing this book. I couldn’t have asked for a better support in all sincerity.
My sincere thanks also go to Chris Sevilleja, Nick Cerminara, and Prosper Otemuyiwa for challenging me to become even better at what I do and for their encouragement, insightful comments, and hard questions. Also, I thank Marcel Nwamadi and Olayinka Omole for contributing to the technical success of this work.
I would like to thank Angular Nigeria and ForLoop Africa, two communities whose growth in recent times spurred me to carry on and not to relent in my efforts to make programming the mainstay in Africa.
I thank my friends, William Imoh, Raphael Ugwu, Nnaemeka Ogbonnaya, and Chioma Imoh, for the stimulating discussions, the constant reminders and evaluations, and for being with me in good times and bad through the last five years.
Last but not least, I would like to thank my siblings, Kelechi Nwamba, Ezinne Nwamba, Ekoma Nwamba, and Chioma Nwamba, for supporting me throughout my life.
About the Reviewer
Andrew Leith Macrae first cut his programming teeth on an Apple IIe, poking bytes into
the RAM. Over the years, he has developed interactive applications with Hypercard,
Director, Flash, and more recently, Adobe AIR for mobile. He has also worked with HTML
since there was HTML to work with and is currently working as a senior frontend
developer at The Learning Channel (www.tsc.ca), using Angular 4 with TypeScript.
He is convinced that TypeScript is the future of JavaScript, bringing the structure and
discipline of strongly typed object-oriented language to facilitate efficient, intentional
coding for the development of large-scale applications for the web.
You can find out more about Andrew or contact him at www.adventmedia.net.
www.PacktPub.com
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/1787286436.
If you'd like to join our team of regular reviewers, you can email us at [email protected]. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Table of Contents
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
From Loose Types to Strict Types
Term definitions
Implications of loose types
The problem
Mitigating loose-type problems
The typeof operator
The toString method
Final Note
Summary
Getting Started with TypeScript
Setting up TypeScript
Hello World
Type annotation in TypeScript
ES6 and beyond
User story
Final notes
Summary
Typescript Native Types and Features
Basic types
Strings
Numbers
Boolean
Arrays
Void
Any
Tuple
Enums
Functions and function types
Function parameters
Function return value
Optional parameters
Interfaces
Interfaces for JavaScript object types
Optional properties
Read-only properties
Interfaces as contracts
Decorators
Decorating methods
Decorating classes
Decorator factories
Summary
Up and Running with Angular and TypeScript
Setting up Angular and TypeScript
Creating a new Angular project
Project structure
Generating files
Basics concepts
Components
Templates
Component styles
Modules
Unit testing
Summary
Advanced Custom Components with TypeScript
Lifecycle hooks
DOM manipulation
ElementRef
Hooking into content initialization
Handling DOM events
View encapsulation
Emulated
Native
None
Summary
Component Composition with TypeScript
Component composability
Hierarchical composition
Component communication
Parent-child flow
Intercepting property changes
Child–parent flow
Accessing properties and methods of a child via a parent component
Accessing child members with ViewChild
Summary
Separating Concerns with Typed Services
Dependency injection
Data in components
Data class services
Component interaction with services
Services as utilities
Summary
Better Forms and Event Handling with TypeScript
Creating types for forms
The form module
Two-way binding
More form fields
Validating the form and form fields
Submitting forms
Handling events
Mouse events
Keyboard events
Summary
Writing Modules, Directives, and Pipes with TypeScript
Directives
Attribute directives
Handling events in directives
Dynamic attribute directives
Structural directives
Why the difference?
The deal with asterisks
Creating structural directives
Pipes
Creating pipes
Passing arguments to pipes
Modules
Summary
Client-Side Routing for SPA
RouterModule
Router directives
Master-details view with routes
Data source
Blog service
Creating routes
Listing posts in the home component
Reading an article with the post component
Summary
Working with Real Hosted Data
Observables
The HTTP module
Building a simple todo demo app
Project setup
Building the API
Installing diskdb
Updating API endpoints
Creating an Angular component
Creating application routes
Creating a todos service
Connecting the service with our todos component
Implementing the view
Build a user directory with Angular
Create a new Angular app
Create a Node server
Install diskDB
Create a new component
Create a service
Updating user.component.ts
Updating user.component.html
Running the app
Summary
Testing and Debugging
Angular 2 testing tools
Jasmine
Main concepts of Jasmine
Karma
Protractor
Angular testing platform
Using Karma (with Jasmine)
Creating a new project
Installing the Karma CLI
Configuring Karma
Testing the components
Testing services
Testing using HTTP
Testing using MockBackend
Testing a directive
Testing a pipe
Debugging
Augury
Augury features
Component tree
Router tree
Source map
Summary
Preface
Deciding to learn Angular can be quite overwhelming. This is because the de facto way of writing an Angular app is by using a superset language, TypeScript, which is fairly new. Ironically, TypeScript simplifies how Angular apps are written by providing strict types (as seen in strictly typed languages such as Java), which improves the predictive behavior of the apps we write. This book is intended to assist you as a beginner/intermediate Angular developer with little or no knowledge of TypeScript or strict typing, in general, by explaining the core concepts of TypeScript.
What this book covers
Chapter 1, From Loose Types to Strict Types, discusses the problems developers faced before TypeScript was introduced, and what problems TypeScript solves. We will do this by discussing loose types and their challenges, and show some examples of how these challenges were being tackled previously and why TypeScript is a better choice.
Chapter 2, Getting Started with Typescript, gives a summary of TypeScript's core concepts with practical examples on how to set up a vanilla JavaScript plus TypeScript project. All loosely typed examples in Chapter 1, From Loose Types to Strict Types, will be rewritten with TypeScript to demonstrate TypeScript's efficiency.
Chapter 3, Typescript Native Types and Features, takes a deep dive into built-in TypeScript strict types that match existing JavaScript loose types. Each type will be discussed extensively with working examples, showing how it should be used and how it should work.
Chapter 4, Up and Running with Angular and TypeScript, discusses how TypeScript applies to Angular. To do so, Angular needs to be up and running with the help of the CLI tool. In this chapter, we will discuss what it takes to have Angular and TypeScript work together. We will also cover the basic Angular concepts that you could find in a Hello World example.
Chapter 5, Advanced Custom Components with TypeScript, discusses the concept of web components and how Angular builds on web components with TypeScript. We will see how to create components with classes, implement lifecycle hooks with TypeScript interfaces, and define components, metadetails using TypeScript decorators.
Chapter 6, Component Composition with TypeScript, discusses how Angular is component-based. It explains that components are composed together as building blocks to make a fully functional app. We will discuss modularizing components using