top | item 46144936

(no title)

SebastianKra | 2 months ago

Yeah that's one of the practical workarounds I mentioned. It mostly comes up while spreading (which is why spreads should usually go first) and when persisting (which is why your ORM should have a runtime schema).

But it's a double edged sword:

- You can brand types with additional information. For example, React Query does this to keep track of which type is stored under which cache-key. This is where most strongly typed languages just give up.

- You can progressively extend/narrow types without having to worry about inheritance. Invariants can be statically checked by encoding them in a type: eg IssuedInvoice = Invoice & { … }. A function that needs only a subset of a domain-object can specify this rather than requiring the email-address to verify a phone number.

- You can even emulate nominal types using keyed unions.

The first two alone have saved me from more errors than structural typing introduced.

discuss

order

No comments yet.