You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/posts/2020-05-25-getting-help-from-your-compiler/index.md
+35-6Lines changed: 35 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,11 +12,31 @@ tags:
12
12
- typescript
13
13
- either
14
14
- validation
15
+
- react
15
16
authors:
16
17
- stefan
17
18
---
18
19
19
-
*I'm learning about functional programming. I'm having loads of fun doing that, but I also think my code is improving because of it. In this post I want to share something that I learned about using Either to do form validation in TypeScript.*
20
+
*I'm learning about functional programming. I'm having loads of fun doing that,
21
+
but I also think my code is improving because of it. In this post I want to
22
+
share something that I learned about using Either to do form validation in
@@ -362,7 +382,7 @@ remind you of what our validation function is returning.
362
382
363
383
In Semantic UI, the `Form` element has to know whether there's an error in the
364
384
form. That's easy, we use the `isLeft` API function of `Either`. So we replace
365
-
the `<form>` element with:
385
+
the `<Form>` element with:
366
386
367
387
```tsx
368
388
import { isLeft } from'fp-ts/lib/Either'
@@ -437,6 +457,10 @@ highly reusable. The other conditionals that you would normally need are now
437
457
abstracted away in the `Either` and `Validation`, so that you can't do it wrong
438
458
anymore. I don't know about you, but I'm happy. 😀
439
459
460
+
The full source code and a completely working example can be found on
461
+
[github][demo-repo]. As a bonus, it also contains a couple of Jest matchers for
462
+
checking Eithers in unit tests.
463
+
440
464
## A New Vocabulary
441
465
442
466
There is one last observation that I'd like to make. All your "C-style"
@@ -449,7 +473,11 @@ languages, that allow you to talk and reason about code.
449
473
As I challenge myself to use the functional programming concepts more and more,
450
474
like I did in this article, I find that it gives me a whole new vocabulary in a
451
475
similar way, but on a higher abstraction level. Learn how to use `fp-ts` in
452
-
TypeScript, and you use the same constructs in Haskell, PureScript, Scala, etc. So any investment that you make in learning this stuff is not limited to the specific programming language that you learn it for. And it's going to allow you to write your code using higher-level abstractions, thereby reducing hopefully the number of bugs.
476
+
TypeScript, and you use the same constructs in Haskell, PureScript, Scala, etc.
477
+
So any investment that you make in learning this stuff is not limited to the
478
+
specific programming language that you learn it for. And it's going to allow you
479
+
to write your code using higher-level abstractions, thereby reducing hopefully
480
+
the number of bugs.
453
481
454
482
## Appendix: lift and lift2
455
483
@@ -516,3 +544,4 @@ export function lift2<E, A, B, C> (
0 commit comments