This TypeScript type haunted my dreams

After a year of being on sabbatical (that’s fancy-talk for unemployed), I recently joined Salto’s engineering team and started learning TypeScript. Salto allows business application admins to use DevOps methodlogies when managing their configuration. One of the cool things about them is that their core business logic is open source. As part of that OSS repo, there’s a standalone library called lowerdash, where I found they have a nifty little type called OneOf to express mutually-exclusive properties. Here’s an example:


This is a companion discussion topic for the original entry at https://amir.rachum.com/typescript-oneof

Let’s put as Key aside for now […]

What does as Key do? Either I just can’t find it or it was never explained later in the article.

I could be wrong, but I suspect it’s just a kludge to disambiguate the following extends clause? In the phrase Key in keyof T extends..., extends could either refer to Key or T. So maybe inserting as Key before extends forces the Key extends... interpretation instead of T extends.... Otherwise it’s just a pointless cast, like writing let foo: string = "foo" as string;

TypeScript has probably the most expressively powerful type system on the planet, and I love it for that, but the syntax around generic constraints is super awkward. I wish it just had where clauses like C# or Rust.

1 Like