this post was submitted on 19 Jul 2023
9 points (100.0% liked)

TypeScript

176 readers
1 users here now

TypeScript is a language for application-scale JavaScript development. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript.

founded 3 years ago
MODERATORS
 

cross-posted from: https://lemmygrad.ml/post/987224

I came up with a trick to iterate over string union types (kind of). I don't know if anyone has discovered this independently, but I think it's too useful not to share.

You can do it with a pattern like this:

const values = ['foo', 'bar', 'baz'] as const;

type Value = (typeof values)[number];

Let's break down what is going on here. values is a list of strings, however the as const makes it so that Typescript treats it as a tuple of string literals instead of just string[]. When defining Value, we use that tuple type to get the type of what you would get by indexing the tuple with a number. Since that type could be any of the string literals in the tuple, the resulting type is a union of all the literals in the tuple, i.e. 'foo' | 'bar' | 'baz'. This makes it so you can use Value as the union type, and if you ever want to iterate over all the values of the union, you can do that with values. This should allow you to do the one thing that Typescript's crappy enums has over string unions.

You can see this technique in action in the bot library I made.

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here