Effective TypeScript

フィード

記事のアイキャッチ画像
Item 74: Know How to Reconstruct Types at Runtime
Effective TypeScript
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 solutions to it.
2ヶ月前
記事のアイキャッチ画像
Notes on TypeScript 5.6
Effective TypeScript
<p>We TypeScript developers are a lucky bunch. While some languages (<a href="https://en.wikipedia.org/wiki/History_of_Python">Python</a>, <a href="https://en.wikipedia.org/wiki/ECMAScript_version_history">JavaScript</a>) are released annually, every three years (<a href="https://en.wikipedia.org/wiki/C%2B%2B#Standardization">C++</a>) or even less, we get <em>four</em> new versions of TypeScript every year. TypeScript 5.6 was <a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-6/">released</a> on September 9th, 2024. Let&#39;s take a look.</p>
3ヶ月前
記事のアイキャッチ画像
A keyof puzzle
Effective TypeScript
<p><em>Effective TypeScript</em> is nearly 400 pages long, but I&#39;ve received the most feedback by far on just one passage. It comes in <a href="https://github.com/danvk/effective-typescript/blob/main/samples/ch-types/types-as-sets.md">Item 7: Think of Types as Sets of Values</a>:</p><blockquote><figure class="highlight ts"><table><tr><td class="code"><pre><code class="hljs ts">keyof (A&amp;B) = (keyof A) | (keyof B)<br>keyof (A|B) = (keyof A) &amp; (keyof B)<br></code></pre></td></tr></table></figure><p>If you can build an intuition for why these equations hold, you&#39;ll have come a long way toward understanding TypeScript&#39;s type system!</p></blockquote><p>I&#39;ll explain these equations in a moment. But before I do, head over to the <a href="https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgHJwLYQCYAUD2oYyA3gFDLIiYQBcyAzmFKAOYDcFyAHvSAK4YARtE6UAnn0EionAL5ki0eEmQEiAZgAipLryrTRXSQeFHKALylnZZBWQT4QTKjWwANAJoAtZAF5dSmosegByVAgAd2RPfCgAa1CAGj16AFoAdg0UiXoAFgAGHOQrZC
4ヶ月前
記事のアイキャッチ画像
A TypeScripter's Take on Zig (Advent of Code 2023)
Effective TypeScript
<p>What can Zig learn from TypeScript, and what can TypeScript learn from Zig?</p>
5ヶ月前
記事のアイキャッチ画像
TypeScript 5.5: A Blockbuster Release
Effective TypeScript
<p>We TypeScript developers are a lucky bunch. While some languages (<a href="https://en.wikipedia.org/wiki/History_of_Python">Python</a>, <a href="https://en.wikipedia.org/wiki/ECMAScript_version_history">JavaScript</a>) are released annually, every three years (<a href="https://en.wikipedia.org/wiki/C%2B%2B#Standardization">C++</a>) or even less, we get <em>four</em> new versions of TypeScript every year. TypeScript 5.5 was <a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-5/">released</a> on June 20th, 2024, and it was a real blockbuster. Let&#39;s take a look.</p>
6ヶ月前
記事のアイキャッチ画像
Item 36: Use a Distinct Type for Special Values
Effective TypeScript
It's tempting to use <code>""</code>, <code>0</code> or <code>-1</code> as special values: an empty string might represent text that hasn't loaded yet, or <code>-1</code> 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 <code>null</code>, allows TypeScript to enforce that you do so.
6ヶ月前
記事のアイキャッチ画像
Now Available: Effective TypeScript, Second Edition
Effective TypeScript
Fully updated, thoroughly revised, now with 50% more book!
7ヶ月前
記事のアイキャッチ画像
The Making of a TypeScript Feature: Inferring Type Predicates
Effective TypeScript
<p>Over the past few months I became a TypeScript contributor and implemented a new feature, <a href="https://github.com/microsoft/TypeScript/pull/57465">type predicate inference</a>, 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 <a href="https://github.com/microsoft/TypeScript/pull/57465">the PR</a> merged, and what I&#39;ve learned along the way.</p><p>This is not a short read, but it will give you a good sense of what it&#39;s like to become a TypeScript contributor and develop a new feature.</p>
8ヶ月前
記事のアイキャッチ画像
Flow Nodes: How Type Inference Is Implemented
Effective TypeScript
<p>If a variable gets a type but no one looks at it, does it really get a type at all?</p><p>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.</p>
9ヶ月前
記事のアイキャッチ画像
The Hidden Side of Type Predicates
Effective TypeScript
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 <code>false</code>.
10ヶ月前
記事のアイキャッチ画像
Effective TypeScript Talk at Etsy (Dec 2020)
Effective TypeScript
<p>Back in 2020 I gave a whole series of <a href="https://amzn.to/3UjPrsK"><em>Effective TypeScript</em></a> 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&#39;s about an hour.</p>
1年前
記事のアイキャッチ画像
Don't Write Traditional Getter and Setter Methods in JavaScript and TypeScript
Effective TypeScript
<p>Getter and setter methods (<code>getFoo</code>, <code>setFoo</code>) 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.</p>
1年前
記事のアイキャッチ画像
Using infer to unpack nested types
Effective TypeScript
TypeScript's <code>infer</code> 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.
1年前
記事のアイキャッチ画像
Overload on the type of this to specialize generics (The Lost Item)
Effective TypeScript
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.
1年前
記事のアイキャッチ画像
The Saga of the Closure Compiler, and Why TypeScript Won
Effective TypeScript
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.
1年前
記事のアイキャッチ画像
TypeScript and SQL: Six Ways to Bridge the Divide
Effective TypeScript
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.
1年前
記事のアイキャッチ画像
Recommendation Update: ✂️ Use knip to detect dead code and types
Effective TypeScript
<p>TL;DR: Use <a href="https://github.com/nadeesha/ts-prune">ts-prune</a> is in maintenance mode. Use <a href="https://github.com/webpro/knip">knip</a> to find dead code instead. It&#39;s great!</p>
1年前
記事のアイキャッチ画像
Notes on TypeScript 5.1
Effective TypeScript
<p>Every three months we get a new TypeScript release and <a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-1/">TypeScript 5.1</a> 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&#39;s take a look!</p>
1年前
記事のアイキャッチ画像
Item 30: Don’t Repeat Type Information in Documentation
Effective TypeScript
<p><em>Chapter 4 of <a href="https://amzn.to/3HIrQN6">Effective TypeScript</a> 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!</em></p><p>What’s wrong with this code?</p><figure class="highlight ts"><table><tr><td class="code"><pre><code class="hljs ts"><span class="hljs-comment">/**</span><br><span class="hljs-comment"> * Returns a string with the foreground color.</span><br><span class="hljs-comment"> * Takes zero or one arguments. With no arguments, returns the</span><br><span class="hljs-comment"> * standard foreground color. With one argument, returns the foreground color</span><br><span class="hljs-comment"> * for a particular page.</span><br><span class="hljs-comment"> */</span><br><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">getForegroundColor<
2年前
記事のアイキャッチ画像
A first look at Deno through the Advent of Code 2022
Effective TypeScript
<p><img src="https://effectivetypescript.com/images/advent-of-code.png" title="Advent of Code Logo" width="64" height="64" style="float: right; margin-left: 10px;">Every year I do the <a href="https://adventofcode.com/">Advent of Code</a> in a different programming language. If you aren&#39;t familiar, it&#39;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&#39;s a great way to learn a language and bond over coding. In <a href="https://danvdk.medium.com/python-tips-tricks-for-the-advent-of-code-2019-89ec23a595dd">2019</a> I used Python, in <a href="https://danvdk.medium.com/advent-of-code-2020-this-time-in-rust-7904559e24bc">2020</a> I used Rust and in <a href="https://effectivetypescript.com/2022/02/06/advent-of-code-2021-golang/">2021</a> I used Go. I also post an increasingly-belated writeup of my experience and impressions of the language so, at the end of April, h
2年前