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 a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
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

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 a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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 included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.