Essential TypeScript 4: From Beginner to Pro [2 ed.] 148427010X, 9781484270103

Learn the essentials and more of TypeScript, a popular superset of the JavaScript language that adds support for static

4,422 1,239 6MB

English Pages 581 Year 2021

Report DMCA / Copyright

DOWNLOAD FILE

Essential TypeScript 4: From Beginner to Pro [2 ed.]
 148427010X, 9781484270103

Table of contents :
Table of Contents
About the Author
About the Technical Reviewer
Part I: Getting Started with TypeScript
Chapter 1: Your First TypeScript Application
Getting Ready for This Book
Step 1: Install Node.js
Step 2: Install Git
Step 3: Install TypeScript
Step 4: Install a Programmer’s Editor
Creating the Project
Initializing the Project
Creating the Compiler Configuration File
Adding a TypeScript Code File
Compiling and Executing the Code
Defining the Data Model
Creating the Todo Item Collection Class
Checking the Basic Data Model Features
Adding Features to the Collection Class
Providing Access to To-Do Items
Removing Completed Tasks
Providing Item Counts
Using a Third-Party Package
Adding Type Declarations for the JavaScript Package
Adding Commands
Filtering Items
Adding Tasks
Marking Tasks Complete
Persistently Storing Data
Applying the Persistent Collection Class
Summary
Chapter 2: Understanding TypeScript
Should You Use TypeScript?
Understanding the TypeScript Developer Productivity Features
Understanding the Limitations of the Productivity Features
Understanding the JavaScript Version Features
Understanding the Limitations of the Version Features
What Do You Need to Know?
How Do You Set Up Your Development Environment?
What Is the Structure of This Book?
Are There Lots of Examples?
Where Can You Get the Example Code?
What If You Have Problems Following the Examples?
What If You Find an Error in the Book?
How Do You Contact the Author?
What If You Really Enjoyed This Book?
What If This Book Has Made You Angry and You Want to Complain?
Summary
Chapter 3: JavaScript Primer, Part 1
Preparing for This Chapter
Getting Confused by JavaScript
Understanding JavaScript Types
Working with Primitive Data Types
Understanding Type Coercion
Avoiding Unintentional Type Coercion
Appreciating the Value of Explicitly Applied Type Coercion
Understanding Nullish Coalescing
Working with Functions
Working with Function Results
Avoiding Argument Mismatch Problems
Using Arrow Functions
Working with Arrays
Using the Spread Operator on Arrays
Destructuring Arrays
Ignoring Elements When Destructuring an Array
Assigning Remaining Elements to an Array
Working with Objects
Adding, Changing, and Deleting Object Properties
Guarding Against Undefined Objects and Properties
Using the Spread and Rest Operators on Objects
Defining Getters and Setters
Defining Methods
Understanding the this Keyword
Understanding the this Keyword in Stand-Alone Functions
Understanding this in Methods
Changing the Behavior of the this Keyword
Understanding this in Arrow Functions
Returning to the Original Problem
Summary
Chapter 4: JavaScript Primer, Part 2
Preparing for This Chapter
Understanding JavaScript Object Inheritance
Inspecting and Modifying an Object’s Prototype
Creating Custom Prototypes
Using Constructor Functions
Chaining Constructor Functions
Checking Prototype Types
Defining Static Properties and Methods
Using JavaScript Classes
Using Inheritance in Classes
Defining Static Methods
Using Iterators and Generators
Using a Generator
Defining Iterable Objects
Using JavaScript Collections
Storing Data by Key Using an Object
Storing Data by Key Using a Map
Using Symbols for Map Keys
Storing Data by Index
Using Modules
Creating a JavaScript Module
Using a JavaScript Module
Exporting Named Features from a Module
Defining Multiple Named Features in a Module
Summary
Chapter 5: Using the TypeScript Compiler
Preparing for This Chapter
Understanding the Project Structure
Using the Node Package Manager
Understanding the TypeScript Compiler Configuration File
Compiling TypeScript Code
Understanding Compiler Errors
Using Watch Mode and Executing the Compiled Code
Automatically Executing Code After Compilation
Starting the Compiler Using NPM
Using the Version Targeting Feature
Setting the Library Files for Compilation
Selecting a Module Format
Useful Compiler Configuration Settings
Summary
Chapter 6: Testing and Debugging TypeScript
Preparing for This Chapter
Debugging TypeScript Code
Preparing for Debugging
Adding Breakpoints
Using Visual Studio Code for Debugging
Using the Integrated Node.js Debugger
Using the Remote Node.js Debugging Feature
Using the TypeScript Linter
Disabling Linting Rules
Unit Testing TypeScript
Configuring the Test Framework
Creating Unit Tests
Starting the Test Framework
Summary
Part II: Working with TypeScript
Chapter 7: Understanding Static Types
Preparing for This Chapter
Understanding Static Types
Creating a Static Type with a Type Annotation
Using Implicitly Defined Static Types
Using the any Type
Using Implicitly Defined Any Types
Disabling Implicit Any Types
Using Type Unions
Using Type Assertions
Asserting to an Unexpected Type
Using a Type Guard
Understanding the Never Type
Using the unknown Type
Using Nullable Types
Restricting Nullable Assignments
Removing null from a Union with an Assertion
Removing null from a Union with a Type Guard
Using the Definite Assignment Assertion
Summary
Chapter 8: Using Functions
Preparing for This Chapter
Defining Functions
Redefining Functions
Understanding Function Parameters
Using Optional Parameters
Using a Parameter with a Default Value
Using a Rest Parameter
Applying Type Annotations to Function Parameters
Controlling Null Parameter Values
Understanding Function Results
Disabling Implicit Returns
Using Type Annotations for Function Results
Defining Void Functions
Overloading Function Types
Understanding Assert Functions
Summary
Chapter 9: Using Arrays, Tuples, and Enums
Preparing for This Chapter
Working with Arrays
Using Inferred Typing for Arrays
Avoiding Problems with Inferred Array Types
Avoiding Problems with Empty Arrays
Understanding the never Array Type Pitfall
Working with Tuples
Processing Tuples
Using Tuple Types
Using Tuples with Optional Elements
Defining Tuples with Rest Elements
Using Enums
Understanding How Enums Work
Using Specific Enum Values
Using String Enums
Understanding the Limitations of Enums
Understanding the Value-Checking Limitation
Understanding the Type Guard Limitation
Using Constant Enums
Using Literal Value Types
Using Literal Value Types in Functions
Mixing Value Types in a Literal Value Type
Using Overrides with Literal Value Types
Using Template Literal String Types
Using Type Aliases
Summary
Chapter 10: Working with Objects
Preparing for This Chapter
Working with Objects
Using Object Shape Type Annotations
Understanding How Shape Types Fit
Using Optional Properties for Irregular Shapes
Including Methods in Shape Types
Enforcing Strict Checking for Methods
Using Type Aliases for Shape Types
Dealing with Excess Properties
Using Shape Type Unions
Understanding Union Property Types
Using Type Guards for Objects
Type Guarding by Checking Properties
Type Guarding with a Type Predicate Function
Using Type Intersections
Using Intersections for Data Correlation
Understanding Intersection Merging
Merging Properties with the Same Type
Merging Properties with Different Types
Merging Methods
Summary
Chapter 11: Working with Classes and Interfaces
Preparing for This Chapter
Using Constructor Functions
Using Classes
Using the Access Control Keywords
Using JavaScript Private Fields
Defining Read-Only Properties
Simplifying Class Constructors
Using Class Inheritance
Understanding Type Inference for Subclasses
Using an Abstract Class
Type Guarding an Abstract Class
Using Interfaces
Implementing Multiple Interfaces
Extending Interfaces
Defining Optional Interface Properties and Methods
Defining an Abstract Interface Implementation
Type Guarding an Interface
Dynamically Creating Properties
Enabling Index Value Checking
Summary
Chapter 12: Using Generic Types
Preparing for This Chapter
Understanding the Problem
Adding Support for Another Type
Creating Generic Classes
Understanding Generic Type Arguments
Using Different Type Arguments
Constraining Generic Type Values
Constraining Generic Types Using a Shape
Defining Multiple Type Parameters
Applying a Type Parameter to a Method
Allowing the Compiler to Infer Type Arguments
Extending Generic Classes
Adding Extra Features to the Existing Type Parameters
Fixing the Generic Type Parameter
Restricting the Generic Type Parameter
Type Guarding Generic Types
Defining a Static Method on a Generic Class
Defining Generic Interfaces
Extending Generic Interfaces
Implementing a Generic Interface
Passing on the Generic Type Parameter
Restricting or Fixing the Generic Type Parameter
Creating an Abstract Interface Implementation
Summary
Chapter 13: Advanced Generic Types
Preparing for This Chapter
Using Generic Collections
Using Generic Iterators
Combining an Iterable and an Iterator
Creating an Iterable Class
Using Index Types
Using the Index Type Query
Explicitly Providing Generic Type Parameters for Index Types
Using the Indexed Access Operator
Using an Index Type for the Collection Class
Using Type Mapping
Changing Mapping Names and Types
Using a Generic Type Parameter with a Mapped Type
Changing Property Optionality and Mutability
Using the Basic Built-in Mappings
Mapping Specific Properties
Combining Transformations in a Single Mapping
Creating Types with a Type Mapping
Using Conditional Types
Nesting Conditional Types
Using Conditional Types in Generic Classes
Using Conditional Types with Type Unions
Using the Built-in Distributive Conditional Types
Using Conditional Types in Type Mappings
Identifying Properties of a Specific Type
Inferring Additional Types in Conditions
Inferring Types of Functions
Summary
Chapter 14: Working with JavaScript
Preparing for This Chapter
Adding the TypeScript Code to the Example Project
Working with JavaScript
Including JavaScript in the Compilation Process
Type Checking JavaScript Code
Describing Types Used in JavaScript Code
Using Comments to Describe Types
Using Type Declaration Files
Describing Third-Party JavaScript Code
Using Definitely Typed Declaration Files
Using Packages That Include Type Declarations
Generating Declaration Files
Summary
Part III: Creating Web Applications
Chapter 15: Creating a Stand-Alone Web App, Part 1
Preparing for This Chapter
Creating the Toolchain
Adding a Bundler
Adding a Development Web Server
Creating the Data Model
Creating the Data Source
Rendering HTML Content Using the DOM API
Adding Support for Bootstrap CSS Styles
Using JSX to Create HTML Content
Understanding the JSX Workflow
Configuring the TypeScript Compiler and the Webpack Loader
Creating the Factory Function
Using the JSX Class
Importing the Factory Function in the JSX Class
Adding Features to the Application
Displaying a Filtered List of Products
Displaying Content and Handling Updates
Summary
Chapter 16: Creating a Stand-Alone Web App, Part 2
Preparing for This Chapter
Adding a Web Service
Incorporating the Data Source into the Application
Using Decorators
Using Decorator Metadata
Completing the Application
Adding a Header Class
Adding an Order Details Class
Adding a Confirmation Class
Completing the Application
Deploying the Application
Adding the Production HTTP Server Package
Creating the Persistent Data File
Creating the Server
Using Relative URLs for Data Requests
Building the Application
Testing the Production Build
Containerizing the Application
Installing Docker
Preparing the Application
Creating the Docker Container
Running the Application
Summary
Chapter 17: Creating an Angular App, Part 1
Preparing for This Chapter
Configuring the Web Service
Configuring the Bootstrap CSS Package
Starting the Example Application
Understanding TypeScript in Angular Development
Understanding the Angular TypeScript Compiler Configuration
Creating the Data Model
Creating the Data Source
Creating the Data Source Implementation Class
Configuring the Data Source
Displaying a Filtered List of Products
Displaying the Category Buttons
Creating the Header Display
Combining the Product, Category, and Header Components
Configuring the Application
Summary
Chapter 18: Creating an Angular App, Part 2
Preparing for This Chapter
Completing the Example Application Features
Adding the Summary Component
Creating the Routing Configuration
Deploying the Application
Adding the Production HTTP Server Package
Creating the Persistent Data File
Creating the Server
Using Relative URLs for Data Requests
Building the Application
Testing the Production Build
Containerizing the Application
Preparing the Application
Creating the Docker Container
Running the Application
Summary
Chapter 19: Creating a React App
Preparing for This Chapter
Configuring the Web Service
Installing the Bootstrap CSS Package
Starting the Example Application
Understanding TypeScript in React Development
Defining the Entity Types
Displaying a Filtered List of Products
Using a Functional Component and Hooks
Displaying a List of Categories and the Header
Composing and Testing the Components
Creating the Data Store
Creating the HTTP Request Class
Connecting the Data Store to the Components
Summary
Chapter 20: Creating a React App, Part 2
Preparing for This Chapter
Configuring URL Routing
Completing the Example Application Features
Adding the Order Summary Component
Adding the Confirmation Component
Completing the Routing Configuration
Deploying the Application
Adding the Production HTTP Server Package
Creating the Persistent Data File
Creating the Server
Using Relative URLs for Data Requests
Building the Application
Testing the Production Build
Containerizing the Application
Preparing the Application
Creating the Docker Container
Running the Application
Summary
Chapter 21: Creating a Vue.js App, Part 1
Preparing for This Chapter
Configuring the Web Service
Configuring the Bootstrap CSS Package
Starting the Example Application
Understanding TypeScript in Vue.js Development
Understanding the TypeScript Vue.js Toolchain
Creating the Entity Classes
Displaying a Filtered List of Products
Displaying a List of Categories and the Header
Composing and Testing the Components
Creating the Data Store
Connecting Components to the Data Store
Adding Support for the Web Service
Summary
Chapter 22: Creating a Vue.js App, Part 2
Preparing for This Chapter
Configuring URL Routing
Completing the Example Application Features
Adding the Order Summary Component
Adding the Confirmation Component
Completing the Routing Configuration
Deploying the Application
Adding the Production HTTP Server Package
Creating the Persistent Data File
Creating the Server
Using Relative URLs for Data Requests
Building the Application
Testing the Production Build
Containerizing the Application
Preparing the Application
Creating the Docker Container
Running the Application
Summary
Index

Polecaj historie