Typesafe Function Overloading in TypeScript

Common frustrations with no easy solution

When helping developers new to TypeScript, I encounter several common patterns of frustration. Two broad categories:

  • Type 1: Users get frustrated that certain JavaScript patterns are hard to express safely in TypeScript. This typically involves runtime reflection, which I won't discuss today and is usually an issue for less experienced or stubborn users (sometimes legitimately!).
  • Type 2: Users understand TypeScript features in isolation but get confused combining them in seemingly natural ways that don't work as expected. Often this is simply because the feature combination hasn't been implemented.

We'll focus on a type 2 problem: function overloading and conditional types in TypeScript. This is an area many find unsatisfactory and I've seen attempted countless times. The intersection of these features comes up often in the TypeScript Discord and at work. The goal is type safety, but the result is confusing compiler errors.

Risks of Mastodon for Journalists

Escape from Musk but end up in a tussle with your employer

Cathy Newman of Channel 4 News, a UK news broadcaster, asks on Twitter:

Ok…Mastodon…erm…which server to use?

The responses are a combination of mutual confusion and assurances that it does not matter and that she can correct any mistake later. What all of the responses fail to mention is that she might not have a choice at all. In at least one possible future where some version of the fediverse prevails, her Mastodon handle will be:

cathy.newman@channel4.com

This has potentially major implications for Newman's future relationships with both with her audiences and her employers.