News for hackers. Stuff that matters.
Re:Use Your Type System by jshxr on Thursday July 24, 2025 @11:43AM (#44672097)
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.
Re:Use Your Type System by paldepind2 on Thursday July 24, 2025 @02:59PM (#44674676)
I 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.