| ▲ | throwaw12 6 hours ago | ||||||||||||||||
I personally love the idea and concept, but struggle to apply to real projects. Suppose I have a User with some attributes like birthday, email and whether they have been verified. in common codebase, you can see `if (user.verified_at != null)` or something along the lines, in case of parsed code I do feel like I should have types for each of them (or interfaces):
(and imagine having a method which accepts user with birthday and email to send an email day before their birthday, would you create UserWithBirthdayAndEmail type?)it feels like it is going to bloat the interface space, how do you tackle this problem? | |||||||||||||||||
| ▲ | bern4444 5 hours ago | parent | next [-] | ||||||||||||||||
It's pretty trivial to create derived and augmented types with Pick, Omit, Required, Partial. Combined with a few parsing functions that return an object typed to whatever specification you need and you are set IE:
Any caller of userHasBDayAndEmail knows for the rest of its nested call stack if the provided user is a User object or a VerifiedUserWithBirthday.The types are cheap to write (they're all derived) and have no runtime impact (types are erased at build/compile time) and these parsing functions are quite small to write https://www.typescriptlang.org/play/?#code/FAFwngDgpgBAqgZyg... | |||||||||||||||||
| |||||||||||||||||
| ▲ | columnarx3 6 hours ago | parent | prev | next [-] | ||||||||||||||||
I think this is the wrong pattern in this instance. You parse an email or phone number because validating leaves it as a plain string, and you lose the context to know for sure if that string is actually an email or phone number. In your instance, you could have:
In this instance, your logic with a method that accepts birthday and email has all the information it needs to make its choice. | |||||||||||||||||
| ▲ | sirwhinesalot 6 hours ago | parent | prev | next [-] | ||||||||||||||||
The computer-science answer to this problem are called "refinement types", where you can attach arbitrary predicates to a type, e.g. (pseudo-code):
Contracts are a similar solution that restricts the predicates to only appearing in function types.The difference between this and an assert is that it gets checked at compile time (it can get quite expensive to do the check though). What can you do in mainstream languages? As much as is worth and no more than that. String -> User is worth it, User -> UserWithBirthday is not. | |||||||||||||||||
| |||||||||||||||||
| ▲ | win311fwg 5 hours ago | parent | prev [-] | ||||||||||||||||
> Suppose I have a User with some attributes like birthday, email and whether they have been verified. Philosophically, birthday and email are not attributes of a user. If you remove a user from existence, a birthdate and email address still exist. So... > would you create UserWithBirthdayAndEmail type ...yes, something like a `profile { user, birthday, email }` type is necessary to compose the attributes you are interested in into something where those attributes do belong together. > it feels like it is going to bloat the interface space, how do you tackle this problem? Like all things formal verification, increase the level of verification in your critical sections and don't sweat the non-critical sections. How impactful will it be to your business if sending a birthday email message fails? | |||||||||||||||||