TypeScript 4 Design Patterns and Best Practices - Theofanis Despoudis - E-Book

TypeScript 4 Design Patterns and Best Practices E-Book

Theofanis Despoudis

0,0
26,39 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.

Mehr erfahren.
Beschreibung

Design patterns are critical armor for every developer to build maintainable apps. TypeScript 4 Design Patterns and Best Practices is a one-stop guide to help you learn design patterns and practices to develop scalable TypeScript applications. It will also serve as handy documentation for future maintainers.
This book takes a hands-on approach to help you get up and running with the implementation of TypeScript design patterns and associated methodologies for writing testable code. You'll start by exploring the practical aspects of TypeScript 4 and its new features. The book will then take you through the traditional gang of four (GOF) design patterns in their classic and alternative form and show you how to use them in real-world development projects. Once you've got to grips with traditional design patterns, you'll advance to learning about their functional programming and reactive programming counterparts and how to couple them to deliver better and more idiomatic TypeScript code.
By the end of this TypeScript book, you'll be able to efficiently recognize when and how to use the right design patterns in any practical use case and gain the confidence to work on scalable and maintainable TypeScript projects of any size.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 367

Veröffentlichungsjahr: 2021

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



TypeScript 4 Design Patterns and Best Practices

Discover effective techniques and design patterns for every programming task

Theo Despoudis

BIRMINGHAM—MUMBAI

TypeScript 4 Design Patterns and Best Practices

Copyright © 2021 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, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.

Packt Publishing has endeavored 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.

Technical Reviewer: Dmytro Shpakovskyi

Group Product Manager: Richa Tripathi

Publishing Product Manager: Ashish Tiwari

Senior Editor: Ruvika Rao

Content Development Editor: Vaishali Ramkumar

Technical Editor: Pradeep Sahu

Copy Editor: Safis Editing

Project Coordinator: Ajesh Devavaram

Proofreader: Safis Editing

Indexer: Rekha Nair

Production Designer: Joshua Misquitta

First published: August 2021

Production reference: 1120821

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham

B3 2PB, UK.

ISBN 978-1-80056-342-1

www.packt.com

To my wife, Georgie Vargemezi, for being my loving partner, a pillar of strength, wisdom, and kindness throughout our joint life journey together.

– Theo Despoudis

Contributors

About the author

Theo Despoudis lives in Ireland, where he works as a software engineer for WP Engine and as a part-time tech practitioner for Fixate. He is the co-author of The React Workshop and Advanced Go Programming in 7 Days, is a Dzone Core member, and maintains some open source projects on GitHub. Theo is available for conference talks, independent consulting, and corporate training service opportunities.

I would like to first and foremost thank my loving and patient wife, my mother-in-law and daughters for their ongoing support, patience, and encouragement throughout the long process of writing this book. Thanks also to the Packt team, my content editors, Vaishali Ramkumar and Ruvika Rao, my project coordinator, Ajesh Devavaram, and my technical reviewer, Dmytro, for the candid and constructive feedback they provided me throughout the development of this book.

About the reviewer

Dmytro Shpakovskyi has over a decade of experience in quality assurance and test automation. Skilled in end-to-end, load, and API test automation, he has spoken at multiple software testing conferences, is a Packt published author, and is certified by ISTQB.

During his career, Dmytro has built from scratch and maintained a number of test automation frameworks, managed distributed teams of quality assurance automation engineers, and helped engineers to convert to automated testing. You can often find Dmytro creating and contributing to open source testing frameworks, mentoring other QA engineers, or exploring new techniques for automated testing. He shares some of his experience at Stijit. In addition to that, Dmytro has authored a book, Modern Web Testing with TestCafe.

Table of Contents

Preface

Section 1: Getting Started with TypeScript 4

Chapter 1: Getting Started with Typescript 4

Technical requirements

Introducing TypeScript 4

Working with input and output8

Useful TypeScript 4 features9

Understanding TypeScript and JavaScript's relationship

How does JavaScript compare to TypeScript?11

Transitioning from JavaScript to TypeScript12

Design patterns in JavaScript13

Installing and using the code examples

Reviewing the libraries included in the code examples15

Understanding the tsconfig.json file16

Running the unit tests18

Using VSCode with TypeScript

Using VSCode for this book's code19

Inspecting types22

Refactoring with VSCode24

Introducing Unified Modeling Language (UML)

What is UML?26

Learning UML class diagrams27

Summary

Q & A

Further reading

Chapter 2: TypeScript Core Principles

Technical requirements

Working with advanced types

Using utility types36

Using advanced types and assertions40

OOP with TypeScript

Abstraction45

Inheritance46

Encapsulation47

Polymorphism48

Developing in the browser

Understanding the DOM49

Using TypeScript with webpack54

Using React55

Developing in the server

Understanding the server environment58

Using Express with TypeScript62

Introducing design patterns in TypeScript

Why design patterns exist65

Design patterns in TypeScript65

Summary

Q&A

Further reading

Section 2: Core Design Patterns and Concepts

Chapter 3: Creational Design Patterns

Technical requirements

Creational design patterns

Singleton pattern

When do we use the Singleton?74

UML class diagram75

Classic implementation75

Modern implementations78

Variants80

Testing81

Criticisms of the singleton82

Real-world examples83

Prototype pattern

When do we use the Prototype pattern?84

UML class diagram85

Classic implementation86

Testing88

Criticisms of the Prototype pattern89

Real-world examples89

Builder pattern

When do we use Builder?91

UML class diagram for Builder91

Classic implementation94

Testing96

Modern implementations98

Criticisms of Builder99

Real-world examples100

Factory method pattern

When do we use the Factory method?101

UML class diagram101

Classic implementation102

Alternative implementations105

Testing106

Real-world examples107

Abstract Factory pattern

When do we use the Abstract Factory?107

UML class diagram108

Classic implementation109

Testing111

Criticisms of Abstract Factory112

Real-world example112

Summary

Q&A

Further reading

Chapter 4: Structural Design Patterns

Technical requirements

Understanding structural design patterns

Adapter pattern

When to use Adapter117

UML class diagram118

Classic implementation119

Testing121

Criticisms of Adapter122

Real-world use cases122

Decorator pattern

When to use Decorator123

UML class diagram124

Classic implementation125

Modern variants126

Testing128

Criticisms of Decorator129

Real-world use cases129

Façade pattern

When to use Façade130

UML class diagram131

Classic implementation131

Testing132

Criticisms of Façade133

Real-world use cases133

Composite pattern

When to use Composite135

UML class diagram135

Classic implementation136

Testing138

Criticisms of Composite138

Real-world use cases138

Proxy pattern

When to use Proxy139

UML class diagram140

Classic implementation141

Modern variant142

Testing143

Criticisms of Proxy143

Real-world use cases143

Bridge pattern

When to use Bridge144

UML class diagram144

Classic implementation145

Testing148

Criticisms of Bridge148

Real-world use cases148

Flyweight pattern

When to use Flyweight149

UML class diagram150

Classic implementation151

Testing152

Criticisms of Flyweight152

Real-world use cases153

Summary

Q&A

Further reading

Chapter 5: Behavioral Design Patterns

Technical requirements

Behavioral design patterns

The Strategy pattern?

When to use the Strategy pattern157

UML class diagram158

Classic implementation158

Testing159

Criticism of this pattern160

Real-world use cases160

Chain of Responsibility

When to use Chain of Responsibility?161

UML class diagram162

Classic implementation163

Testing165

Criticisms of this pattern166

Real-world use case166

The Command pattern

When to use the Command pattern?167

UML class diagram168

Classic implementation169

Testing171

Criticism of this pattern171

Real-world use case172

The Iterator pattern

When to use the Iterator pattern?173

UML class diagram173

Classic implementation174

Testing176

Criticism of this pattern176

Real-world use case176

The Mediator pattern

When to use the Meditator pattern?178

UML class diagram179

Classic implementation180

Testing182

Criticisms of this pattern182

Real-world use cases183

The Observer pattern

When to use the Observer pattern?184

UML class diagram185

Classic implementation186

Testing188

Criticisms of this pattern188

Real-world use case189

The Memento pattern

When to use the Memento pattern?189

UML class diagram190

Classic implementation191

Testing193

Criticisms of this pattern193

Real-world use case193

The State pattern

When to use the State pattern?194

UML class diagram195

Classic implementation196

Testing198

Criticisms of this pattern199

Real-world use case199

The Template method pattern

When to use the Template method pattern?201

UML class diagram202

Classic implementation202

Testing204

Criticism of this pattern204

Real-world use case205

The Visitor pattern

When to use the Visitor pattern?206

UML class diagram207

Classic implementation207

Testing209

Criticisms of this pattern210

Real-world use case210

Summary

Q&A

Further reading

Section 3: Advanced Concepts and Best Practices

Chapter 6: Functional Programming with TypeScript

Technical requirements

Learning key concepts in functional programming

Pure functions217

Recursion219

Functions as first-class citizens221

Function composition223

Referential transparency225

Immutability226

Understanding functional lenses

Implementation of lenses230

Use cases of lenses233

Understanding transducers

Understanding monads

Summary

Q & A

Further reading

Chapter 7: Reactive Programming with TypeScript

Technical requirements

Learning Reactive programming concepts

The asynchronous propagation of changes

The pull pattern249

The push pattern252

The push-pull pattern254

Understanding Promises and Futures

Futures258

Learning observables

Getting started with ReactiveX observables261

Composable operators263

Cold versus hot observables265

Summary

Q & A

Further reading

Chapter 8: Developing Modern and Robust TypeScript Applications

Technical requirements

Combining patterns

Singleton271

Iterator272

Command274

Using utility types

Using domain-driven design

Understanding entities280

Understanding value objects280

Understanding domain events281

Applying the SOLID principles

Understanding the single-responsibility principle282

Understanding the open-closed principle284

Understanding the Liskov substitution principle286

Understanding the interface segregation principle289

Understanding the dependency inversion principle290

Is using SOLID a best practice?293

Summary

Q&A

Further reading

Chapter 9: Anti-Patterns and Workarounds

Technical requirements

Class overuse

Not using runtime assertions

Permissive or incorrect types

Using idiomatic code from other languages

From the Java language307

From the Go language310

Type inference gotchas

Summary

Q & A

Further reading

Other Books You May Enjoy

Section 1: Getting Started with TypeScript 4

This first part of the book introduces TypeScript version 4 and its association with JavaScript. We'll take a look at its modern features and how to write idiomatic TypeScript code. We'll show how to run the examples included in this book and how to use VSCode to develop apps with TypeScript and provide a brief introduction to Unified Modeling Language (UML) and how we utilize it in this book. We'll subsequently identify the essential OOP facilities that TypeScript offers and how to create abstractions through types. We'll end this part with an informative introduction to the design patterns and concepts that we will learn about in this book.

This section comprises the following chapters:

Chapter 1, Getting Started with TypeScript 4Chapter 2, TypeScript Core Principles