TypeScript 4 Design Patterns and Best Practices

Book description

A detailed and easy-to-follow guide to help you improve your TypeScript development skills and enable you to solve application design problems using modern practices

Key Features

  • Identify common gotchas and antipatterns when developing TypeScript applications and understand how to avoid them
  • Discover expert techniques and best practices in developing large-scale TypeScript applications
  • Explore advanced design patterns taken from functional programming and reactive programming

Book Description

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.

What you will learn

  • Understand the role of design patterns and their significance
  • Explore all significant design patterns within the context of TypeScript
  • Analyze, and develop classical design patterns in TypeScript
  • Find out how design patterns differ from design concepts
  • Understand how to put the principles of design patterns into practice
  • Discover additional patterns that stem from functional and reactive programming

Who this book is for

If you're a TypeScript developer looking to learn how to apply established design patterns to solve common programming problems instead of reinventing solutions, you'll find this book useful. You're not expected to have prior knowledge of design patterns. Basic TypeScript knowledge is all you need to get started with this book.

Table of contents

  1. TypeScript 4 Design Patterns and Best Practices
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Download the color images
    6. Conventions used
    7. Get in touch
    8. Share your thoughts
  6. Section 1: Getting Started with TypeScript 4
  7. Chapter 1: Getting Started with Typescript 4
    1. Technical requirements
    2. Introducing TypeScript 4
      1. Working with input and output
      2. Useful TypeScript 4 features
    3. Understanding TypeScript and JavaScript's relationship
      1. How does JavaScript compare to TypeScript?
      2. Transitioning from JavaScript to TypeScript
      3. Design patterns in JavaScript
    4. Installing and using the code examples
      1. Reviewing the libraries included in the code examples
      2. Understanding the tsconfig.json file
      3. Running the unit tests
    5. Using VSCode with TypeScript
      1. Using VSCode for this book's code
      2. Inspecting types
      3. Refactoring with VSCode
    6. Introducing Unified Modeling Language (UML)
      1. What is UML?
      2. Learning UML class diagrams
    7. Summary
    8. Q & A
    9. Further reading
  8. Chapter 2: TypeScript Core Principles
    1. Technical requirements
    2. Working with advanced types
      1. Using utility types
      2. Using advanced types and assertions
    3. OOP with TypeScript
      1. Abstraction
      2. Inheritance
      3. Encapsulation
      4. Polymorphism
    4. Developing in the browser
      1. Understanding the DOM
      2. Using TypeScript with webpack
      3. Using React
    5. Developing in the server
      1. Understanding the server environment
      2. Using Express with TypeScript
    6. Introducing design patterns in TypeScript
      1. Why design patterns exist
      2. Design patterns in TypeScript
    7. Summary
    8. Q&A
    9. Further reading
  9. Section 2: Core Design Patterns and Concepts
  10. Chapter 3: Creational Design Patterns
    1. Technical requirements
    2. Creational design patterns
    3. Singleton pattern
      1. When do we use the Singleton?
      2. UML class diagram
      3. Classic implementation
      4. Modern implementations
      5. Variants
      6. Testing
      7. Criticisms of the singleton
      8. Real-world examples
    4. Prototype pattern
      1. When do we use the Prototype pattern?
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of the Prototype pattern
      6. Real-world examples
    5. Builder pattern
      1. When do we use Builder?
      2. UML class diagram for Builder
      3. Classic implementation
      4. Testing
      5. Modern implementations
      6. Criticisms of Builder
      7. Real-world examples
    6. Factory method pattern
      1. When do we use the Factory method?
      2. UML class diagram
      3. Classic implementation
      4. Alternative implementations
      5. Testing
      6. Real-world examples
    7. Abstract Factory pattern
      1. When do we use the Abstract Factory?
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of Abstract Factory
      6. Real-world example
    8. Summary
    9. Q&A
    10. Further reading
  11. Chapter 4: Structural Design Patterns
    1. Technical requirements
    2. Understanding structural design patterns
    3. Adapter pattern
      1. When to use Adapter
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of Adapter
      6. Real-world use cases
    4. Decorator pattern
      1. When to use Decorator
      2. UML class diagram
      3. Classic implementation
      4. Modern variants
      5. Testing
      6. Criticisms of Decorator
      7. Real-world use cases
    5. Façade pattern
      1. When to use Façade
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of Façade
      6. Real-world use cases
    6. Composite pattern
      1. When to use Composite
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of Composite
      6. Real-world use cases
    7. Proxy pattern
      1. When to use Proxy
      2. UML class diagram
      3. Classic implementation
      4. Modern variant
      5. Testing
      6. Criticisms of Proxy
      7. Real-world use cases
    8. Bridge pattern
      1. When to use Bridge
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of Bridge
      6. Real-world use cases
    9. Flyweight pattern
      1. When to use Flyweight
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of Flyweight
      6. Real-world use cases
    10. Summary
    11. Q&A
    12. Further reading
  12. Chapter 5: Behavioral Design Patterns
    1. Technical requirements
    2. Behavioral design patterns
    3. The Strategy pattern?
      1. When to use the Strategy pattern
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticism of this pattern
      6. Real-world use cases
    4. Chain of Responsibility
      1. When to use Chain of Responsibility?
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of this pattern
      6. Real-world use case
    5. The Command pattern
      1. When to use the Command pattern?
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticism of this pattern
      6. Real-world use case
    6. The Iterator pattern
      1. When to use the Iterator pattern?
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticism of this pattern
      6. Real-world use case
    7. The Mediator pattern
      1. When to use the Meditator pattern?
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of this pattern
      6. Real-world use cases
    8. The Observer pattern
      1. When to use the Observer pattern?
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of this pattern
      6. Real-world use case
    9. The Memento pattern
      1. When to use the Memento pattern?
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of this pattern
      6. Real-world use case
    10. The State pattern
      1. When to use the State pattern?
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of this pattern
      6. Real-world use case
    11. The Template method pattern
      1. When to use the Template method pattern?
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticism of this pattern
      6. Real-world use case
    12. The Visitor pattern
      1. When to use the Visitor pattern?
      2. UML class diagram
      3. Classic implementation
      4. Testing
      5. Criticisms of this pattern
      6. Real-world use case
    13. Summary
    14. Q&A
    15. Further reading
  13. Section 3: Advanced Concepts and Best Practices
  14. Chapter 6: Functional Programming with TypeScript
    1. Technical requirements
    2. Learning key concepts in functional programming
      1. Pure functions
      2. Recursion
      3. Functions as first-class citizens
      4. Function composition
      5. Referential transparency
      6. Immutability
    3. Understanding functional lenses
      1. Implementation of lenses
      2. Use cases of lenses
    4. Understanding transducers
    5. Understanding monads
    6. Summary
    7. Q & A
    8. Further reading
  15. Chapter 7: Reactive Programming with TypeScript
    1. Technical requirements
    2. Learning Reactive programming concepts
    3. The asynchronous propagation of changes
      1. The pull pattern
      2. The push pattern
      3. The push-pull pattern
    4. Understanding Promises and Futures
      1. Futures
    5. Learning observables
      1. Getting started with ReactiveX observables
      2. Composable operators
      3. Cold versus hot observables
    6. Summary
    7. Q & A
    8. Further reading
  16. Chapter 8: Developing Modern and Robust TypeScript Applications
    1. Technical requirements
    2. Combining patterns
      1. Singleton
      2. Iterator
      3. Command
    3. Using utility types
    4. Using domain-driven design
      1. Understanding entities
      2. Understanding value objects
      3. Understanding domain events
    5. Applying the SOLID principles
      1. Understanding the single-responsibility principle
      2. Understanding the open-closed principle
      3. Understanding the Liskov substitution principle
      4. Understanding the interface segregation principle
      5. Understanding the dependency inversion principle
      6. Is using SOLID a best practice?
    6. Summary
    7. Q&A
    8. Further reading
  17. Chapter 9: Anti-Patterns and Workarounds
    1. Technical requirements
    2. Class overuse
    3. Not using runtime assertions
    4. Permissive or incorrect types
    5. Using idiomatic code from other languages
      1. From the Java language
      2. From the Go language
    6. Type inference gotchas
    7. Summary
    8. Q & A
    9. Further reading
    10. Why subscribe?
  18. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Share your thoughts

Product information

  • Title: TypeScript 4 Design Patterns and Best Practices
  • Author(s): Theo Despoudis
  • Release date: September 2021
  • Publisher(s): Packt Publishing
  • ISBN: 9781800563421