When you're working with generic types and run into a seemingly unavoidable error, there's a trick that can often make it go away: intersect what you have with whatever TypeScript wants it to be. Your errors will melt away! Read on for examples and caveats.| effectivetypescript.com
This series will present a few tips and tricks for working with TypeScript generics that I've picked up over the past year. This post looks at how you can use classes and currying to combine explicit type parameters and type inference.| effectivetypescript.com
The New TypeScript Handbook has some real gems in it. Here's what it has to say about generics: Writing generic functions is fun, and it can be easy to get carried away with type parameters. Having too many type parameters or using constraints where they aren't needed can make inference less successful, frustrating callers of your function. It goes on to offer a few specific pieces of advice about how to use generics, including one that I've started to think of as the "Golden Rule of Generics...| effectivetypescript.com
A fundamental part of TypeScript's design is that TypeScript types are erased at runtime. There's no way to access them. But inevitably, you'll want to do just that. This comes up most often when you want to validate that user input matches a TypeScript type. Faced with this problem, TypeScript developers often reach for Zod, a schema validation tool. But Zod has some downsides, and it's not the only solution to this conundrum. This sample item explores this problem and three possible solutio...| effectivetypescript.com
We TypeScript developers are a lucky bunch. While some languages (Python, JavaScript) are released annually, every three years (C++) or even less, we get four new versions of TypeScript every year. TypeScript 5.6 was released on September 9th, 2024. Let's take a look.| Effective TypeScript
Effective TypeScript is nearly 400 pages long, but I've received the most feedback by far on just one passage. It comes in Item 7: Think of Types as Sets of Values: keyof (A&B) = (keyof A) | (keyof B) keyof (A|B) = (keyof A) & (keyof B) If you can build an intuition for why these equations hold, you'll have come a long way toward understanding TypeScript's type system! I'll explain these equations in a moment. But before I do, head over to the TypeScript Playground and test them out with a fe...| Effective TypeScript
What can Zig learn from TypeScript, and what can TypeScript learn from Zig?| effectivetypescript.com
We TypeScript developers are a lucky bunch. While some languages (Python, JavaScript) are released annually, every three years (C++) or even less, we get four new versions of TypeScript every year. TypeScript 5.5 was released on June 20th, 2024, and it was a real blockbuster. Let's take a look.| effectivetypescript.com
It's tempting to use "", 0 or -1 as special values: an empty string might represent text that hasn't loaded yet, or -1 could stand in for a missing number. In TypeScript, this is almost always a bad idea. Special values need to be handled specially, and giving them a distinct type, such as null, allows TypeScript to enforce that you do so.| effectivetypescript.com
Fully updated, thoroughly revised, now with 50% more book!| effectivetypescript.com
Over the past few months I became a TypeScript contributor and implemented a new feature, type predicate inference, that should be one of the headliners for TypeScript 5.5. This post tells the story of how that happened: why I wanted to contribute to TypeScript, the journey to implementing the feature and getting the PR merged, and what I've learned along the way. This is not a short read, but it will give you a good sense of what it's like to become a TypeScript contributor and develop a new...| effectivetypescript.com
If a variable gets a type but no one looks at it, does it really get a type at all? This post looks at how type inference is implemented in the TypeScript compiler. It's of some interest to anyone who uses TypeScript and is curious how it works, but it will be most relevant to developers who want to contribute to TypeScript itself.| effectivetypescript.com
Type guards are a powerful tool for improving TypeScript's built-in control flow analysis. This post looks at when it's appropriate to use a type predicate, and in particular what it means when a type predicate returns false.| effectivetypescript.com
Back in 2020 I gave a whole series of Effective TypeScript talks at companies that were interested in the language and the book. The talk that I gave at Etsy in December of 2020 was one of the most fun. It was recorded and is now available to watch. It's about an hour.| effectivetypescript.com
Getter and setter methods (getFoo, setFoo) are common in Java and considered a best practice. But they're a code smell that's best avoided in JavaScript and TypeScript because the problem they solve in Java does not exist in JS/TS. This post looks at what that problem is and how you can solve it in TypeScript without imposing the boilerplate of getters and setters.| effectivetypescript.com
TypeScript's infer keyword can infer quite a bit more than you might expect. It's extremely effective at extracting types from the sort of nested structures that you might get from codegen or an API specification.| effectivetypescript.com
I cut one item from Effective TypeScript during the final stages of editing. Four years later, it's time for it to see the light of day! It's a trick for specializing generic types for certain subtypes of their type parameters. This post shows how it works, why it's indispensible for wrapper types, and also explains why I cut it from the book.| effectivetypescript.com
It's Christmastime and I've been happily working through this year's Advent of Code in Deno (look forward to a blog post in the new year). What with all the presents, it's a good time to think about what we'd most like to see from TypeScript in the new year. Here are my top seven feature requests for 2023. Yes, that's a lot, but really I'd be thrilled with just one or two. Pretty please?| effectivetypescript.com
TL;DR: Use ts-prune is in maintenance mode. Use knip to find dead code instead. It's great!| effectivetypescript.com
This post looks at the Closure Compiler, Google's tool from the mid-2000s for adding types to JavaScript. It looks at how its focus on minification led to very different design choices than TypeScript, and how this and a few other factors led to TypeScript becoming the ubiquitous solution for JavaScript + types. The Closure Compiler represents an alternative path that JavaScript could have taken, and it gives us perspective on TypeScript as it exists today.| effectivetypescript.com
If you develop server code with TypeScript, you'll inevitably come up against the question of how to interact with your database. There's lots of type information in your database (the structure of the tables) and it's not immediately clear how to share that type information between the DB and TypeScript. This post and its accompanying video present six ways to solve this problem and offer some advice gleaned from years of experience combining Postgres and TypeScript.| effectivetypescript.com
Every three months we get a new TypeScript release and TypeScript 5.1 landed on June 1, 2023. This release has a few interesting new features, but by far the most noticeable changes are performance improvements and error message ergonomics. Let's take a look!| effectivetypescript.com
Chapter 4 of Effective TypeScript covers type design: the process of crafting your types to accurately model your domain. This item has always been a favorite of mine because of how immediately actionable it is. When you review code, be on the lookout for violations! What’s wrong with this code? /** * Returns a string with the foreground color. * Takes zero or one arguments. With no arguments, returns the * standard foreground color. With one argument, returns the foreground color * for a p...| effectivetypescript.com
Every year I do the Advent of Code in a different programming language. If you aren't familiar, it's an online coding competition with a new two-part problem every day from December 1st to the 25th. Thousands of programmers participate and share their solutions. It's a great way to learn a language and bond over coding. In 2019 I used Python, in 2020 I used Rust and in 2021 I used Go. I also post an increasingly-belated writeup of my experience and impressions of the language so, at the end o...| effectivetypescript.com
We talk all the time about how to define and use types in TypeScript, but we rarely talk about how TypeScript chooses to display our types. There are often several possible ways to display the same type, and the choice can have a big impact on the usability of your library. TypeScript tries to make good decisions on its own about type display, but it also gives us a few very obscure levers by which we can control it ourselves. Let's dive in to the strange world of type display!| effectivetypescript.com
It'll surprise no one to hear that TypeScript is my favorite programming language. But I do still enjoy dabbling in other languages. It's a great way to get perspective on what makes TypeScript unique, and how other language designers are thinking about the same problems. My favorite way to learn a new language is through the annual Advent of Code (AoC). AoC runs every year from Dec 1-25. Every day unlocks a new puzzle with two parts which build on each other. Lots of people do these puzzles ...| effectivetypescript.com
Hang out on the internet much and you'll hear gripes about how TypeScript isn't "sound," and that this makes it a poor choice of language. In this post, I'll explain what this means and walk through the sources of unsoundness in TypeScript. Rest assured, TypeScript is a great language and it's never a good idea to listen to people on the internet!| effectivetypescript.com
I've written many words about TypeScript and I'm sure I've read even more. Here are four words that make me cringe every time I see them. If you write about TypeScript, please steer clear of these!| effectivetypescript.com
TypeScript's type system has grown steadily more powerful over the past five years, allowing you to precisely type more and more patterns in JavaScript. The upcoming TypeScript 4.1 release includes a particularly exciting new addition to the type system: template literal types. Template literal types solve a long-standing gap in TypeScript's type system and, as I'll argue at the end of the post, they solve it in a particularly TypeScripty way. To understand template literal types, let's start...| effectivetypescript.com