This is the code sample repo for the Effective TypeScript: 83 Specific Ways to Improve Your TypeScript (O'Reilly, May 2024). The book is available through:
and others. You can get the latest updates on the book at effectivetypescript.com.
Still on the first edition? Check out the 1st-edition
branch.
Below you'll find a complete table of contents. Each item links to a page with its "Things to Remember" (a short summary of the item) and a listing of all its code samples. The code samples are accompanied by links to the TypeScript playground. They should produce the expected errors (and no others) and expected types.
Unless otherwise noted in a comment at the top of the code sample, the samples are run with TypeScript's strict
setting. These were produced and checked using literate-ts and TypeScript 5.4.5.
Chapter 1: Getting to Know TypeScript
any
TypeChapter 2: TypeScript's Type System
type
and interface
readonly
to Avoid Errors Associated with MutationChapter 3: Type Inference and Control Flow Analysis
Chapter 4: Type Design
null
or undefined
in Type AliasesChapter 5: Unsoundness and the any Type
any
Typesany
to Plain any
any
for Values with an Unknown TypeChapter 6: Generics and Type-Level Programming
Chapter 7: TypeScript Recipes
never
Types to Perform Exhaustiveness CheckingRecord
Types to Keep Values in Syncnever
Properties to Model Exclusive OrChapter 8: Type Declarations and @types
@types
in devDependencies
this
in Callbacks if It's Part of Their APIChapter 9: Writing and Running Your Code
Chapter 10: Modernization and Migration
@ts-check
and JSDoc to Experiment with TypeScriptallowJs
to Mix TypeScript and JavaScriptnoImplicitAny