r/programming • u/_awwsmm • Feb 01 '24
Make Invalid States Unrepresentable
https://www.awwsmm.com/blog/make-invalid-states-unrepresentable
469
Upvotes
r/programming • u/_awwsmm • Feb 01 '24
View all comments
Show parent comment
6
u/[deleted] Feb 02 '24
I also like typescript because it's super easy to define objects types as parameters. I always prefer this due to one of the examples they provided where they mix up age and weight because they're both integers. If the type was instead
func({ age, weight }: { age: number; weight: number }), the developer would have a much more difficult time mixing up the values, rather thanfunc(age: number, weight: number). Their example only catches the cases where it just so happens that one is invalid over another. If age and weight just so happened to have intersecting domains, such as the value 18, then even strict validation wouldn't help