Book Image

The TypeScript Workshop

By : Ben Grynhaus, Jordan Hudgens, Rayon Hunte, Matt Morgan, Vekoslav Stefanovski
5 (1)
Book Image

The TypeScript Workshop

5 (1)
By: Ben Grynhaus, Jordan Hudgens, Rayon Hunte, Matt Morgan, Vekoslav Stefanovski

Overview of this book

By learning TypeScript, you can start writing cleaner, more readable code that’s easier to understand and less likely to contain bugs. What’s not to like? It’s certainly an appealing prospect, but learning a new language can be challenging, and it’s not always easy to know where to begin. This book is the perfect place to start. It provides the ideal platform for JavaScript programmers to practice writing eloquent, productive TypeScript code. Unlike many theory-heavy books, The TypeScript Workshop balances clear explanations with opportunities for hands-on practice. You’ll quickly be up and running building functional websites, without having to wade through pages and pages of history and dull, dry fluff. Guided exercises clearly demonstrate how key concepts are used in the real world, and each chapter is rounded off with an activity that challenges you to apply your new knowledge in the context of a realistic scenario. Whether you’re a hobbyist eager to get cracking on your next project, or a professional developer looking to unlock your next promotion, pick up a copy and make a start! Whatever your motivation, by the end of this book, you’ll have the confidence and understanding to make it happen with TypeScript.
Table of Contents (16 chapters)
Preface

About the Book

By learning TypeScript, developers can start writing cleaner, more readable code that is easier to understand and less likely to contain bugs. What's not to like?

It's certainly an appealing prospect, but learning a new language can be challenging, and it's not always easy to know where to begin. This book is the perfect place to start. It provides the ideal platform for JavaScript programmers to get to grips with writing eloquent, productive TypeScript code.

Unlike many theory-heavy books, The TypeScript Workshop balances clear explanations with opportunities for hands-on practice. You'll quickly be up and running building functional websites, without having to wade through pages and pages of history and dull, dry fluff. Guided exercises clearly demonstrate how key concepts are used in the real world, and each chapter is rounded off with an activity that challenges you to apply your new knowledge in the context of a realistic scenario.

Whether you're a hobbyist eager to get cracking on your next project or a professional developer looking to unlock your next promotion, pick up a copy and make a start! Whatever your motivation, by the end of this book, you'll have the confidence and understanding to make it happen with TypeScript.

About the Authors

Ben Grynhaus is a full-stack developer with a passion for the frontend. With over 7 years of experience, most of them in web development working with various tech stacks, he specializes in TypeScript, React, and Angular. Ben has worked on several products at Microsoft and is now part of an innovative start-up in the marketing domain. He has published numerous open source npm modules that help in Angular app development, especially when integrating with React.

Jordan Hudgens is a full-stack developer and the founder of DevCamp and the Bottega Code School. As a developer over the past 15 years, he specializes in Ruby on Rails, React, Vue.js, and TypeScript with a focus on API development. He has built applications for a wide variety of organizations, including Eventbrite and Quip. He has published and maintains multiple open source Node Package Manager (npm) modules that help individuals automate the development process for JavaScript and TypeScript applications. Additionally, he has published over 30 courses, taught 42,000 students globally, and written several programming books.

Rayon Hunte has been working with Angular and TypeScript for more than 3 years. He has built complex web applications, such as a vehicle management system and a land management web application for the local government. TypeScript has enabled Rayon to leverage his knowledge of JavaScript and web frameworks to build complex, scalable web applications. Having been a development team lead, Rayon has firsthand knowledge of how large projects can become too complicated and impossible to modify and scale as time passes and more features are added. He realizes that adding strong typing to your projects is essential in modern web development and for him, TypeScript has been a real game-changer.

Matt Morgan has been a software engineer, architect, and technology leader for more than 20 years. He's worked with many technologies over the years, such as RDBMS, Java, and Node.js, and seen many generations of web frameworks rise and fall. He is an occasional OSS contributor and a frequent blogger. Matt is most interested in finding ways to improve workflows and developer experience. A great toolchain is a force multiplier.

Wekoslav Stefanovski has about two decades of professional developer experience using a variety of development technologies. He has been using JavaScript since the previous millennium and has a long and fruitful love/hate relationship with it. On the other hand, with TypeScript it was love at first compilation, and it's only gotten better since then. His passions include building better programs and building better programmers.

Who This Book Is For

The TypeScript Workshop is designed for software developers who want to broaden their skill set by learning TypeScript. To get the most out of this book, you should have a basic knowledge of JavaScript or experience of using another similar programming language.

About the Chapters

Chapter 1, TypeScript Fundamentals, equips you with TypeScript fundamentals. You'll first learn how to set up your compiler options. Then, you'll perform various exercises on TypeScript types and objects.

Chapter 2, Declaration Files, gets you started with declaration files in TypeScript. You'll learn how to create declaration files from scratch and implement common development patterns for creating declaration files.

Chapter 3, Functions, dives deep into TypeScript functions. The chapter begins by introducing basic functions in TypeScript and then progresses to teach you advanced topics, such as type inference, currying, and the use of import, export, and the require syntax.

Chapter 4, Classes and Objects, teaches you how to define classes and instantiate them to create objects. You'll learn how to create classes that take in multiple objects as arguments to build dynamic behavior and confidently use TypeScript to generate HTML code.

Chapter 5, Interfaces and Inheritance, shows how you can use the power of interfaces and inheritance in TypeScript to write better, more maintainable code with well-structured functions, classes, and objects, and also be able to reuse your existing code efficiently.

Chapter 6, Advanced Types, teaches you how to use type literal and type alias. The chapter also discusses the fundamentals of how you can implement complex types such as intersection and union types.

Chapter 7, Decorators, first establishes the motivation for decorators and then teaches you how you can use them to add complex logic to your code without getting your application logic cluttered up.

Chapter 8, Dependency Injection in TypeScript, introduces dependency injection (DI) in TypeScript. The chapter begins with some fundamentals of design patterns in TypeScript and teaches you how to use the DI design pattern in a simple application.

Chapter 9, Generics and Conditional Types, describes the fundamentals of generics and conditional types in TypeScript. The chapter then describes how you can use generics to make your code more type-safe and avoid errors at runtime.

Chapter 10, Event Loop and Asynchronous Behavior, first establishes the motivation for events loops and asynchronous behavior, and then through several exercises teaches you how you use the asynchronous approach in TypeScript.

Chapter 11, Higher-Order Functions and Callbacks, begins with the fundamentals of higher-order functions and callbacks in TypeScript and then teaches you how you can implement them in TypeScript through several exercises and examples.

Chapter 12, Guide to Promises in TypeScript, first establishes the motivation for using Promises and then teaches you how you can implement them in TypeScript.

Chapter 13, Async/Await in TypeScript, covers common uses of async/await and discusses the landscape of asynchronous programming in TypeScript.

Chapter 14, TypeScript and React, covers the React library and how to build enhanced user interfaces with TypeScript. You will bootstrap React applications using the Create React App command-line interface.

Conventions

Code words in the text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "This code invokes myFunction with an argument of 'world' and assigns the result of the function call to a new constant message (const message)."

A block of code is set as follows:

const message = myFunction('world');
console.log(message);
// Hello world!

Before You Begin

Please make sure you have followed the instructions given below regarding the installation of the required compilers and code editors before you begin executing the code provided in the book.

Hardware Requirements

For an optimal experience, we recommend the following hardware configuration:

  • Processor: Intel Core i5 or equivalent
  • Memory: 4 GB RAM
  • Storage: 35 GB available space

Software Requirements

You'll also need the following software installed in advance:

  • OS: Windows 7 SP1 64-bit, Windows 8.1 64-bit, or Windows 10 64-bit, Ubuntu Linux, or the latest version of macOS
  • Browser: The latest version of either Google Chrome or Mozilla Firefox

Installation and Setup

VS Code

This book uses VS Code as the IDE to save and run TypeScript and JavaScript files. You can download VS Code from the following website: https://code.visualstudio.com/download. Scroll to the bottom of the page and click on the download button relevant to your system. Follow the instructions displayed on your screen.

Node.js

You need to install the latest version of Node.js, which includes npm. You can download and install Node.js from https://nodejs.org/en/download/. Click and download the installer relevant to your system.

TypeScript

This book uses TypeScript version 4.1.3. Once you have VS Code and Node.js installed in your system, you can install TypeScript by opening a terminal and running the following command:

npm install -g [email protected]

The preceding command will install version 4.1.3 globally. There are several other libraries and dependencies that you might have to install as part of executing the code given in the exercises and activities in this book. However, instructions for doing so have been provided in the relevant chapter/section.

Installing the Code Bundle

Download the code files from GitHub at https://github.com/PacktWorkshops/The-TypeScript-Workshop. The files here contain the code for the exercises and activities for each chapter. This can be a useful reference as you go through the book. You can either download the code files in the .zip format or clone the entire repository to your desktop using GitHub Desktop.

Get in Touch

Feedback from our readers is always welcome.

General feedback: If you have any questions about this book, please mention the book title in the subject of your message and email us at [email protected].

Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you could report this to us. Please visit www.packtpub.com/support/errata and complete the form.

Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you could provide us with the location address or website name. Please contact us at [email protected] with a link to the material.

If you are interested in becoming an author: If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.

Please Leave a Review

Let us know what you think by leaving a detailed, impartial review on Amazon. We appreciate all feedback – it helps us continue to make great products and help aspiring developers build their skills. Please spare a few minutes to give your thoughts – it makes a big difference to us. You can leave a review on Amazon via the following link: https://packt.link/r/1838828494.