Effective TypeScript: 62 Specific Ways to Improve Your TypeScript

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

TypeScript is a typed superset of JavaScript with the potential to solve many of the headaches for which JavaScript is famous. But TypeScript has a learning curve of its own, and understanding how to use it effectively can take time. This book guides you through 62 specific ways to improve your use of TypeScript. Author Dan Vanderkam, a principal software engineer at Sidewalk Labs, shows you how to apply these ideas, following the format popularized by Effective C++ and Effective Java (both from Addison-Wesley). You’ll advance from a beginning or intermediate user familiar with the basics to an advanced user who knows how to use the language well. Effective TypeScript is divided into eight chapters: Getting to Know TypeScript TypeScript’s Type System Type Inference Type Design Working with any Types Declarations and @types Writing and Running Your Code Migrating to TypeScript

Author(s): Dan Vanderkam
Edition: 1
Publisher: O'Reilly Media
Year: 2019

Language: English
Pages: 266

Copyright
Table of Contents
Preface
Who This Book Is For
Why I Wrote This Book
How This Book Is Organized
Conventions in TypeScript Code Samples
Typographical Conventions Used in This Book
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Chapter 1. Getting to Know TypeScript
Item 1: Understand the Relationship Between TypeScript and JavaScript
Things to Remember
Item 2: Know Which TypeScript Options You’re Using
Things to Remember
Item 3: Understand That Code Generation Is Independent of Types
Code with Type Errors Can Produce Output
You Cannot Check TypeScript Types at Runtime
Type Operations Cannot Affect Runtime Values
Runtime Types May Not Be the Same as Declared Types
You Cannot Overload a Function Based on TypeScript Types
TypeScript Types Have No Effect on Runtime Performance
Things to Remember
Item 4: Get Comfortable with Structural Typing
Things to Remember
Item 5: Limit Use of the any Type
There’s No Type Safety with any Types
any Lets You Break Contracts
There Are No Language Services for any Types
any Types Mask Bugs When You Refactor Code
any Hides Your Type Design
any Undermines Confidence in the Type System
Things to Remember
Chapter 2. TypeScript’s Type System
Item 6: Use Your Editor to Interrogate and Explore the Type System
Things to Remember
Item 7: Think of Types as Sets of Values
Things to Remember
Item 8: Know How to Tell Whether a Symbol Is in the Type Space or Value Space
Things to Remember
Item 9: Prefer Type Declarations to Type Assertions
Things to Remember
Item 10: Avoid Object Wrapper Types (String, Number, Boolean, Symbol, BigInt)
Things to Remember
Item 11: Recognize the Limits of Excess Property Checking
Things to Remember
Item 12: Apply Types to Entire Function Expressions When Possible
Things to Remember
Item 13: Know the Differences Between type and interface
Things to Remember
Item 14: Use Type Operations and Generics to Avoid Repeating Yourself
Things to Remember
Item 15: Use Index Signatures for Dynamic Data
Things to Remember
Item 16: Prefer Arrays, Tuples, and ArrayLike to number Index Signatures
Things to Remember
Item 17: Use readonly to Avoid Errors Associated with Mutation
Things to Remember
Item 18: Use Mapped Types to Keep Values in Sync
Things to Remember
Chapter 3. Type Inference
Item 19: Avoid Cluttering Your Code with Inferable Types
Things to Remember
Item 20: Use Different Variables for Different Types
Things to Remember
Item 21: Understand Type Widening
Things to Remember
Item 22: Understand Type Narrowing
Things to Remember
Item 23: Create Objects All at Once
Things to Remember
Item 24: Be Consistent in Your Use of Aliases
Things to Remember
Item 25: Use async Functions Instead of Callbacks for Asynchronous Code
Things to Remember
Item 26: Understand How Context Is Used in Type Inference
Tuple Types
Objects
Callbacks
Things to Remember
Item 27: Use Functional Constructs and Libraries to Help Types Flow
Things to Remember
Chapter 4. Type Design
Item 28: Prefer Types That Always Represent Valid States
Things to Remember
Item 29: Be Liberal in What You Accept and Strict in What You Produce
Things to Remember
Item 30: Don’t Repeat Type Information in Documentation
Things to Remember
Item 31: Push Null Values to the Perimeter of Your Types
Things to Remember
Item 32: Prefer Unions of Interfaces to Interfaces of Unions
Things to Remember
Item 33: Prefer More Precise Alternatives to String Types
Things to Remember
Item 34: Prefer Incomplete Types to Inaccurate Types
Things to Remember
Item 35: Generate Types from APIs and Specs, Not Data
Things to Remember
Item 36: Name Types Using the Language of Your Problem Domain
Things to Remember
Item 37: Consider “Brands” for Nominal Typing
Things to Remember
Chapter 5. Working with any
Item 38: Use the Narrowest Possible Scope for any Types
Things to Remember
Item 39: Prefer More Precise Variants of any to Plain any
Things to Remember
Item 40: Hide Unsafe Type Assertions in Well-Typed Functions
Things to Remember
Item 41: Understand Evolving any
Things to Remember
Item 42: Use unknown Instead of any for Values with an Unknown Type
Things to Remember
Item 43: Prefer Type-Safe Approaches to Monkey Patching
Things to Remember
Item 44: Track Your Type Coverage to Prevent Regressions in Type Safety
Things to Remember
Chapter 6. Types Declarations and @types
Item 45: Put TypeScript and @types in devDependencies
Things to Remember
Item 46: Understand the Three Versions Involved in Type Declarations
Things to Remember
Item 47: Export All Types That Appear in Public APIs
Things to Remember
Item 48: Use TSDoc for API Comments
Things to Remember
Item 49: Provide a Type for this in Callbacks
Things to Remember
Item 50: Prefer Conditional Types to Overloaded Declarations
Things to Remember
Item 51: Mirror Types to Sever Dependencies
Things to Remember
Item 52: Be Aware of the Pitfalls of Testing Types
Things to Remember
Chapter 7. Writing and Running Your Code
Item 53: Prefer ECMAScript Features to TypeScript Features
Enums
Parameter Properties
Namespaces and Triple-Slash Imports
Decorators
Things to Remember
Item 54: Know How to Iterate Over Objects
Things to Remember
Item 55: Understand the DOM hierarchy
Things to Remember
Item 56: Don’t Rely on Private to Hide Information
Things to Remember
Item 57: Use Source Maps to Debug TypeScript
Things to Remember
Chapter 8. Migrating to TypeScript
Item 58: Write Modern JavaScript
Use ECMAScript Modules
Use Classes Instead of Prototypes
Use let/const Instead of var
Use for-of or Array Methods Instead of for(;;)
Prefer Arrow Functions Over Function Expressions
Use Compact Object Literals and Destructuring Assignment
Use Default Function Parameters
Use async/await Instead of Raw Promises or Callbacks
Don’t Put use strict in TypeScript
Things to Remember
Item 59: Use @ts-check and JSDoc to Experiment with TypeScript
Undeclared Globals
Unknown Libraries
DOM Issues
Inaccurate JSDoc
Things to Remember
Item 60: Use allowJs to Mix TypeScript and JavaScript
Things to Remember
Item 61: Convert Module by Module Up Your Dependency Graph
Undeclared Class Members
Values with Changing Types
Things to Remember
Item 62: Don’t Consider Migration Complete Until You Enable noImplicitAny
Things to Remember
Index
About the Author
Colophon