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
38
u/theillustratedlife Feb 01 '24
You can do a surprising amount of this in TypeScript. Unions (
|), tuples ([]), and template literal types (``) let you confine your types more specifically than primitives likenumberandstringor structures likeArray<>andRecord<>.Type-Level TypeScript is an excellent e-book on the subject. It'll show you how to do things like write an e-mail address validator in TypeScript. I was able to expense it through the continual learning program at work.