Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Libs provide something out of the box to do this, e.g. `ts-essentials` provides `Opaque`, so you just write `type DateString = Opaque<string, 'DateString'>`


Their version is essentially my Symbol approach, but only in the type system. This does work, but it has the disadvantage that the type and runtime don't match. It’s certainly better than most Brand types which tend to rely on strings, but I prefer not to expose public properties which don’t exist.

The class/private approach has the advantage of hiding the extra property from everything except the type checker.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: