|
|
|
|
|
by epolanski
1204 days ago
|
|
Also, parsing composes, validating, doesn't. I once had to implement a feature on a real estate website. For a given location I would get a list of stats (demographics, cost per square meter, average salary in the area, etc). Of those stats some themselves contained lists. At the beginning I modeled everything with arrays in react. This led to passing down lists and having to check at multiple steps whether they were non-empty and handle that case. Then I modeled everything with a NonEmptyArray guard in TypeScript ```
interface NonEmptyArray<A> extends Array<A> {
0: A // tells the typesystem that index 0 exists and is of type A
} function isNonEmpty(as: A[]): as is NonEmptyArray<A> { return as.length > 0 }
``` then after receiving the response with those lists I could parse them into NonEmptyArrays, remove all of the checks of emptiness inside the react components till handling the fact that some of these elements were empty trickled up to the outermost component and everything became very clean and simple to understand/maintain. |
|