Effective TypeScript

Page 218

future. Unless you’re using Angular or another framework that requires annotations and until they’re standardized, don’t use TypeScript’s decorators.

Things to Remember • By and large, you can convert TypeScript to JavaScript by removing all the types from your code. • Enums, parameter properties, triple-slash imports, and decorators are historical exceptions to this rule. • In order to keep TypeScript’s role in your codebase as clear as possible, I recom‐ mend avoiding these features.

Item 54: Know How to Iterate Over Objects This code runs fine, and yet TypeScript flags an error in it. Why? const obj = { one: 'uno', two: 'dos', three: 'tres', }; for (const k in obj) { const v = obj[k]; // ~~~~~~ Element implicitly has an 'any' type // because type ... has no index signature }

Inspecting the obj and k symbols gives a clue: const obj = { /* ... */ }; // const obj: { // one: string; // two: string; // three: string; // } for (const k in obj) { // const k: string // ... }

The type of k is string, but you’re trying to index into an object whose type only has three specific keys: 'one', 'two', and 'three'. There are strings other than these three, so this has to fail. Plugging in a narrower type declaration for k fixes the issue: let k: keyof typeof obj; for (k in obj) {

200

// Type is "one" | "two" | "three"

| 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