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

Preface

This quick start guide is a book that covers every topic related to TypeScript. This book takes the reader through all the features and benefits of using TypeScript. With this guide, you will grow your typing skills, which will make your JavaScript knowledge in play. TypeScript is a superset of JavaScript. This book does not teach you how to do web programming but illustrates how to use TypeScript on top of a language that you already know and like. 

Who this book is for

This book is for developers who know JavaScript but have never touched Typescript.

What this book covers

Chapter 1, Getting Started with TypeScript, covers how to bring TypeScript into a new project or an existing JavaScript project. We will see how to set up your environment in your current build setup. TypeScript is supported by a variety of build tools, such as Grunt, Gulp, Webpack, or simply using the CLI. We also dive into the essential options, among all the ones that are available, to help you get started with TypeScript.

Chapter 2, Onboarding Types with Primitive, illustrates scoping a variable, the subtlety between undefined variables and null variables, and how to make a variable optional or required. At the end of this chapter, the reader will be in a situation where all the variables will be declared properly with an type that's accurate supported by TypeScript. The distinction between a primitive and non-primitive type will not be a conundrum anymore. The use of enum or symbol will be natural, and the creation of new types will become a habit any time a new domain object is introduced in your system.

Chapter 3, Unleashing the Power of Type with Objects, illustrates the difference between an object, Object, object literal and an object built with a constructor. This chapter also introduces the notion of a union between types that will allow an infinite combination of types for a single value. Furthermore, the concept of intersection is introduced, which allows us to manipulate type differently. At the end of this chapter, the reader will be able to create complex combinations of objects that hold advanced structures. We will dissect how to create a dictionary with a strongly typed index signature, understand how type can be beneficial with a map, and how to use the right object in order to be as accurate as possible when defining an object that can have a broad reach.

Chapter 4, Transforming Your Code in Object-Orientedexplains that object-oriented programming has its own set of terminology, and TypeScript relies on many of them. In this chapter, all the concepts of object-oriented programming that TypeScript supports are discussed with examples. We will see what a class is and how to instantiate a class into an object. In the same vein, we will see how a constructor can be strongly typed with TypeScript, but also how with a shorthand syntax we can assign a class's fields directly from the constructor. The principle of encapsulation with visibility, how to implement an interface, and how to bring abstraction to a class are all covered.

Chapter 5, Scoping Variables with Different Patterns,covers the subtlety the most basic concept, which is the variable. Knowing the exact type from a primitive to an object is essential for accessing specific members. Scoping down the exact type at runtime and design time is crucial for consistency between the two environments and to have feedback about what is possible or not. The variety of configuration among the different types of variable requires many different patterns that are covered in this chapter.

Chapter 6, Reusing Code through Generic, builds on ideas introduced in the previous chapters. The chapter builds these ideas by enhancing type and making them generic. Basic topics, such as defining a generic class and interface, are present. Through the chapter, we move into more advanced topics, such as generic constraints. The goal of this chapter is to make your code more generic to increase the reusability of your classes, functions, and structures to reduce the burden of duplicating code.

Chapter 7, Mastering the Art of Defining Types,covers how to create type from libraries that we are not working directly with but by importing the library inside your TypeScript project. The main difference is that when consuming code outside your project, you will not directly use TypeScript code but their definition. The reason is that JavaScript is provided in those libraries, not TypeScript code. We will see how it is possible to master the art of creating definition files for code that does not provide them, allowing you to keep working in a strong environment.

To get the most out of this book

This book is for JavaScript developers who want to get started with TypeScript for building applications. No previous knowledge of TypeScript is expected.

Download the example code files

You can download the example code files for this book from your account at www.packtpub.com. If you purchased this book elsewhere, you can visit www.packtpub.com/support and register to have the files emailed directly to you.

You can download the code files by following these steps:

  1. Log in or register at www.packtpub.com.
  2. Select the SUPPORT tab.
  3. Click on Code Downloads & Errata.
  4. Enter the name of the book in the Search box and follow the onscreen instructions.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR/7-Zip for Windows
  • Zipeg/iZip/UnRarX for Mac
  • 7-Zip/PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Typescript-3.0-Quick-Start-Guide. In case there's an update to the code, it will be updated on the existing GitHub repository.

We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Download the color images

We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/TypeScript3QuickStartGuide_ColorImages.pdf.

Conventions used

There are a number of text conventions used throughout this book.

CodeInText: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. Here is an example: "Mount the downloaded WebStorm-10*.dmg disk image file as another disk in your system."

A block of code is set as follows:

let a:number = 2;
a = "two"; // Doesn't compile

 

 

 

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

function switchFunction(num: number) {
let b: string = "functionb";

   switch (num) {
       case 1:
   let b: string = "case 1";
       break;
   }
}

Bold: Indicates a new term, an important word, or words that you see onscreen. For example, words in menus or dialog boxes appear in the text like this. Here is an example: "Select System info from the Administration panel."

Note

Warnings or important notes appear like this.

Note

Tips and tricks appear like this.

Get in touch

Feedback from our readers is always welcome.

General feedback: Email [email protected] and mention the book title in the subject of your message. If you have questions about any aspect of this book, please 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 would report this to us. Please visit www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.

Piracy: If you come across any illegal copies of our works in any form on the Internet, we would be grateful if you would 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.

Reviews

Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!

For more information about Packt, please visit packtpub.com.