Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Learn React with TypeScript 3
Learn React with TypeScript 3

Learn React with TypeScript 3: Beginner's guide to modern React web development with TypeScript 3

eBook
$27.98 $39.99
Paperback
$48.99
Subscription
Free Trial
Renews at $19.99p/m

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Table of content icon View table of contents Preview book icon Preview Book

Learn React with TypeScript 3

What is New in TypeScript 3

In its six years of existence, TypeScript has continued to move forward and mature nicely. Is TypeScript 3 a significant release for React developers? What exactly are the new features that we have to add to our toolkit in TypeScript 3? These questions will be answered in this chapter, starting with the tuple type and how it can now be successfully used with the rest and spread JavaScript syntax, which is very popular in the React community. We'll then move on to the new unknown type and how it can be used as an alternative to the any type. Further more, we'll break TypeScript projects up into smaller projects with the new project references in TypeScript. Finally, we'll go about defining default properties in a strongly-typed React component that has improved in TypeScript 3.

By the end of the chapter, we'll be ready to start learning...

Technical requirements

In this chapter, we will use the same technologies as in Chapter 1, TypeScript Basics:

  • TypeScript playground: This is a website at https://www.typescriptlang.org/play/, which allows us to play around with and understand the features in TypeScript without installing it.
  • Node.js and npm: TypeScript and React are dependent on these. You can install them from https://nodejs.org/en/download/. If you already have these installed, make sure npm is at least version 5.2.
  • TypeScript: This can be installed via npm, entering the following command in a terminal:
npm install -g typescript
  • It is important that we are using TypeScript 3 in this chapter. You can check your TypeScript version by using the following command in a terminal:
tsc -v

If you need to upgrade to the latest version, you can run the following command:

npm install -g typescript@latest
  • Visual...

Tuples

Tuples have had a few enhancements in TypeScript 3, so that they can be used with the popular rest and spread JavaScript syntax. Before we get into the specific enhancements, we'll go through what tuples are, along with what the rest and spread syntax is. A tuple is like an array but the number of elements are fixed. It's a simple way to structure data and use some type safety.

Let's have a play with tuples:

  1. In the TypeScript playground, let's enter the following example of a tuple variable:
let product: [string, number];

We've initialized a product variable to a tuple type with two elements. The first element is a string and the second a number.

  1. We can store a product name and its unit price in the product variable on the next line, as follows:
product = ["Table", 500];
  1. Let's try to store the product name and unit price the...

The unknown type

unknown is a new type that has been added in TypeScript 3. Before TypeScript 3, we may have used the any type when we weren't sure of all the properties and methods in an object from a third-party library. However, when we declare a variable with the any type, the TypeScript compiler won't do any type checking on it. The unknown type can be used in these situations to make our code more type-safe. This is because unknown types are type-checked. So, unknown can often be used as an alternative to any.

In the TypeScript playground, let's go through an example of a function using any and an improved version using unknown:

  1. First, let's create a logScores function that takes in a parameter of type any. It logs out the name and scores properties from the argument to the console:
function logScores(scores: any) {
console.log(scores.firstName);
...

Project references

TypeScript 3 allows TypeScript projects to depend on other TypeScript projects by allowing tsconfig.json to reference other tsconfig.json files.

This makes it easier to split our code up into smaller projects. Our frontend code might be in TypeScript, in addition to having our backend in TypeScript. With TypeScript 3, we can have a frontend TypeScript project, a backend TypeScript project, and a shared TypeScript project that contains code that is used in both the frontend and backend. Splitting our code up into smaller projects can also can give us faster builds, because they can work incrementally.

Setting up an example

In order to explore this, we are going to work through an example of a TypeScript project...

Default JSX properties

TypeScript 3 has also improved how we can set default properties on React components with --strictNullChecks. Before TypeScript 3, we had to set properties that had default values as optional and perform null checks when referencing them. We haven't introduced React yet in this book, so we'll only touch on this briefly at this point.

Let's look through an example to get a feel for the improvement:

  1. The following is a React component with some default properties in TypeScript 2.9. The component is called SplitText and it takes in some text, splits it, and renders the bits that have been split in a list:
interface IProps {
text: string;
delimiter?: string;
}

class SplitText extends Component<IProps> {
static defaultProps = {
delimiter: ","
};
render() {
const bits = this.props.text.split(this.props.delimiter!);
...

Summary

Using the rest and spread syntax is very common nowadays, particularly when building React apps. We've seen how TypeScript 3, with the enhancement of tuples, allows us to use rest and spread in a strongly-typed fashion.

We've also seen how we can use the unknown type to reduce our use of the any type. The unknown type does require us to write more code, but it also allows us to create a more strongly-typed, more maintainable code base.

TypeScript has always made working with large code bases easier. With the introduction of project references, we can now split our solution into smaller projects more easily. This approach makes large solutions even more maintainable and flexible, and also yields faster build times with the new --build flag.

We briefly went through how using defaultprops in a React component has improved. We'll be using this frequently as...

Questions

In order to cement what we have learned about TypeScript 3, have a go at the following questions:

  1. We have the following function, which draws a point:
function drawPoint(x: number, y: number, z: number) {
...
}

We also have the following point variable:

const point: [number, number, number] = [100, 200, 300];

How can we call the drawPoint function in a terse manner?

  1. We need to create another version of the drawPoint function, where we can call it by passing the x, y, and z point values as parameters:
drawPoint(1, 2, 3);

Internally, in the implementation of drawPoint, we draw the point from a tuple type [number, number, number]. How can we define the method parameter(s) with the required tuple?

  1. In your implementation of drawPoint, how can you make z in the point optional?
  1. We have a function called getData, which calls a web API to get some data. The number...

Further reading

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Learn the latest and core features of React such as components, React Router, and suspense
  • Dive into TypeScript 3 and it`s core components such as interfaces, types aliases, tuples, generics and much more.
  • Build small-to-large scale single page applications with React, Redux, GraphQL and TypeScript

Description

React today is one of the most preferred choices for frontend development. Using React with TypeScript enhances development experience and offers a powerful combination to develop high performing web apps. In this book, you’ll learn how to create well structured and reusable react components that are easy to read and maintain by leveraging modern web development techniques. We will start with learning core TypeScript programming concepts before moving on to building reusable React components. You'll learn how to ensure all your components are type-safe by leveraging TypeScript's capabilities, including the latest on Project references, Tuples in rest parameters, and much more. You'll then be introduced to core features of React such as React Router, managing state with Redux and applying logic in lifecycle methods. Further on, you'll discover the latest features of React such as hooks and suspense which will enable you to create powerful function-based components. You'll get to grips with GraphQL web API using Apollo client to make your app more interactive. Finally, you'll learn how to write robust unit tests for React components using Jest. By the end of the book, you'll be well versed with all you need to develop fully featured web apps with React and TypeScript.

Who is this book for?

The ideal target audience for this book are web developers who want to get started with creating modern day web apps with React and TypeScript.You are expected to have a basic understanding of JavaScript and HTML programming. No prior knowledge of TypeScript and React is needed.

What you will learn

  • Gain a first-hand experience of TypeScript and its productivity features
  • Transpile your TypeScript code into JavaScript for it to run in a browser
  • Learn relevant advanced types in TypeScript for creating strongly typed and reusable components.
  • Create stateful function-based components that handle lifecycle events using hooks
  • Get to know what GraphQL is and how to work with it by executing basic queries to get familiar with the syntax
  • Become confident in getting good unit testing coverage on your components using Jest
Estimated delivery fee Deliver to Egypt

Standard delivery 10 - 13 business days

$12.95

Premium delivery 3 - 6 business days

$34.95
(Includes tracking information)

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Nov 29, 2018
Length: 502 pages
Edition : 1st
Language : English
ISBN-13 : 9781789610253
Vendor :
Microsoft
Languages :
Tools :

What do you get with Print?

Product feature icon Instant access to your digital eBook copy whilst your Print order is Shipped
Product feature icon Paperback book shipped to your preferred address
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Estimated delivery fee Deliver to Egypt

Standard delivery 10 - 13 business days

$12.95

Premium delivery 3 - 6 business days

$34.95
(Includes tracking information)

Product Details

Publication date : Nov 29, 2018
Length: 502 pages
Edition : 1st
Language : English
ISBN-13 : 9781789610253
Vendor :
Microsoft
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
$19.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
$199.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts
$279.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just $5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total $ 152.97
React Cookbook
$48.99
Learn React with TypeScript 3
$48.99
Software Architect’s Handbook
$54.99
Total $ 152.97 Stars icon

Table of Contents

13 Chapters
TypeScript Basics Chevron down icon Chevron up icon
What is New in TypeScript 3 Chevron down icon Chevron up icon
Getting Started with React and TypeScript Chevron down icon Chevron up icon
Routing with React Router Chevron down icon Chevron up icon
Advanced Types Chevron down icon Chevron up icon
Component Patterns Chevron down icon Chevron up icon
Working with Forms Chevron down icon Chevron up icon
React Redux Chevron down icon Chevron up icon
Interacting with RESTful APIs Chevron down icon Chevron up icon
Interacting with GraphQL APIs Chevron down icon Chevron up icon
Unit Testing with Jest Chevron down icon Chevron up icon
Answers Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.4
(11 Ratings)
5 star 45.5%
4 star 0%
3 star 18.2%
2 star 18.2%
1 star 18.2%
Filter icon Filter
Top Reviews

Filter reviews by




Just Some Guy Sep 02, 2022
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This is a great book - one of the best React books I've read, actually (and I've read A LOT of them). It's also a good book for learning TypeScript hands-on by way of real-world examples. To be clear, I would _highly_ recommend you learn ES6, React, and TypeScript on your own before approaching this book. But even so, the book does a respectable job of explaining both TS and React to even a newcomer (assuming you already know ES6/CSS/HTML5).The first few chapters focus on TypeScript itself. While it's certainly doesn't explain every detail of the language, it's a good intro that starts simple and will give you enough of a foundation to build on.From there it dives into React and various popular libraries (Router, Redux, Axios, GraphQL, etc.) - with most of the book taking a step-by-step approach to building a few simple demo apps (typical stuff). Each chapter includes abundant code samples that are clear and easy to follow. There are screenshots mixed in when appropriate, and the writing is totally straight forward and easy to digest from start to finish.This was published in 2018, and I was worried it would already be outdated (as I read it in 2022). I was happy to find that it still holds up pretty well. The code was built using React 16.7 - and is indeed primarily built around class-based React Components and the associated lifecycle methods (i.e. the old way). BUT the GOOD news is, ~1/3 of the sample code actually does use Function components, and there are several good examples built around the most important hooks (useState, useEffect, and useReducer).Yes, Class Components are dead – but odds are you'll run into some legacy TypeScript that uses them anyway, so this is still good stuff to know even today.My only complaint about this book is that it really doesn't delve into anything useful around fullstack development (nothing around mocking a Node server, using containers, deploying to any kind of server, etc.). That said, there are plenty of other books on those topics - so it's not the end of the world.Final word: This is a great book for anyone who wants to learn React TypeScript development - and it still holds up pretty well 5 years later. As long as you know what to expect, it's a great learning resource.
Amazon Verified review Amazon
J. Smith Jan 19, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book has you installing tools and coding almost from the first paragraph. You learn by reading then doing. The order of introduction is good - as is the pacing. Example code is simple and abundant. The scope is focused to just what you need to come up to speed. It's an easy read.The introduction to TypeScript coming from JavaScript is the best I have seen. It does not cover all the features of TypeScript. Instead, it gives you just enough to be comfortable and understand what TypeScript is for.This book does not teach you to program. It does not teach you web development. It is targeted at developers who have some background with JavaScript and browser development and want to come up to speed quickly on React and Typescript.
Amazon Verified review Amazon
M. Bürschgens Dec 07, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
When you are to learn modern SharePoint UI development you need a combination of TypeScript and React that is pretty hard to learn with the official tutorials if you don't know either of them. SharePoint tutorials require knowledge of TypeScript and React, TypeScript tutorials are only about TypeScript and React tutorials are React with pure JavaScript. This book is the fastest way to get the basic knowledge for SharePoint UI development so that you can continue with the SharePoint specific topics.The reviews mentioning that the book is already outdated are irrelevant for SharePoint since the package versions Microsoft recommends for SharePoint development are *even more* outdated.
Amazon Verified review Amazon
A. Le Dec 23, 2018
Full star icon Full star icon Full star icon Full star icon Full star icon 5
My book arrived sooner than I have expected. I am an experienced web developer but have not worked with React or Typescript before. I like how the author explains the technical aspects of react that I can easily understand. To get the most of this book, you must at least try to install react using npm or yarn on your own first, then this book will be the light bulb.
Amazon Verified review Amazon
Amazon Customer Feb 25, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Really good book which takes you to react through typescript. It teaches you both, typescript and react. It misses to give details of behind the scene working of react, but still, it is best you have in the market in my opinion. Just one stop for both.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is the delivery time and cost of print book? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela
What is custom duty/charge? Chevron down icon Chevron up icon

Customs duty are charges levied on goods when they cross international borders. It is a tax that is imposed on imported goods. These duties are charged by special authorities and bodies created by local governments and are meant to protect local industries, economies, and businesses.

Do I have to pay customs charges for the print book order? Chevron down icon Chevron up icon

The orders shipped to the countries that are listed under EU27 will not bear custom charges. They are paid by Packt as part of the order.

List of EU27 countries: www.gov.uk/eu-eea:

A custom duty or localized taxes may be applicable on the shipment and would be charged by the recipient country outside of the EU27 which should be paid by the customer and these duties are not included in the shipping charges been charged on the order.

How do I know my custom duty charges? Chevron down icon Chevron up icon

The amount of duty payable varies greatly depending on the imported goods, the country of origin and several other factors like the total invoice amount or dimensions like weight, and other such criteria applicable in your country.

For example:

  • If you live in Mexico, and the declared value of your ordered items is over $ 50, for you to receive a package, you will have to pay additional import tax of 19% which will be $ 9.50 to the courier service.
  • Whereas if you live in Turkey, and the declared value of your ordered items is over € 22, for you to receive a package, you will have to pay additional import tax of 18% which will be € 3.96 to the courier service.
How can I cancel my order? Chevron down icon Chevron up icon

Cancellation Policy for Published Printed Books:

You can cancel any order within 1 hour of placing the order. Simply contact customercare@packt.com with your order details or payment transaction id. If your order has already started the shipment process, we will do our best to stop it. However, if it is already on the way to you then when you receive it, you can contact us at customercare@packt.com using the returns and refund process.

Please understand that Packt Publishing cannot provide refunds or cancel any order except for the cases described in our Return Policy (i.e. Packt Publishing agrees to replace your printed book because it arrives damaged or material defect in book), Packt Publishing will not accept returns.

What is your returns and refunds policy? Chevron down icon Chevron up icon

Return Policy:

We want you to be happy with your purchase from Packtpub.com. We will not hassle you with returning print books to us. If the print book you receive from us is incorrect, damaged, doesn't work or is unacceptably late, please contact Customer Relations Team on customercare@packt.com with the order number and issue details as explained below:

  1. If you ordered (eBook, Video or Print Book) incorrectly or accidentally, please contact Customer Relations Team on customercare@packt.com within one hour of placing the order and we will replace/refund you the item cost.
  2. Sadly, if your eBook or Video file is faulty or a fault occurs during the eBook or Video being made available to you, i.e. during download then you should contact Customer Relations Team within 14 days of purchase on customercare@packt.com who will be able to resolve this issue for you.
  3. You will have a choice of replacement or refund of the problem items.(damaged, defective or incorrect)
  4. Once Customer Care Team confirms that you will be refunded, you should receive the refund within 10 to 12 working days.
  5. If you are only requesting a refund of one book from a multiple order, then we will refund you the appropriate single item.
  6. Where the items were shipped under a free shipping offer, there will be no shipping costs to refund.

On the off chance your printed book arrives damaged, with book material defect, contact our Customer Relation Team on customercare@packt.com within 14 days of receipt of the book with appropriate evidence of damage and we will work with you to secure a replacement copy, if necessary. Please note that each printed book you order from us is individually made by Packt's professional book-printing partner which is on a print-on-demand basis.

What tax is charged? Chevron down icon Chevron up icon

Currently, no tax is charged on the purchase of any print book (subject to change based on the laws and regulations). A localized VAT fee is charged only to our European and UK customers on eBooks, Video and subscriptions that they buy. GST is charged to Indian customers for eBooks and video purchases.

What payment methods can I use? Chevron down icon Chevron up icon

You can pay with the following card types:

  1. Visa Debit
  2. Visa Credit
  3. MasterCard
  4. PayPal
What is the delivery time and cost of print books? Chevron down icon Chevron up icon

Shipping Details

USA:

'

Economy: Delivery to most addresses in the US within 10-15 business days

Premium: Trackable Delivery to most addresses in the US within 3-8 business days

UK:

Economy: Delivery to most addresses in the U.K. within 7-9 business days.
Shipments are not trackable

Premium: Trackable delivery to most addresses in the U.K. within 3-4 business days!
Add one extra business day for deliveries to Northern Ireland and Scottish Highlands and islands

EU:

Premium: Trackable delivery to most EU destinations within 4-9 business days.

Australia:

Economy: Can deliver to P. O. Boxes and private residences.
Trackable service with delivery to addresses in Australia only.
Delivery time ranges from 7-9 business days for VIC and 8-10 business days for Interstate metro
Delivery time is up to 15 business days for remote areas of WA, NT & QLD.

Premium: Delivery to addresses in Australia only
Trackable delivery to most P. O. Boxes and private residences in Australia within 4-5 days based on the distance to a destination following dispatch.

India:

Premium: Delivery to most Indian addresses within 5-6 business days

Rest of the World:

Premium: Countries in the American continent: Trackable delivery to most countries within 4-7 business days

Asia:

Premium: Delivery to most Asian addresses within 5-9 business days

Disclaimer:
All orders received before 5 PM U.K time would start printing from the next business day. So the estimated delivery times start from the next day as well. Orders received after 5 PM U.K time (in our internal systems) on a business day or anytime on the weekend will begin printing the second to next business day. For example, an order placed at 11 AM today will begin printing tomorrow, whereas an order placed at 9 PM tonight will begin printing the day after tomorrow.


Unfortunately, due to several restrictions, we are unable to ship to the following countries:

  1. Afghanistan
  2. American Samoa
  3. Belarus
  4. Brunei Darussalam
  5. Central African Republic
  6. The Democratic Republic of Congo
  7. Eritrea
  8. Guinea-bissau
  9. Iran
  10. Lebanon
  11. Libiya Arab Jamahriya
  12. Somalia
  13. Sudan
  14. Russian Federation
  15. Syrian Arab Republic
  16. Ukraine
  17. Venezuela