fosstodon.org is one of the many independent Mastodon servers you can use to participate in the fediverse.
Fosstodon is an invite only Mastodon instance that is open to those who are interested in technology; particularly free & open source software. If you wish to join, contact us for an invite.

Administered by:

Server stats:

10K
active users

Axel Rauschmayer

Neat trick: If we create a Set with a `const` Array, then the type of its elements is the union of the Array element types. In other words: The Set behaves a little like a tuple.

const packStatus = new Set([
'draft',
'approved',
'shipped',
] as const);
// const packStatus: Set<"draft" | "approved" | "shipped">

packStatus.has('abc'); // type error!
// Auto-completion works for the arguments of .has(), .delete(), etc.

Source: Matt Pocock. x.com/mattpocockuk/status/1823

X (formerly Twitter)Matt Pocock (@mattpocockuk) on XLots of TS devs use arrays of unique values to avoid the pitfalls of enums. But why not use a Set instead?