Preface to the First Edition (2019)
In the spring of 2016, I visited my old coworker Evan Martin at Google’s San Francisco office and asked him what he was excited about. I’d asked him this same question many times over the years because the answers were wide-ranging and unpredictable but always interesting: C++ build tools, Linux audio drivers, online crosswords, emacs scripts. This time, Evan was excited about TypeScript and Visual Studio Code.
I was surprised! I’d heard of TypeScript before, but I knew only that it was created by Microsoft and that I mistakenly believed it had something to do with .NET. As a lifelong Linux user, I couldn’t believe that Evan had hopped on team Microsoft.
Then Evan showed me VS Code and the TypeScript playground and I was instantly converted. Everything was so fast, and the code intelligence made it easy to build a mental model of the type system. After years of writing type annotations in JSDoc comments for the Closure Compiler, this felt like typed JavaScript that really worked. And Microsoft had built a cross-platform text editor on top of Chromium? Perhaps this was a language and toolchain worth learning.
I’d recently joined Sidewalk Labs and was writing our first JavaScript. The codebase was still small enough that Evan and I were able to convert it all to TypeScript over the next few days.
I’ve been hooked ever since. TypeScript is more than just a type system. It also brings a whole suite of language services which are fast and easy to use. The cumulative effect is that TypeScript doesn’t just make JavaScript development safer: it also makes it more fun!
Brooklyn, NY
October 2019
Acknowledgments to the First Edition
There are many people who helped make this book possible. Thanks to Evan Martin for introducing me to TypeScript and showing me how to think about it. To Douwe Osinga for connecting me with O’Reilly and being supportive of the project. To Brett Slatkin for advice on structure and for showing me that someone I knew could write an Effective book. To Scott Meyers for coming up with this format and for his “Effective Effective Books” blog post, which provided essential guidance.
To my reviewers, Rick Battagline, Ryan Cavanaugh, Boris Cherny, Yakov Fain, Jesse Hallett, and Jason Killian. To all my coworkers at Sidewalk who learned TypeScript with me over the years. To everyone at O’Reilly who helped make this book happen: Angela Rufino, Jennifer Pollock, Deborah Baker, Nick Adams, and Jasmine Kwityn. To the TypeScript NYC crew, Jason, Orta, and Kirill, and to all the speakers. Many items were inspired by talks at the Meetup, as described in the following list:
-
Item 3 was inspired by a blog post of Evan Martin’s that I found particularly enlightening as I was first learning TypeScript.
-
Item 7 was inspired by Anders’s talk about structural typing and
keyof
relationships at TSConf 2018, and by a talk of Jesse Hallett’s at the April 2019 TypeScript NYC Meetup. -
Both Basarat’s guide and helpful answers by DeeV and GPicazo on Stack Overflow were essential in writing Item 9.
-
Item 10 builds on similar advice in Item 4 of Effective JavaScript.
-
I was inspired to write Item 11 by mass confusion around this topic at the August 2019 TypeScript NYC Meetup.
-
Item 13 was greatly aided by several questions about
type
vs.interface
on Stack Overflow. Jesse Hallett suggested the formulation around extensibility. -
Jacob Baskin provided encouragement and early feedback on Item 15.
-
Item 18 was inspired by several code samples submitted to the r/typescript subreddit.
-
Item 24 is based on my own writing on Medium and a talk I gave at the October 2018 TypeScript NYC Meetup.
-
Item 29 is based on common advice in Haskell (“make illegal states unrepresentable”). The Air France 447 story is inspired by Jeff Wise’s incredible 2011 article in Popular Mechanics.
-
Item 30 is based on an issue I ran into with the Mapbox type declarations. Jason Killian suggested the phrasing in the title.
-
The advice about naming in Item 41 is common, but this particular formulation was inspired by Dan North’s short article in 97 Things Every Programmer Should Know (Kevlin, Henney, O’Reilly).
-
Item 64 was inspired by Jason Killian’s talk at the very first TypeScript NYC Meetup in September 2017.
-
Item 25 is based on the TypeScript 2.1 release notes. The term “evolving any” is not widely used outside the TypeScript compiler itself, but I find it useful to have a name for this unusual pattern.
-
Item 46 was inspired by a blog post of Jesse Hallett’s.
-
Item 47 was greatly aided by feedback from Titian Cernicova-Dragomir in TypeScript issue #33128.
-
Item 49 is based on York Yao’s work on the
type-coverage
tool. I wanted something like this and it existed! -
Item 66 is based on a talk I gave at the December 2017 TypeScript NYC Meetup.
-
Item 52 owes a debt of gratitude to David Sheldrick’s post on the Artsy blog on conditional types, which greatly demystified the topic for me.
-
Item 70 was inspired by a talk Steve Faulkner, aka southpolesteve, gave at the February 2019 Meetup.
-
Item 55 is based on my own writing on Medium and work on the typings-checker tool, which eventually got folded into dtslint.
-
Item 72 was inspired/reinforced by Kat Busch’s Medium post on the various types of enums in TypeScript, as well as Boris Cherny’s writings on this topic in Programming TypeScript (O’Reilly).
-
Item 60 was inspired by my own confusion and that of my coworkers on this topic. The definitive explanation is given by Anders on TypeScript PR #12253.
-
The MDN documentation was essential for writing Item 75.
-
Chapter 10 is based on my own experience migrating the aging dygraphs library.
I found many of the blog posts and talks that led to this book through the excellent r/typescript subreddit. I’m particularly grateful to developers who provided code samples there which were essential for understanding common issues in beginner TypeScript. Thanks to Marius Schulz for the TypeScript Weekly newsletter. While it’s only occasionally weekly, it’s always an excellent source of material and a great way to keep up with TypeScript. To Anders, Daniel, Ryan, and the whole TypeScript team at Microsoft for the talks and all the feedback on issues. Most of my issues were misunderstandings, but there is nothing quite so satisfying as filing a bug and immediately seeing Anders Hejlsberg himself fix it!
Finally, thanks to Alex for being so supportive during this project and so understanding of all the working vacations, mornings, evenings, and weekends I needed to complete it.
Get Effective TypeScript, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.