2 ms·
Yup, I have been using branded types in TS to avoid similar issues, and I still do miss F# units of measure. Even though branded types prevent assignment of va
by lf-non 5y ago
Yup, I have been using branded types in TS to avoid similar issues, and I still do miss F# units of measure.
Even though branded types prevent assignment of values with invalid units, opaque types are inferior in that the compiler doesn't know the relationship between the types (as exemplified in the parent comment) and you need a slew of helper functions & casting.
- eyelidlessness 5y agoI use a variant of branding in TS for this too, but it’s very much shoving semantics into the language that aren’t meant to be there. Subclasses of primitive types would be semantically closer to F#, but at the expense of being able to ever treat the values as values
- WorldMaker 5y agoI just realized that you could probably do a decently complex unit algebra (at least to the basic level of F#'s unit of measure support) using the unit strings as brands and Typescript's template string types to match them. I'm not sure if that's a good idea or not in practice, but after seeing Wordle done with template string types I'm more certain than ever it is possible to do some interesting things with it.