Addendum to my nominal types: For the 'fake' nominal types (you can't put a symbol on a number!) I like to make those fields optional, which makes the type less of a lie. "If this number did have a [unit symbol] property, the value would be 'USD'". That will still prevent accidental USD <-> CAD conversion, but allows converting to/from regular numbers without explicit casting.
A pile of thoughts on the subject from back when I first came up with it: http://www.nuke24.net/plog/32.html
Re:TypeScript 5.0
by triyambakam on Friday March 17, 2023 @02:30AM
(#35193911)
Very cool
This is really great. I once spent a little time thinking about how to better represent RDF in Type/Javascript. These approaches seem like a good direction to go in. Would love to see these ideas developed further.
Re:TypeScript 5.0
by isleyaardvark on Sunday March 19, 2023 @11:30AM
(#35219949)
I’ve seen several TS users describe that approach as “branding” (or “flavoring” if it is optional).
Want structural/duck typing? No problem; that's the default. Define new interfaces that old classes happen to implement without having to wrap them. I always wanted that feature!
Want nominal types? No problem; just declare a field indicating the type name!
(I like to express things in a way that can be easily translated to RDF, hence my use of URIs for type and attribute names)Since TypeScript types don't runtime checks, you can even do it on atomic types!