Now in its third edition, this bestselling guide to TypeScript takes you through the nuts and bolts of the language. No frills, no fuss—just TypeScript essentials!
TypeScript enhances JavaScript with static typing, while keeping all the JS flexibility you know and love! It’s the perfect choice for any developer looking to improve the predictability and reliability of their code. Essential TypeScript 5, Third Edition teaches you how to get the most out of TypeScript 5 for a consistent, dependable development experience.
Inside Essential TypeScript 5, Third Edition you’ll learn how to
Configure the TypeScript development tools
Use type annotations
Create strongly typed functions and classes
Use generic types
Use type guards to determine types
Create and consume type declaration files
Use TypeScript to create web applications with Angular and React
The book starts you off with a proper understanding of the JavaScript type system that will make using TypeScript so much easier. On that solid foundation, you’ll build your understanding of TypeScript development, following a hands-on learning path all the way to TypeScript’s advanced features.
About the Technology
TypeScript is a popular superset of JavaScript that adds support for static typing. TypeScript’s typing features, which will be instantly familiar to C# or Java programmers, help you reduce errors and improve the overall quality of your JavaScript code.
About the Book
Essential TypeScript 5 is a fully updated third edition of the classic Adam Freeman bestseller. It provides full coverage of TypeScript 5, including new features like decorators. You’ll begin with the hows-and-whys of TypeScript, then quickly progress to practical applications of static types. No wasted pages! Each chapter is focused on the skills you need to write awesome web apps.
What's Inside
Configure your development tools
Create strongly typed functions and classes
Use generic types, type annotations, and type guards
Create and consume type declaration files
Author(s): Adam Freeman
Edition: 3
Publisher: Manning Publications Co.
Year: 2023
Language: English
Pages: 568
Essential TypeScript 5
Copyright
dedication
contents
front matter
preface
about this book
Who should read this book
How this book is organized: a roadmap
About the code
liveBook discussion forum
about the author
about the cover illustration
1 Understanding TypeScript
1.1 Should you use TypeScript?
1.1.1 Understanding the TypeScript developer productivity features
1.1.2 Understanding the JavaScript version features
1.2 What do you need to know?
1.3 How do you set up your development environment?
1.4 What Is the structure of this book?
1.5 Are there lots of examples?
1.6 Where can you get the example code?
1.7 What if you have problems following the examples?
1.7.1 What if you find an error in the book?
1.8 How do you contact the author?
1.9 What if you really enjoyed this book?
1.10 What if this book has made you angry?
Summary
Part 1.
2 Your first TypeScript application
2.1 Getting ready for this book
2.1.1 Step 1: Install Node.js
2.1.2 Step 2: Install Git
2.1.3 Step 3: Install TypeScript
2.1.4 Step 4: Install a programmer’s editor
2.2 Creating the project
2.2.1 Initializing the project
2.2.2 Creating the compiler configuration file
2.2.3 Adding a TypeScript code file
2.2.4 Compiling and executing the code
2.2.5 Defining the data model
2.2.6 Adding features to the collection class
2.3 Using a third-party package
2.3.1 Preparing for the third-party package
2.3.2 Installing and using the third-party package
2.3.3 Adding type declarations for the JavaScript package
2.4 Adding commands
2.4.1 Filtering items
2.4.2 Adding tasks
2.4.3 Marking tasks complete
2.5 Persistently storing data
Summary
3 JavaScript primer, part 1
3.1 Preparing for this chapter
3.2 Getting confused by JavaScript
3.3 Understanding JavaScript types
3.3.1 Working with primitive data types
3.3.2 Understanding type coercion
3.3.3 Working with functions
3.4 Working with arrays
3.4.1 Using the spread operator on arrays
3.4.2 Destructuring arrays
3.5 Working with objects
3.5.1 Adding, changing, and deleting object properties
3.5.2 Using the spread and rest operators on objects
3.5.3 Defining getters and setters
3.5.4 Defining methods
3.6 Understanding the this keyword
3.6.1 Understanding the this keyword in stand-alone functions
3.6.2 Understanding this in methods
3.6.3 Changing the behavior of the this keyword
3.6.4 Understanding this in arrow functions
3.6.5 Returning to the original problem
Summary
4 JavaScript primer, part 2
4.1 Preparing for this chapter
4.2 Understanding JavaScript object inheritance
4.2.1 Inspecting and modifying an object’s prototype
4.2.2 Creating custom prototypes
4.2.3 Using constructor functions
4.2.4 Chaining constructor functions
4.2.5 Checking prototype types
4.2.6 Defining static properties and methods
4.2.7 Using JavaScript classes
4.3 Using iterators and generators
4.3.1 Using a generator
4.3.2 Defining iterable objects
4.4 Using JavaScript collections
4.4.1 Storing data by key using an object
4.4.2 Storing data by key using a map
4.4.3 Using symbols for map keys
4.4.4 Storing data by index
4.5 Using modules
4.5.1 Declaring the module type
4.5.2 Creating a JavaScript module
4.5.3 Using a JavaScript module
4.5.4 Exporting named features from a module
4.5.5 Defining multiple named features in a module
Summary
5 Using the TypeScript compiler
5.1 Preparing for this chapter
5.2 Understanding the project structure
5.3 Using the Node Package Manager
5.4 Understanding the compiler configuration file
5.5 Compiling TypeScript code
5.5.1 Understanding compiler errors
5.5.2 Using watch mode and executing the compiled code
5.6 Using the version targeting feature
5.7 Setting the library files for compilation
5.8 Selecting a module format
5.8.1 Specifying a module format
5.9 Useful compiler configuration settings
Summary
6 Testing and debugging TypeScript
6.1 Preparing for this chapter
6.2 Debugging TypeScript code
6.2.1 Preparing for debugging
6.2.2 Using Visual Studio Code for debugging
6.2.3 Using the integrated Node.js debugger
6.2.4 Using the remote Node.js debugging feature
6.3 Using the TypeScript linter
6.3.1 Disabling linting rules
6.4 Unit testing TypeScript
6.4.1 Configuring the test framework
6.4.2 Creating unit tests
6.4.3 Starting the test framework
Summary
Part 2.
7 Understanding static types
7.1 Preparing for this chapter
7.2 Understanding static types
7.2.1 Creating a static type with a type annotation
7.2.2 Using implicitly defined static types
7.2.3 Using the any type
7.3 Using type unions
7.4 Using Type Assertions
7.4.1 Asserting to an unexpected type
7.5 Using a type guard
7.5.1 Understanding the never type
7.6 Using the unknown type
7.7 Using nullable types
7.7.1 Restricting nullable assignments
7.7.2 Removing null from a union with an assertion
7.7.3 Removing null from a union with a type guard
7.7.4 Using the definite assignment assertion
Summary
8 Using functions
8.1 Preparing for this chapter
8.2 Defining functions
8.2.1 Redefining functions
8.2.2 Understanding function parameters
8.2.3 Understanding function results
8.2.4 Overloading function types
8.2.5 Understanding assert functions
Summary
9 Using arrays, tuples, and enums
9.1 Preparing for this chapter
9.2 Working with arrays
9.2.1 Using inferred typing for arrays
9.2.2 Avoiding problems with inferred array types
9.2.3 Avoiding problems with empty arrays
9.3 Working with tuples
9.3.1 Processing tuples
9.3.2 Using tuple types
9.3.3 Using tuples with optional elements
9.3.4 Defining tuples with rest elements
9.4 Using enums
9.4.1 Understanding how enums work
9.4.2 Using string enums
9.4.3 Understanding the limitations of enums
9.5 Using literal value types
9.5.1 Using literal value types in functions
9.5.2 Mixing value types in a literal value type
9.5.3 Using overrides with literal value types
9.5.4 Using template literal string types
9.6 Using type aliases
Summary
10 Working with objects
10.1 Preparing for this chapter
10.2 Working with objects
10.2.1 Using object shape type annotations
10.2.2 Understanding how shape types fit
10.2.3 Using type aliases for shape types
10.2.4 Using shape type unions
10.2.5 Understanding union property types
10.2.6 Using type guards for objects
10.3 Using type intersections
10.3.1 Using intersections for data correlation
10.3.2 Understanding intersection merging
Summary
11 Working with classes and interfaces
11.1 Preparing for this chapter
11.2 Using constructor functions
11.3 Using classes
11.3.1 Using the access control keywords
11.3.2 Using JavaScript private fields
11.3.3 Defining read-only properties
11.3.4 Simplifying class constructors
11.3.5 Defining Accessors
11.3.6 Using auto-accessors
11.3.7 Using class inheritance
11.3.8 Using an abstract class
11.4 Using interfaces
11.4.1 Implementing multiple interfaces
11.4.2 Extending interfaces
11.4.3 Defining optional interface properties and methods
11.4.4 Defining an abstract interface implementation
11.4.5 Type guarding an interface
11.5 Dynamically creating properties
11.5.1 Enabling index value checking
Summary
12 Using generic types
12.1 Preparing for this chapter
12.2 Understanding the problem solved by generic types
12.2.1 Adding support for another type
12.3 Creating generic classes
12.3.1 Understanding generic type arguments
12.3.2 Using different type arguments
12.3.3 Constraining generic type values
12.3.4 Defining multiple type parameters
12.3.5 Allowing the compiler to infer type arguments
12.3.6 Extending generic classes
12.3.7 Type guarding generic types
12.3.8 Defining a static method on a generic class
12.4 Defining generic interfaces
12.4.1 Extending generic interfaces
12.4.2 Implementing a generic interface
Summary
13 Advanced generic types
13.1 Preparing for this chapter
13.2 Using generic collections
13.3 Using generic iterators
13.3.1 Combining an iterable and an iterator
13.3.2 Creating an iterable class
13.4 Using index types
13.4.1 Using the index type query
13.4.2 Explicitly providing generic type parameters for index types
13.4.3 Using the indexed access operator
13.4.4 Using an index type for the collection class
13.5 Using type mapping
13.5.1 Changing mapping names and types
13.5.2 Using a generic type parameter with a mapped type
13.5.3 Changing property optionality and mutability
13.5.4 Using the basic built-in mappings
13.5.5 Combining transformations in a single mapping
13.5.6 Creating types with a type mapping
13.6 Using conditional types
13.6.1 Nesting conditional types
13.6.2 Using conditional types in generic classes
13.6.3 Using conditional types with type unions
13.6.4 Using conditional types in type mappings
13.6.5 Identifying properties of a specific type
13.6.6 Inferring additional types in conditions
Summary
14 Using decorators
14.1 Preparing for this chapter
14.2 Understanding decorators
14.2.1 Using decorator context data
14.2.2 Using specific types in a decorator
14.3 Using the other decorator types
14.3.1 Creating a class decorator
14.3.2 Creating a field decorator
14.3.3 Creating an accessor decorator
14.3.4 Creating an auto-accessor decorator
14.4 Passing an additional argument to a decorator
14.5 Applying multiple decorators
14.6 Using an initializer
14.7 Accumulating state data
Summary
15 Working with JavaScript
15.1 Preparing for this chapter
15.1.1 Adding TypeScript code to the example project
15.2 Working with JavaScript
15.2.1 Including JavaScript in the compilation process
15.2.2 Type-checking JavaScript code
15.3 Describing types used in JavaScript code
15.3.1 Using comments to describe types
15.3.2 Using type declaration files
15.3.3 Describing third-party JavaScript code
15.3.4 Using Definitely Typed declaration files
15.3.5 Using packages that include type declarations
15.4 Generating declaration files
Summary
Part 3.
16 Creating a stand-alone web app, part 1
16.1 Preparing for this chapter
16.2 Creating the toolchain
16.3 Adding a bundler
16.4 Adding a development web server
16.5 Creating the data model
16.5.1 Creating the data source
16.6 Rendering HTML content using the DOM API
16.6.1 Adding support for Bootstrap CSS styles
16.7 Using JSX to create HTML content
16.7.1 Understanding the JSX workflow
16.7.2 Configuring the compiler and the loader
16.7.3 Creating the factory function
16.7.4 Using the JSX class
16.7.5 Importing the factory function in the JSX class
16.8 Adding features to the application
16.8.1 Displaying a filtered list of products
16.8.2 Displaying content and handling updates
Summary
17 Creating a stand-alone web app, part 2
17.1 Preparing for this chapter
17.2 Adding a web service
17.2.1 Incorporating the data source into the application
17.3 Completing the application
17.3.1 Adding a header class
17.3..2 Adding an order details class
17.3.3 Adding a confirmation class
17.3.4 Completing the application
17.4 Deploying the application
17.4.1 Adding the production HTTP server package
17.4.2 Creating the persistent data file
17.4.3 Creating the server
17.4.4 Using relative URLs for data requests
17.4.5 Building the application
17.4.6 Testing the production build
17.5 Containerizing the application
17.5.1 Installing Docker
17.5.2 Preparing the application
17.5.3 Creating the Docker container
17.5.4 Running the application
Summary
18 Creating an Angular app, part 1
18.1 Preparing for this chapter
18.1.1 Configuring the web service
18.1.2 Configuring the Bootstrap CSS package
18.1.3 Starting the example application
18.2 Understanding TypeScript in Angular development
18.2.1 Understanding the TypeScript compiler configuration
18.3 Creating the data model
18.3.1 Creating the Data Source
18.3.2 Creating the data source implementation class
18.3.3 Configuring the data source
18.4 Displaying a filtered list of products
18.4.1 Displaying the category buttons
18.4.2 Creating the header display
18.4.3 Combining the components
18.5 Configuring the application
Summary
19 Creating an Angular app, part 2
19.1 Preparing for this chapter
19.2 Completing the example application features
19.2.1 Adding the summary component
19.2.2 Creating the routing configuration
19.3 Deploying the application
19.3.1 Adding the production HTTP server package
19.3.2 Creating the persistent data file
19.3.3 Creating the server
19.3.4 Using relative URLs for data requests
19.3.5 Building the application
19.3.6 Testing the production build
19.4 Containerizing the application
19.4.1 Preparing the application
19.4.2 Creating the Docker container
19.4.3 Running the application
Summary
20 Creating a React app
20.1 Preparing for this chapter
20.1.1 Configuring the web service
20.1.2 Installing the Bootstrap CSS package
20.1.3 Starting the example application
20.2 Understanding TypeScript in React development
20.3 Defining the entity types
20.4 Displaying a filtered list of products
20.4.1 Using a functional component and hooks
20.4.2 Displaying a list of categories and the header
20.4.3 Composing and testing the components
20.5 Creating the data store
20.5.1 Implementing the HTTP API clients
Summary
21 Creating a React app, part 2
21.1 Preparing for this chapter
21.2 Configuring URL routing
21.3 Completing the example application features
21.3.1 Adding the confirmation component
21.3.2 Consuming the orders web service
21.3.3 Completing the application
21.4 Deploying the application
21.4.1 Adding the production HTTP server package
21.4.2 Creating the persistent data file
21.4.3 Creating the server
21.4.4 Using relative URLs for data requests
21.4.5 Building the application
21.4.6 Testing the production build
21.5 Containerizing the application
21.5.1 Preparing the application
21.5.2 Creating the Docker container
21.5.3 Running the application
Summary
index