3 ms·
Unfortunately, this can be somewhat awkward to implement in certain structural typed languages like TypeScript. I often find myself writing something along the
by jshxr 1y ago
Unfortunately, this can be somewhat awkward to implement in certain structural typed languages like TypeScript. I often find myself writing something along the lines of
type UserID = string & { readonly __tag: unique symbol }
which always feels a bit hacky.
- deleted 1y ago[deleted]
- deleted 1y ago[deleted]
- paldepind2 1y agoI never understood why people are so keen to do that in TypeScript. With that definition a `UserID` can still be silently "coerced" to a `string` everywhere. So you only get halfway there to an encapsulated type. I think it's a much better idea to do: type UserID = { readonly __tag: unique symbol } Now clients of `UserID` no longer knows anything about the representation. Like with the original approach you need a bit of casting, but that can be neatly encapsulated as it would be in the original approach anyway.