TypeScript has conquered the world of JavaScript: it's one of the world's fastest growing and most popular languages across developer surveys, widely used in consumer and business companies alike, and frequently credited for helping massive web applications scale. But what is TypeScript? How does it work, why does it work, and how can we use it?
Learning TypeScript takes beginner to intermediate JavaScript programmers from knowing nothing about "types" or a "type system" to full mastery of the fundamentals of TypeScript. It's more than a means to find bugs and typos--it's a useful system for declaring the way our JavaScript should work and helping us stick to it.
You'll learn how TypeScript:
• interacts with JavaScript
• analyzes and understands code
• augments your existing development pattern
• helps you document your code
• works with IDEs to provide refactoring tools
• assists local development in refactoring code
• helps you develop more quickly with fewer bugs
Author(s): Josh Goldberg
Edition: 1
Publisher: O'Reilly Media
Year: 2022
Language: English
Commentary: Vector PDF
Pages: 318
City: Sebastopol, CA
Tags: Programming; JavaScript; Web Applications; TypeScript; Type Annotations
Cover
Copyright
Table of Contents
Preface
Who Should Read This Book
Why I Wrote This Book
Navigating This Book
Examples and Projects
Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Part I. Concepts
Chapter 1. From JavaScript to TypeScript
History of JavaScript
Vanilla JavaScript’s Pitfalls
Costly Freedom
Loose Documentation
Weaker Developer Tooling
TypeScript!
Getting Started in the TypeScript Playground
TypeScript in Action
Freedom Through Restriction
Precise Documentation
Stronger Developer Tooling
Compiling Syntax
Getting Started Locally
Running Locally
Editor Features
What TypeScript Is Not
A Remedy for Bad Code
Extensions to JavaScript (Mostly)
Slower Than JavaScript
Finished Evolving
Summary
Chapter 2. The Type System
What’s in a Type?
Type Systems
Kinds of Errors
Assignability
Understanding Assignability Errors
Type Annotations
Unnecessary Type Annotations
Type Shapes
Modules
Summary
Chapter 3. Unions and Literals
Union Types
Declaring Union Types
Union Properties
Narrowing
Assignment Narrowing
Conditional Checks
Typeof Checks
Literal Types
Literal Assignability
Strict Null Checking
The Billion-Dollar Mistake
Truthiness Narrowing
Variables Without Initial Values
Type Aliases
Type Aliases Are Not JavaScript
Combining Type Aliases
Summary
Chapter 4. Objects
Object Types
Declaring Object Types
Aliased Object Types
Structural Typing
Usage Checking
Excess Property Checking
Nested Object Types
Optional Properties
Unions of Object Types
Inferred Object-Type Unions
Explicit Object-Type Unions
Narrowing Object Types
Discriminated Unions
Intersection Types
Dangers of Intersection Types
Summary
Part II. Features
Chapter 5. Functions
Function Parameters
Required Parameters
Optional Parameters
Default Parameters
Rest Parameters
Return Types
Explicit Return Types
Function Types
Function Type Parentheses
Parameter Type Inferences
Function Type Aliases
More Return Types
Void Returns
Never Returns
Function Overloads
Call-Signature Compatibility
Summary
Chapter 6. Arrays
Array Types
Array and Function Types
Union-Type Arrays
Evolving Any Arrays
Multidimensional Arrays
Array Members
Caveat: Unsound Members
Spreads and Rests
Spreads
Spreading Rest Parameters
Tuples
Tuple Assignability
Tuple Inferences
Summary
Chapter 7. Interfaces
Type Aliases Versus Interfaces
Types of Properties
Optional Properties
Read-Only Properties
Functions and Methods
Call Signatures
Index Signatures
Nested Interfaces
Interface Extensions
Overridden Properties
Extending Multiple Interfaces
Interface Merging
Member Naming Conflicts
Summary
Chapter 8. Classes
Class Methods
Class Properties
Function Properties
Initialization Checking
Optional Properties
Read-Only Properties
Classes as Types
Classes and Interfaces
Implementing Multiple Interfaces
Extending a Class
Extension Assignability
Overridden Constructors
Overridden Methods
Overridden Properties
Abstract Classes
Member Visibility
Static Field Modifiers
Summary
Chapter 9. Type Modifiers
Top Types
any, Again
unknown
Type Predicates
Type Operators
keyof
typeof
Type Assertions
Asserting Caught Error Types
Non-Null Assertions
Type Assertion Caveats
Const Assertions
Literals to Primitives
Read-Only Objects
Summary
Chapter 10. Generics
Generic Functions
Explicit Generic Call Types
Multiple Function Type Parameters
Generic Interfaces
Inferred Generic Interface Types
Generic Classes
Explicit Generic Class Types
Extending Generic Classes
Implementing Generic Interfaces
Method Generics
Static Class Generics
Generic Type Aliases
Generic Discriminated Unions
Generic Modifiers
Generic Defaults
Constrained Generic Types
keyof and Constrained Type Parameters
Promises
Creating Promises
Async Functions
Using Generics Right
The Golden Rule of Generics
Generic Naming Conventions
Summary
Part III. Usage
Chapter 11. Declaration Files
Declaration Files
Declaring Runtime Values
Global Values
Global Interface Merging
Global Augmentations
Built-In Declarations
Library Declarations
DOM Declarations
Module Declarations
Wildcard Module Declarations
Package Types
declaration
Dependency Package Types
Exposing Package Types
DefinitelyTyped
Type Availability
Summary
Chapter 12. Using IDE Features
Navigating Code
Finding Definitions
Finding References
Finding Implementations
Writing Code
Completing Names
Automatic Import Updates
Code Actions
Working Effectively with Errors
Language Service Errors
Summary
Chapter 13. Configuration Options
tsc Options
Pretty Mode
Watch Mode
TSConfig Files
tsc --init
CLI Versus Configuration
File Inclusions
include
exclude
Alternative Extensions
JSX Syntax
resolveJsonModule
Emit
outDir
target
Emitting Declarations
Source Maps
noEmit
Type Checking
lib
skipLibCheck
Strict Mode
Modules
module
moduleResolution
Interoperability with CommonJS
isolatedModules
JavaScript
allowJs
checkJs
JSDoc Support
Configuration Extensions
extends
Configuration Bases
Project References
composite
references
Build Mode
Summary
Part IV. Extra Credit
Chapter 14. Syntax Extensions
Class Parameter Properties
Experimental Decorators
Enums
Automatic Numeric Values
String-Valued Enums
Const Enums
Namespaces
Namespace Exports
Nested Namespaces
Namespaces in Type Definitions
Prefer Modules Over Namespaces
Type-Only Imports and Exports
Summary
Chapter 15. Type Operations
Mapped Types
Mapped Types from Types
Changing Modifiers
Generic Mapped Types
Conditional Types
Generic Conditional Types
Type Distributivity
Inferred Types
Mapped Conditional Types
never
never and Intersections and Unions
never and Conditional Types
never and Mapped Types
Template Literal Types
Intrinsic String Manipulation Types
Template Literal Keys
Remapping Mapped Type Keys
Type Operations and Complexity
Summary
Glossary
Index
About the Author
Colophon