Effective TypeScript

Page 241

But as you transition to TypeScript, there’s not much value in keeping 'use strict' in your source code. By and large, the sanity checks that TypeScript provides are far stricter than those offered by strict mode. There is some value in having a 'use strict' in the JavaScript that tsc emits. If you set the alwaysStrict or strict compiler options, TypeScript will parse your code in strict mode and put a 'use strict' in the JavaScript output for you. In short, don’t write 'use strict' in your TypeScript. Use alwaysStrict instead. These are just a few of the many new JavaScript features that TypeScript lets you use. TC39, the body that governs JS standards, is very active, and new features are added year to year. The TypeScript team is currently committed to implementing any fea‐ ture that reaches stage 3 (out of 4) in the standardization process, so you don’t even have to wait for the ink to dry. Check out the TC39 GitHub repo3 for the latest. As of this writing, the pipeline and decorators proposals in particular have great potential to impact TypeScript.

Things to Remember • TypeScript lets you write modern JavaScript whatever your runtime environ‐ ment. Take advantage of this by using the language features it enables. In addi‐ tion to improving your codebase, this will help TypeScript understand your code. • Use TypeScript to learn language features like classes, destructuring, and async/ await. • Don’t bother with 'use strict': TypeScript is stricter. • Check the TC39 GitHub repo and TypeScript release notes to learn about all the latest language features.

Item 59: Use @ts-check and JSDoc to Experiment with TypeScript Before you begin the process of converting your source files from JavaScript to Type‐ Script (Item 60), you may want to experiment with type checking to get an initial read on the sorts of issues that will come up. TypeScript’s @ts-check directive lets you do exactly this. It directs the type checker to analyze a single file and report whatever issues it finds. You can think of it as an extremely loose version of type checking: looser even than TypeScript with noImplicitAny off (Item 2).

3 https://github.com/tc39/proposals

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

|

223


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