Effective TypeScript

Page 220

function foo(abc: ABC) { for (const [k, v] of Object.entries(abc)) { k // Type is string v // Type is any } }

While these types may be hard to work with, they are at least honest! You should also be aware of the possibility of prototype pollution. Even in the case of an object literal that you define, for-in can produce additional keys: > Object.prototype.z = 3; // Please don't do this! > const obj = {x: 1, y: 2}; > for (const k in obj) { console.log(k); } x y z

Hopefully this doesn’t happen in a nonadversarial environment (you should never add enumerable properties to Object.prototype), but it is another reason that for-in produces string keys even for object literals. If you want to iterate over the keys and values in an object, use either a keyof declara‐ tion (let k: keyof T) or Object.entries. The former is appropriate for constants or other situations where you know that the object won’t have additional keys and you want precise types. The latter is more generally appropriate, though the key and value types are more difficult to work with.

Things to Remember • Use let k: keyof T and a for-in loop to iterate objects when you know exactly what the keys will be. Be aware that any objects your function receives as parame‐ ters might have additional keys. • Use Object.entries to iterate over the keys and values of any object.

Item 55: Understand the DOM hierarchy Most of the items in this book are agnostic about where you run your TypeScript: in a web browser, on a server, on a phone. This one is different. If you’re not working in a browser, skip ahead! The DOM hierarchy is always present when you’re running JavaScript in a web browser. When you use document.getElementById to get an element or docu ment.createElement to create one, it’s always a particular kind of element, even if

202

|

Chapter 7: Writing and Running Your Code


Turn static files into dynamic content formats.

Create a flipbook

Articles inside

Item 62: Don’t Consider Migration Complete Until You Enable noImplicitAny

14min
pages 252-264

Item 61: Convert Module by Module Up Your Dependency Graph

6min
pages 247-251

Item 60: Use allowJs to Mix TypeScript and JavaScript

1min
page 246

Item 59: Use @ts-check and JSDoc to Experiment with TypeScript

7min
pages 241-245

Item 58: Write Modern JavaScript

8min
pages 234-240

Item 57: Use Source Maps to Debug TypeScript

5min
pages 228-233

Item 56: Don’t Rely on Private to Hide Information

4min
pages 225-227

Item 54: Know How to Iterate Over Objects

2min
pages 218-219

Item 53: Prefer ECMAScript Features to TypeScript Features

6min
pages 213-217

Item 55: Understand the DOM hierarchy

7min
pages 220-224

Item 52: Be Aware of the Pitfalls of Testing Types

7min
pages 207-212

Item 51: Mirror Types to Sever Dependencies

3min
pages 205-206

Item 50: Prefer Conditional Types to Overloaded Declarations

3min
pages 203-204

Item 49: Provide a Type for this in Callbacks

4min
pages 199-202

Item 48: Use TSDoc for API Comments

2min
pages 196-198

Item 45: Put TypeScript and @types in devDependencies

3min
pages 189-190

Item 46: Understand the Three Versions Involved in Type Declarations

7min
pages 191-194

Item 44: Track Your Type Coverage to Prevent Regressions in Type Safety

5min
pages 186-188

Item 42: Use unknown Instead of any for Values with an Unknown Type

6min
pages 180-183

Item 43: Prefer Type-Safe Approaches to Monkey Patching

2min
pages 184-185

Item 41: Understand Evolving any

4min
pages 177-179

Item 40: Hide Unsafe Type Assertions in Well-Typed Functions

3min
pages 175-176

Item 38: Use the Narrowest Possible Scope for any Types

2min
pages 171-172

Item 39: Prefer More Precise Variants of any to Plain any

2min
pages 173-174

Item 37: Consider “Brands” for Nominal Typing

6min
pages 167-170

Item 34: Prefer Incomplete Types to Inaccurate Types

6min
pages 156-159

Item 36: Name Types Using the Language of Your Problem Domain

2min
pages 165-166

Item 35: Generate Types from APIs and Specs, Not Data

7min
pages 160-164

Item 33: Prefer More Precise Alternatives to String Types

5min
pages 152-155

Item 32: Prefer Unions of Interfaces to Interfaces of Unions

3min
pages 149-151

Item 29: Be Liberal in What You Accept and Strict in What You Produce

4min
pages 140-142

Item 30: Don’t Repeat Type Information in Documentation

3min
pages 143-144

Item 31: Push Null Values to the Perimeter of Your Types

5min
pages 145-148

Item 26: Understand How Context Is Used in Type Inference

6min
pages 125-128

Item 28: Prefer Types That Always Represent Valid States

7min
pages 135-139

Item 27: Use Functional Constructs and Libraries to Help Types Flow

6min
pages 129-134

Item 25: Use async Functions Instead of Callbacks for Asynchronous Code

6min
pages 120-124

Item 20: Use Different Variables for Different Types

4min
pages 105-107

Item 18: Use Mapped Types to Keep Values in Sync

3min
pages 95-98

Item 21: Understand Type Widening

5min
pages 108-110

Item 24: Be Consistent in Your Use of Aliases

3min
pages 117-119

Item 17: Use readonly to Avoid Errors Associated with Mutation

8min
pages 89-94

Item 22: Understand Type Narrowing

4min
pages 111-113

Item 19: Avoid Cluttering Your Code with Inferable Types

7min
pages 99-104

Item 23: Create Objects All at Once

4min
pages 114-116

Item 13: Know the Differences Between type and interface

5min
pages 70-73

BigInt

4min
pages 61-63

Item 12: Apply Types to Entire Function Expressions When Possible

5min
pages 67-69

Item 16: Prefer Arrays, Tuples, and ArrayLike to number Index Signatures

6min
pages 85-88

Item 14: Use Type Operations and Generics to Avoid Repeating Yourself

11min
pages 74-81

Item 15: Use Index Signatures for Dynamic Data

4min
pages 82-84

Item 11: Recognize the Limits of Excess Property Checking

4min
pages 64-66

Item 9: Prefer Type Declarations to Type Assertions Item 10: Avoid Object Wrapper Types (String, Number, Boolean, Symbol,

5min
pages 58-60

Item 5: Limit Use of the any Type

5min
pages 38-42

Item 2: Know Which TypeScript Options You’re Using

4min
pages 25-27

Space

7min
pages 53-57

Item 1: Understand the Relationship Between TypeScript and JavaScript

9min
pages 19-24

Item 4: Get Comfortable with Structural Typing

6min
pages 34-37

Item 7: Think of Types as Sets of Values Item 8: Know How to Tell Whether a Symbol Is in the Type Space or Value

10min
pages 47-52

Item 6: Use Your Editor to Interrogate and Explore the Type System

4min
pages 43-46

Item 3: Understand That Code Generation Is Independent of Types

8min
pages 28-33
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.
Effective TypeScript by Johan Corrales - Issuu